-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
FactoryDefinition.php
235 lines (186 loc) 路 6.47 KB
/
FactoryDefinition.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
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
<?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;
use Nette\DI\Helpers;
use Nette\DI\ServiceCreationException;
use Nette\PhpGenerator as Php;
use Nette\Utils\Type;
/**
* Definition of standard service.
*/
final class FactoryDefinition extends Definition
{
private const MethodCreate = 'create';
private Definition $resultDefinition;
private ?string $reference = null;
public function __construct()
{
$this->resultDefinition = new ServiceDefinition;
}
public function setImplement(string $interface): static
{
if (!interface_exists($interface)) {
throw new Nette\InvalidArgumentException(sprintf(
"Service '%s': Interface '%s' not found.",
$this->getName(),
$interface,
));
}
$rc = new \ReflectionClass($interface);
$method = $rc->getMethods()[0] ?? null;
if (!$method || $method->isStatic() || $method->name !== self::MethodCreate || count($rc->getMethods()) > 1) {
throw new Nette\InvalidArgumentException(sprintf(
"Service '%s': Interface %s must have just one non-static method create().",
$this->getName(),
$interface,
));
}
Helpers::ensureClassType(Type::fromReflection($method), "return type of $interface::create()");
return parent::setType($interface);
}
public function getImplement(): ?string
{
return $this->getType();
}
public function getResultType(): ?string
{
return $this->resultDefinition->getType();
}
public function setResultDefinition(Definition $definition): static
{
$this->resultDefinition = $definition;
return $this;
}
/** @return ServiceDefinition */
public function getResultDefinition(): Definition
{
return $this->resultDefinition;
}
public function resolveType(Nette\DI\Resolver $resolver): void
{
if (!$this->getType()) {
throw new ServiceCreationException('Type is missing in definition of service.');
} elseif ($this->reference === null) {
$this->resultDefinition->setAutowired(false);
$this->reference = $resolver->getContainerBuilder()->addDefinition(null, $this->resultDefinition)->getName();
}
$type = Type::fromReflection(new \ReflectionMethod($this->getType(), self::MethodCreate));
$resultDef = $this->resultDefinition;
try {
$resolver->resolveDefinition($resultDef);
} catch (ServiceCreationException $e) {
if ($resultDef->getType()) {
throw $e;
}
$resultDef->setType($type->getSingleName());
$resolver->resolveDefinition($resultDef);
}
if (!$type->allows($resultDef->getType())) {
throw new ServiceCreationException(sprintf(
'Factory for %s cannot create incompatible %s type.',
$type,
$resultDef->getType(),
));
}
}
public function complete(Nette\DI\Resolver $resolver): void
{
$resultDef = $this->resultDefinition;
if ($resultDef instanceof ServiceDefinition) {
$this->completeParameters($resolver);
$this->convertArguments($resultDef->getCreator()->arguments);
foreach ($resultDef->getSetup() as $setup) {
$this->convertArguments($setup->arguments);
}
if ($resultDef->getEntity() instanceof Reference && !$resultDef->getCreator()->arguments) {
$resultDef->setCreator([ // render as $container->createMethod()
new Reference(Nette\DI\ContainerBuilder::ThisContainer),
Nette\DI\Container::getMethodName($resultDef->getEntity()->getValue()),
]);
}
}
$resolver->completeDefinition($resultDef);
}
private function completeParameters(Nette\DI\Resolver $resolver): void
{
$interface = $this->getType();
$method = new \ReflectionMethod($interface, self::MethodCreate);
$ctorParams = [];
if (
($class = $resolver->resolveEntityType($this->resultDefinition->getCreator()))
&& ($ctor = (new \ReflectionClass($class))->getConstructor())
) {
foreach ($ctor->getParameters() as $param) {
$ctorParams[$param->name] = $param;
}
}
foreach ($method->getParameters() as $param) {
if (isset($ctorParams[$param->name])) {
$ctorParam = $ctorParams[$param->name];
$ctorType = Type::fromReflection($ctorParam);
if ($ctorType && !$ctorType->allows((string) Type::fromReflection($param))) {
throw new ServiceCreationException(sprintf(
"Type of \$%s in %s::create() doesn't match type in %s constructor.",
$param->name,
$interface,
$class,
));
}
$this->resultDefinition->getCreator()->arguments[$ctorParam->getPosition()] = new Php\Literal('$' . $ctorParam->name);
} elseif (!$this->resultDefinition->getSetup()) {
// [param1, param2] => '$param1, $param2'
$stringifyParams = fn(array $params): string => implode(
', ',
array_map(fn(string $param) => '$' . $param, $params),
);
$ctorParamsKeys = array_keys($ctorParams);
$hint = Nette\Utils\Helpers::getSuggestion($ctorParamsKeys, $param->name);
throw new ServiceCreationException(sprintf(
'Cannot implement %s::create(): factory method parameters (%s) are not matching %s::__construct() parameters (%s).',
$interface,
$stringifyParams(array_map(fn(\ReflectionParameter $param) => $param->name, $method->getParameters())),
$class,
$stringifyParams($ctorParamsKeys),
) . ($hint ? " Did you mean to use '\${$hint}' in factory method?" : ''));
}
}
}
public function convertArguments(array &$args): void
{
foreach ($args as &$v) {
if (is_string($v) && $v && $v[0] === '$') {
$v = new Php\Literal($v);
}
}
}
public function generateMethod(Php\Method $method, Nette\DI\PhpGenerator $generator): void
{
$class = (new Php\ClassType)
->addImplement($this->getType());
$class->addMethod('__construct')
->addPromotedParameter('container')
->setPrivate()
->setType($generator->getClassName());
$methodCreate = $class->addMethod(self::MethodCreate);
$this->resultDefinition->generateMethod($methodCreate, $generator);
$body = $methodCreate->getBody();
$body = str_replace('$this', '$this->container', $body);
$body = str_replace('$this->container->container', '$this->container', $body);
$rm = new \ReflectionMethod($this->getType(), self::MethodCreate);
$methodCreate
->setParameters(array_map((new Php\Factory)->fromParameterReflection(...), $rm->getParameters()))
->setReturnType((string) Type::fromReflection($rm))
->setBody($body);
$method->setBody('return new class ($this) ' . $class . ';');
}
public function __clone()
{
parent::__clone();
$this->resultDefinition = unserialize(serialize($this->resultDefinition));
}
}