1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
|
|
|
|
6
|
|
|
use Bdf\Prime\Entity\Hydrator\HydratorInterface; |
|
|
|
|
7
|
|
|
use Bdf\Prime\Entity\Hydrator\HydratorRegistry; |
8
|
|
|
use Bdf\Prime\Entity\Instantiator\InstantiatorInterface; |
9
|
|
|
use Bdf\Prime\Entity\Instantiator\RegistryInstantiator; |
10
|
|
|
use Bdf\Prime\Mapper\MapperFactory; |
11
|
|
|
use Bdf\Prime\Repository\EntityRepository; |
12
|
|
|
use Bdf\Prime\Repository\RepositoryInterface; |
13
|
|
|
use Bdf\Prime\Types\TypesRegistryInterface; |
|
|
|
|
14
|
|
|
use Bdf\Serializer\SerializerInterface; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ServiceLocator |
19
|
|
|
*/ |
20
|
|
|
class ServiceLocator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ConnectionManager |
24
|
|
|
*/ |
25
|
|
|
private $connectionManager; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var RepositoryInterface[] |
29
|
|
|
*/ |
30
|
|
|
private $repositories = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var MapperFactory |
34
|
|
|
*/ |
35
|
|
|
private $mapperFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var SerializerInterface |
39
|
|
|
*/ |
40
|
|
|
private $serializer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Closure |
44
|
|
|
*/ |
45
|
|
|
private $serializerResolver; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var HydratorRegistry |
49
|
|
|
*/ |
50
|
|
|
private $hydrators; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var InstantiatorInterface |
54
|
|
|
*/ |
55
|
|
|
private $instantiator; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* DI container |
59
|
|
|
* |
60
|
|
|
* @var ContainerInterface |
61
|
|
|
*/ |
62
|
|
|
private $di; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* SericeLocator constructor. |
66
|
|
|
* |
67
|
|
|
* @param ConnectionManager $connectionManager |
68
|
|
|
* @param MapperFactory $mapperFactory |
69
|
|
|
* @param InstantiatorInterface $instantiator |
70
|
|
|
*/ |
71
|
106 |
|
public function __construct(ConnectionManager $connectionManager = null, MapperFactory $mapperFactory = null, InstantiatorInterface $instantiator = null) |
72
|
|
|
{ |
73
|
106 |
|
$this->connectionManager = $connectionManager ?: new ConnectionManager(); |
|
|
|
|
74
|
106 |
|
$this->mapperFactory = $mapperFactory ?: new MapperFactory(); |
|
|
|
|
75
|
106 |
|
$this->instantiator = $instantiator ?: new RegistryInstantiator(); |
|
|
|
|
76
|
106 |
|
$this->hydrators = new HydratorRegistry(); |
77
|
|
|
|
78
|
|
|
// TODO Legacy. Should be removed |
|
|
|
|
79
|
106 |
|
if ($cache = $this->connectionManager->config()->getMetadataCache()) { |
|
|
|
|
80
|
1 |
|
$this->mapperFactory->setMetadataCache($cache); |
81
|
|
|
} |
|
|
|
|
82
|
106 |
|
if ($cache = $this->connectionManager->config()->getResultCache()) { |
|
|
|
|
83
|
|
|
$this->mapperFactory->setResultCache($cache); |
84
|
|
|
} |
85
|
106 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns connection manager |
89
|
|
|
* |
90
|
|
|
* @return ConnectionManager |
91
|
|
|
*/ |
92
|
1105 |
|
public function connections() |
93
|
|
|
{ |
94
|
1105 |
|
return $this->connectionManager; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns connection manager |
99
|
|
|
* |
100
|
|
|
* @return MapperFactory |
101
|
|
|
*/ |
102
|
131 |
|
public function mappers() |
103
|
|
|
{ |
104
|
131 |
|
return $this->mapperFactory; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns connection manager config |
109
|
|
|
* |
110
|
|
|
* @return Configuration |
111
|
|
|
* |
112
|
|
|
* @deprecated Since 1.1. |
|
|
|
|
113
|
|
|
*/ |
114
|
3 |
|
public function config() |
115
|
|
|
{ |
116
|
3 |
|
return $this->connectionManager->config(); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get a db connection |
121
|
|
|
* |
122
|
|
|
* @param string $name |
123
|
|
|
* |
124
|
|
|
* @return ConnectionInterface |
125
|
|
|
*/ |
126
|
1007 |
|
public function connection($name = null) |
127
|
|
|
{ |
128
|
1007 |
|
return $this->connectionManager->getConnection($name); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Register a repository |
133
|
|
|
* |
134
|
|
|
* @param string $entityClass |
135
|
|
|
* @param RepositoryInterface $repository |
136
|
|
|
*/ |
137
|
3 |
|
public function registerRepository($entityClass, RepositoryInterface $repository) |
138
|
|
|
{ |
139
|
3 |
|
$this->repositories[$entityClass] = $repository; |
140
|
3 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Unregister a repository |
144
|
|
|
* |
145
|
|
|
* @param string $entityClass |
146
|
|
|
*/ |
147
|
1 |
|
public function unregisterRepository($entityClass) |
148
|
|
|
{ |
149
|
1 |
|
if (isset($this->repositories[$entityClass]) && $this->repositories[$entityClass] instanceof EntityRepository) { |
150
|
|
|
$this->repositories[$entityClass]->destroy(); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
unset($this->repositories[$entityClass]); |
154
|
1 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get mapper for specified entity |
158
|
|
|
* |
159
|
|
|
* @param string|object $entityClass Name of Entity object to load mapper for |
160
|
|
|
* |
161
|
|
|
* @return RepositoryInterface |
162
|
|
|
*/ |
163
|
934 |
|
public function repository($entityClass) |
164
|
|
|
{ |
165
|
934 |
|
if (is_object($entityClass)) { |
166
|
353 |
|
$entityClass = get_class($entityClass); |
167
|
|
|
} |
168
|
|
|
|
169
|
934 |
|
if (!isset($this->repositories[$entityClass])) { |
170
|
122 |
|
$mapper = $this->mapperFactory->build($this, $entityClass); |
171
|
|
|
|
172
|
122 |
|
if ($mapper === null) { |
173
|
6 |
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
116 |
|
$this->repositories[$entityClass] = $mapper->repository(); |
177
|
|
|
} |
178
|
|
|
|
179
|
931 |
|
return $this->repositories[$entityClass]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get repository names |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
3 |
|
public function repositoryNames() |
188
|
|
|
{ |
189
|
3 |
|
return array_keys($this->repositories); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set the serializer |
194
|
|
|
* |
195
|
|
|
* @param \Closure|SerializerInterface $serializer |
196
|
|
|
* |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
104 |
|
public function setSerializer($serializer) |
200
|
|
|
{ |
201
|
104 |
|
if ($serializer instanceof \Closure) { |
202
|
1 |
|
$this->serializerResolver = $serializer; |
203
|
103 |
|
} elseif ($serializer instanceof SerializerInterface) { |
|
|
|
|
204
|
103 |
|
$this->serializer = $serializer; |
205
|
|
|
} |
206
|
|
|
|
207
|
104 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the serializer |
212
|
|
|
* |
213
|
|
|
* @return SerializerInterface |
214
|
|
|
*/ |
215
|
24 |
|
public function serializer() |
216
|
|
|
{ |
217
|
24 |
|
if ($this->serializerResolver !== null) { |
218
|
1 |
|
$resolver = $this->serializerResolver; |
219
|
1 |
|
$this->serializer = $resolver(); |
220
|
1 |
|
$this->serializerResolver = null; |
221
|
|
|
} |
222
|
|
|
|
223
|
24 |
|
return $this->serializer; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get the entity hydrators registry |
228
|
|
|
* |
229
|
|
|
* @return HydratorRegistry |
230
|
|
|
*/ |
231
|
3 |
|
public function hydrators() |
232
|
|
|
{ |
233
|
3 |
|
return $this->hydrators; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Get the entity hydrator |
238
|
|
|
* |
239
|
|
|
* @param string|object $entity The entity class or object |
240
|
|
|
* |
241
|
|
|
* @return HydratorInterface |
242
|
|
|
*/ |
243
|
565 |
|
public function hydrator($entity) |
244
|
|
|
{ |
245
|
565 |
|
if (is_object($entity)) { |
246
|
497 |
|
$entity = get_class($entity); |
247
|
|
|
} |
248
|
|
|
|
249
|
565 |
|
return $this->hydrators->get($entity); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get the entity instantiator |
254
|
|
|
* |
255
|
|
|
* @return InstantiatorInterface |
256
|
|
|
*/ |
257
|
493 |
|
public function instantiator() |
258
|
|
|
{ |
259
|
493 |
|
return $this->instantiator; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get the types registry |
264
|
|
|
* |
265
|
|
|
* @return TypesRegistryInterface |
266
|
|
|
* |
267
|
|
|
* @deprecated Since 1.1. |
|
|
|
|
268
|
|
|
*/ |
269
|
110 |
|
public function types() |
270
|
|
|
{ |
271
|
110 |
|
return $this->connectionManager->config()->getTypes(); |
|
|
|
|
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* DI accessor |
276
|
|
|
* |
277
|
|
|
* @return ContainerInterface |
278
|
|
|
*/ |
279
|
1 |
|
public function di() |
280
|
|
|
{ |
281
|
1 |
|
return $this->di; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* DI accessor |
286
|
|
|
* |
287
|
|
|
* @param ContainerInterface $di |
288
|
|
|
* |
289
|
|
|
* @return $this |
290
|
|
|
*/ |
291
|
1 |
|
public function setDI(ContainerInterface $di) |
|
|
|
|
292
|
|
|
{ |
293
|
1 |
|
$this->di = $di; |
294
|
|
|
|
295
|
1 |
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Clear all cache repositories |
300
|
|
|
*/ |
301
|
1 |
|
public function clearRepositories() |
302
|
|
|
{ |
303
|
1 |
|
foreach ($this->repositories as $repository) { |
304
|
1 |
|
if ($repository instanceof EntityRepository) { |
305
|
1 |
|
$repository->destroy(); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
1 |
|
$this->repositories = []; |
310
|
1 |
|
} |
311
|
|
|
} |
312
|
|
|
|