1
|
|
|
<?php |
2
|
|
|
namespace Del\Common; |
3
|
|
|
|
4
|
|
|
use Pimple\Container as PimpleContainer; |
5
|
|
|
use Del\Common\Container\RegistrationInterface; |
6
|
|
|
use Del\Common\Config\DbCredentials; |
7
|
|
|
use Doctrine\ORM\Tools\Setup; |
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
|
10
|
|
|
class ContainerService |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var PimpleContainer |
14
|
|
|
*/ |
15
|
|
|
private $container; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var DbCredentials |
19
|
|
|
*/ |
20
|
|
|
private $credentials; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $paths; |
26
|
|
|
|
27
|
|
|
/** @var string $proxyPath */ |
28
|
|
|
private $proxyPath; |
29
|
|
|
|
30
|
|
|
/** @var \Doctrine\ORM\Configuration $config */ |
31
|
|
|
private $config; |
32
|
|
|
|
33
|
|
|
public function __construct(){} |
34
|
|
|
public function __clone(){} |
35
|
|
|
|
36
|
9 |
|
public static function getInstance() |
37
|
|
|
{ |
38
|
9 |
|
static $inst = null; |
39
|
9 |
|
if($inst === null) |
40
|
9 |
|
{ |
41
|
1 |
|
$inst = new ContainerService(); |
42
|
1 |
|
$inst->container = new PimpleContainer(); |
43
|
1 |
|
$inst->paths = ['src/Entity']; |
44
|
1 |
|
} |
45
|
9 |
|
return $inst; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return PimpleContainer |
51
|
|
|
*/ |
52
|
7 |
|
public function getContainer() |
53
|
|
|
{ |
54
|
7 |
|
if (!isset($this->dbInitialised)) { |
55
|
|
|
|
56
|
7 |
|
$this->container['db.credentials'] = $this->getDbCredentials()->toArray(); |
57
|
7 |
|
$this->container['entity.paths'] = $this->getEntityPaths(); |
58
|
|
|
|
59
|
7 |
|
$paths = $this->container['entity.paths']; |
60
|
7 |
|
$dbParams = $this->container['db.credentials']; |
61
|
7 |
|
$isDevMode = false; |
62
|
|
|
|
63
|
7 |
|
$this->config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode); |
64
|
7 |
|
$this->addProxyPath(); |
65
|
7 |
|
$entityManager = EntityManager::create($dbParams, $this->config); |
66
|
|
|
|
67
|
|
|
// The Doctrine Entity Manager |
68
|
7 |
|
$this->container['doctrine.entity_manager'] = $entityManager; |
69
|
7 |
|
$this->container['dbInitialised'] = true; |
70
|
|
|
|
71
|
7 |
|
} |
72
|
7 |
|
return $this->container; |
73
|
|
|
} |
74
|
|
|
|
75
|
7 |
|
private function addProxyPath() |
76
|
|
|
{ |
77
|
7 |
|
if(isset($this->proxyPath)) { |
78
|
1 |
|
$this->config->setProxyDir($this->proxyPath); |
79
|
1 |
|
} |
80
|
7 |
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $path |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
4 |
|
public function addEntityPath($path) |
89
|
|
|
{ |
90
|
4 |
|
$this->paths[] = $path; |
91
|
4 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
8 |
|
public function getEntityPaths() |
98
|
|
|
{ |
99
|
8 |
|
return $this->paths; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $proxyPath |
104
|
|
|
* @return ContainerService |
105
|
|
|
*/ |
106
|
2 |
|
public function setProxyPath($proxyPath) |
107
|
|
|
{ |
108
|
1 |
|
$this->proxyPath = $proxyPath; |
109
|
2 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return DbCredentials |
116
|
|
|
*/ |
117
|
8 |
|
public function getDbCredentials() |
118
|
|
|
{ |
119
|
8 |
|
return $this->credentials ? $this->credentials : new DbCredentials(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param DbCredentials $credentials |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
3 |
|
public function setDbCredentials(DbCredentials $credentials) |
127
|
|
|
{ |
128
|
3 |
|
$this->credentials = $credentials; |
129
|
3 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
2 |
|
public function registerToContainer(RegistrationInterface $config) |
134
|
|
|
{ |
135
|
2 |
|
if($config->hasEntityPath()) { |
136
|
1 |
|
$this->addEntityPath($config->getEntityPath()); |
137
|
1 |
|
} |
138
|
2 |
|
$config->addToContainer($this->container); |
139
|
2 |
|
} |
140
|
|
|
} |
141
|
|
|
|