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