|
1
|
|
|
<?php |
|
2
|
|
|
namespace Del\Common; |
|
3
|
|
|
|
|
4
|
|
|
use Barnacle\Container; |
|
5
|
|
|
use Barnacle\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 Container |
|
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
|
|
|
private function __construct(){} |
|
34
|
|
|
private function __clone(){} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Container|null $container |
|
38
|
|
|
* @return ContainerService|null |
|
39
|
|
|
*/ |
|
40
|
9 |
|
public static function getInstance(Container $container = null) |
|
41
|
|
|
{ |
|
42
|
9 |
|
static $inst = null; |
|
43
|
9 |
|
if($inst === null) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$inst = new ContainerService(); |
|
46
|
1 |
|
$inst->container = $container ?: new Container(); |
|
47
|
1 |
|
$inst->paths = $inst->initEntityPaths(); |
|
48
|
|
|
} |
|
49
|
9 |
|
return $inst; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* by default looks for src/Entity |
|
54
|
|
|
* or src/anything/Entity |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
1 |
|
private function initEntityPaths(): array |
|
59
|
|
|
{ |
|
60
|
1 |
|
$paths = []; |
|
61
|
1 |
|
$paths = $this->addPathIfExists($paths, 'src'); |
|
62
|
1 |
|
$possibleModules = glob('src/*'); |
|
63
|
|
|
|
|
64
|
1 |
|
foreach ($possibleModules as $path) { |
|
65
|
1 |
|
$paths = $this->addPathIfExists($paths, $path); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
return $paths; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param array $paths |
|
73
|
|
|
* @param string $path |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
1 |
|
private function addPathIfExists(array $paths, string $path): array |
|
77
|
|
|
{ |
|
78
|
1 |
|
if (is_dir($path . '/Entity')) { |
|
79
|
|
|
$paths[] = $path . '/Entity'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return $paths; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return Container |
|
88
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
89
|
|
|
*/ |
|
90
|
7 |
|
public function getContainer() |
|
91
|
|
|
{ |
|
92
|
7 |
|
if (!isset($this->dbInitialised)) { |
|
93
|
|
|
|
|
94
|
7 |
|
$this->container['db.credentials'] = $this->getDbCredentials()->toArray(); |
|
95
|
7 |
|
$this->container['entity.paths'] = $this->getEntityPaths(); |
|
96
|
|
|
|
|
97
|
7 |
|
$paths = $this->container['entity.paths']; |
|
98
|
7 |
|
$dbParams = $this->container['db.credentials']; |
|
99
|
7 |
|
$isDevMode = false; |
|
100
|
|
|
|
|
101
|
7 |
|
$this->config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false); |
|
102
|
7 |
|
$this->addProxyPath(); |
|
103
|
7 |
|
$entityManager = EntityManager::create($dbParams, $this->config); |
|
104
|
|
|
|
|
105
|
|
|
// The Doctrine Entity Manager |
|
106
|
7 |
|
$this->container['doctrine.entity_manager'] = $entityManager; |
|
107
|
7 |
|
$this->container[EntityManager::class] = $entityManager; |
|
108
|
7 |
|
$this->container['dbInitialised'] = true; |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
7 |
|
return $this->container; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
7 |
|
private function addProxyPath() |
|
115
|
|
|
{ |
|
116
|
7 |
|
if(isset($this->proxyPath)) { |
|
117
|
1 |
|
$this->config->setProxyDir($this->proxyPath); |
|
118
|
|
|
} |
|
119
|
7 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $path |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
4 |
|
public function addEntityPath($path) |
|
127
|
|
|
{ |
|
128
|
4 |
|
if (is_dir($path)) { |
|
129
|
2 |
|
$this->paths[] = $path; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
4 |
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
8 |
|
public function getEntityPaths() |
|
139
|
|
|
{ |
|
140
|
8 |
|
return $this->paths; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $proxyPath |
|
145
|
|
|
* @return ContainerService |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function setProxyPath($proxyPath) |
|
148
|
|
|
{ |
|
149
|
1 |
|
$this->proxyPath = $proxyPath; |
|
150
|
1 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return DbCredentials |
|
156
|
|
|
*/ |
|
157
|
8 |
|
public function getDbCredentials() |
|
158
|
|
|
{ |
|
159
|
8 |
|
return $this->credentials ? $this->credentials : new DbCredentials(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param DbCredentials $credentials |
|
164
|
|
|
* @return $this |
|
165
|
|
|
*/ |
|
166
|
3 |
|
public function setDbCredentials(DbCredentials $credentials) |
|
167
|
|
|
{ |
|
168
|
3 |
|
$this->credentials = $credentials; |
|
169
|
3 |
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param RegistrationInterface $config |
|
174
|
|
|
*/ |
|
175
|
2 |
|
public function registerToContainer(RegistrationInterface $config) |
|
176
|
|
|
{ |
|
177
|
2 |
|
if($config->hasEntityPath()) { |
|
178
|
1 |
|
$this->addEntityPath($config->getEntityPath()); |
|
179
|
|
|
} |
|
180
|
2 |
|
$config->addToContainer($this->container); |
|
181
|
2 |
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|