-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
LocatorDefinition.php
173 lines (135 loc) 路 4.47 KB
/
LocatorDefinition.php
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
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\DI\Definitions;
use Nette;
/**
* Multi accessor/factory definition.
*/
final class LocatorDefinition extends Definition
{
/** @var Reference[] */
private array $references = [];
private ?string $tagged = null;
public function setImplement(string $interface): static
{
if (!interface_exists($interface)) {
throw new Nette\InvalidArgumentException(sprintf("[%s]\nInterface '%s' not found.", $this->getDescriptor(), $interface));
}
$methods = (new \ReflectionClass($interface))->getMethods();
if (!$methods) {
throw new Nette\InvalidArgumentException(sprintf("[%s]\nInterface %s must have at least one method.", $this->getDescriptor(), $interface));
}
foreach ($methods as $method) {
if ($method->isStatic() || !(
($method->name === 'get' && $method->getNumberOfParameters() === 1)
|| (preg_match('#^(get|create)[A-Z]#', $method->name) && $method->getNumberOfParameters() === 0)
)) {
throw new Nette\InvalidArgumentException(sprintf(
"[%s]\nMethod %s::%s() does not meet the requirements: is create*(), get*() or get(\$name) and is non-static.",
$this->getDescriptor(),
$interface,
$method->name,
));
}
if ($method->name !== 'get') {
Nette\DI\Helpers::ensureClassType(
Nette\Utils\Type::fromReflection($method),
"return type of $interface::$method->name()",
$this->getDescriptor(),
allowNullable: true,
);
}
}
return parent::setType($interface);
}
public function getImplement(): ?string
{
return $this->getType();
}
public function setReferences(array $references): static
{
$this->references = [];
foreach ($references as $name => $ref) {
$this->references[$name] = str_starts_with($ref, '@')
? new Reference(substr($ref, 1))
: Reference::fromType($ref);
}
return $this;
}
/** @return Reference[] */
public function getReferences(): array
{
return $this->references;
}
public function setTagged(?string $tagged): static
{
$this->tagged = $tagged;
return $this;
}
public function getTagged(): ?string
{
return $this->tagged;
}
public function resolveType(Nette\DI\Resolver $resolver): void
{
}
public function complete(Nette\DI\Resolver $resolver): void
{
if ($this->tagged !== null) {
$this->references = [];
foreach ($resolver->getContainerBuilder()->findByTag($this->tagged) as $name => $tag) {
if (isset($this->references[$tag])) {
trigger_error(sprintf(
"[%s]\nDuplicated tag '%s' with value '%s'.",
$this->getDescriptor(),
$this->tagged,
$tag,
));
}
$this->references[$tag] = new Reference($name);
}
}
foreach ($this->references as $name => $ref) {
$this->references[$name] = $resolver->normalizeReference($ref);
}
}
public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
{
$class = (new Nette\PhpGenerator\ClassType)
->addImplement($this->getType());
$class->addMethod('__construct')
->addPromotedParameter('container')
->setPrivate()
->setType($generator->getClassName());
foreach ((new \ReflectionClass($this->getType()))->getMethods() as $rm) {
preg_match('#^(get|create)(.*)#', $rm->name, $m);
$name = lcfirst($m[2]);
$nullable = $rm->getReturnType()->allowsNull();
$methodInner = $class->addMethod($rm->name)
->setReturnType((string) Nette\Utils\Type::fromReflection($rm));
if (!$name) {
$class->addProperty('mapping', array_map(fn($item) => $item->getValue(), $this->references))
->setPrivate();
$methodInner->setBody('if (!isset($this->mapping[$name])) {
' . ($nullable ? 'return null;' : 'throw new Nette\DI\MissingServiceException("Service \'$name\' is not defined.");') . '
}
return $this->container->getService($this->mapping[$name]);')
->addParameter('name');
} elseif (isset($this->references[$name])) {
$ref = $this->references[$name]->getValue();
if ($m[1] === 'get') {
$methodInner->setBody('return $this->container->getService(?);', [$ref]);
} else {
$methodInner->setBody('return $this->container->?();', [Nette\DI\Container::getMethodName($ref)]);
}
} else {
$methodInner->setBody($nullable ? 'return null;' : 'throw new Nette\DI\MissingServiceException("Service is not defined.");');
}
}
$method->setBody('return new class ($this) ' . $class . ';');
}
}