-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistry.php
More file actions
234 lines (195 loc) · 6.78 KB
/
Registry.php
File metadata and controls
234 lines (195 loc) · 6.78 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
<?php
/*
* This file is part of the enhavo package.
*
* (c) WE ARE INDEED GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Enhavo\Component\Type;
use Enhavo\Component\Type\Exception\TypeNotFoundException;
use Enhavo\Component\Type\Exception\TypeNotValidException;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class Registry implements RegistryInterface
{
use ContainerAwareTrait;
private string $namespace;
/** @var RegistryEntry[] */
private array $entries = [];
/** @var RegistryExtension[] */
private array $extensions = [];
public function __construct(string $namespace)
{
$this->namespace = $namespace;
}
public function register(string $class, string $id)
{
$this->checkInterface($class);
/* @var string|TypeInterface $class */
$this->checkClassAlreadyExists($class);
$this->checkNameExists($class::getName());
$this->checkParentType($class);
$this->entries[] = new RegistryEntry($id, $class, $class::getName());
}
/**
* @throws TypeNotValidException
*/
private function checkInterface($class): void
{
if (!in_array(TypeInterface::class, class_implements($class))) {
throw TypeNotValidException::invalidInterface($class);
}
}
/**
* @throws TypeNotValidException
*/
private function checkNameExists($name)
{
$entry = $this->getEntry($name);
if (null !== $name && null !== $entry) {
throw TypeNotValidException::nameExists($name, $this->namespace, $entry->getClass());
}
}
/**
* @param string $class
*
* @throws TypeNotValidException
*/
private function checkClassAlreadyExists($class)
{
$entry = $this->getEntry($class);
if (null !== $entry) {
throw TypeNotValidException::classExists($class, $this->namespace);
}
}
/**
* @param string|TypeInterface $class
*
* @throws TypeNotValidException
*/
private function checkParentType($class)
{
/** @var string|TypeInterface $parentClass */
$parentClass = $class::getParentType();
$classes = [$class];
while (null !== $parentClass) {
if (in_array($parentClass, $classes)) {
$classes[] = $parentClass;
throw TypeNotValidException::circleReferences($classes);
}
if (null !== $parentClass && !class_exists($parentClass)) {
throw TypeNotValidException::parentNotFound($class, $parentClass);
}
$reflection = new \ReflectionClass($parentClass);
if (!$reflection->implementsInterface(TypeInterface::class)) {
throw TypeNotValidException::parentInvalidInterface($class, $parentClass);
}
$classes[] = $parentClass;
$class = $parentClass;
$parentClass = $parentClass::getParentType();
}
}
private function getEntry($name): ?RegistryEntry
{
foreach ($this->entries as $entry) {
if (null !== $entry->getName() && $entry->getName() === $name) {
return $entry;
} elseif ($entry->getClass() === $name) {
return $entry;
}
}
return null;
}
/**
* @throws TypeNotFoundException
*/
public function getType(string $name): TypeInterface
{
$entry = $this->getEntry($name);
if (null === $entry) {
throw TypeNotFoundException::notFound($name, $this->namespace, $this->getPossibleNames());
}
if (null === $entry->getService()) {
$entry->setService($this->createService($entry));
}
return clone $entry->getService();
}
private function createService(RegistryEntry $entry): TypeInterface
{
/** @var TypeInterface $service */
$service = $this->container->get($entry->getId());
$parent = $service::getParentType();
if (null !== $parent) {
$service->setParent($this->getType($parent));
}
return $service;
}
private function getPossibleNames()
{
$names = [];
foreach ($this->entries as $entry) {
if (null !== $entry->getName()) {
$names[] = $entry->getName();
}
$names[] = $entry->getClass();
}
return $names;
}
public function registerExtension(string $class, string $id, int $priority = 10)
{
$this->checkExtensionInterface($class);
/* @var string|TypeExtensionInterface $class */
$this->checkExtendedTypeAvailable($class);
$this->extensions[] = new RegistryExtension($id, $class, $class::getExtendedTypes(), $priority);
usort($this->extensions, function (RegistryExtension $a, RegistryExtension $b) {
return $b->getPriority() - $a->getPriority();
});
}
private function checkExtensionInterface($class): void
{
if (!in_array(TypeExtensionInterface::class, class_implements($class))) {
throw TypeNotValidException::invalidExtensionInterface($class);
}
}
private function checkExtendedTypeAvailable($extensionClass)
{
$types = $extensionClass::getExtendedTypes();
foreach ($this->entries as $entry) {
foreach ($types as $type) {
if ($entry->getClass() === $type || ($entry->getName() && $entry->getName() === $type)) {
return;
}
}
}
throw TypeNotValidException::extendedTypeNotExists($extensionClass, $types);
}
public function getExtensions(TypeInterface $type): array
{
$extensions = [];
foreach ($this->extensions as $extension) {
foreach ($extension->getExtendedTypes() as $extendedType) {
if ($this->checkExtends($type, $extendedType)) {
if (null === $extension->getService()) {
$service = $this->container->get($extension->getId());
$extension->setService($service);
}
$extensionService = clone $extension->getService();
$extensions[] = $extensionService;
}
}
}
return $extensions;
}
private function checkExtends($type, $extendedType)
{
if (interface_exists($extendedType) && is_subclass_of($type::class, $extendedType)) {
return true;
} elseif ($type::class === $extendedType) {
return true;
} elseif ($type::getName() && $type::getName() === $extendedType) {
return true;
}
return false;
}
}