/
ServiceDefinition.php
226 lines (178 loc) 路 5.12 KB
/
ServiceDefinition.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
<?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\ServiceCreationException;
/**
* Definition of standard service.
*
* @property string|null $class
* @property Statement $factory
* @property Statement[] $setup
*/
final class ServiceDefinition extends Definition
{
/** @var Statement */
private $creator;
/** @var Statement[] */
private $setup = [];
public function __construct()
{
$this->creator = new Statement(null);
}
/** @return static */
public function setType(?string $type)
{
return parent::setType($type);
}
/**
* @param string|array|Definition|Reference|Statement $factory
* @return static
*/
public function setFactory($factory, array $args = [])
{
return $this->setCreator($factory, $args);
}
public function getFactory(): Statement
{
return $this->creator;
}
/**
* @param string|array|Definition|Reference|Statement $creator
* @return static
*/
public function setCreator($creator, array $args = [])
{
$this->creator = $creator instanceof Statement
? $creator
: new Statement($creator, $args);
return $this;
}
public function getCreator(): Statement
{
return $this->creator;
}
/** @return string|array|Definition|Reference|null */
public function getEntity()
{
return $this->creator->getEntity();
}
/** @return static */
public function setArguments(array $args = [])
{
$this->creator->arguments = $args;
return $this;
}
/** @return static */
public function setArgument($key, $value)
{
$this->creator->arguments[$key] = $value;
return $this;
}
/**
* @param Statement[] $setup
* @return static
*/
public function setSetup(array $setup)
{
foreach ($setup as $v) {
if (!$v instanceof Statement) {
throw new Nette\InvalidArgumentException('Argument must be Nette\DI\Definitions\Statement[].');
}
}
$this->setup = $setup;
return $this;
}
/** @return Statement[] */
public function getSetup(): array
{
return $this->setup;
}
/**
* @param string|array|Definition|Reference|Statement $entity
* @return static
*/
public function addSetup($entity, array $args = [])
{
$this->setup[] = $entity instanceof Statement
? $entity
: new Statement($entity, $args);
return $this;
}
public function resolveType(Nette\DI\Resolver $resolver): void
{
if (!$this->getEntity()) {
if (!$this->getType()) {
throw new ServiceCreationException('Factory and type are missing in definition of service.');
}
$this->setCreator($this->getType(), $this->creator->arguments ?? []);
} elseif (!$this->getType()) {
$type = $resolver->resolveEntityType($this->creator);
if (!$type) {
throw new ServiceCreationException('Unknown service type, specify it or declare return type of factory.');
}
$this->setType($type);
$resolver->addDependency(new \ReflectionClass($type));
}
// auto-disable autowiring for aliases
if ($this->getAutowired() === true && $this->getEntity() instanceof Reference) {
$this->setAutowired(false);
}
}
public function complete(Nette\DI\Resolver $resolver): void
{
$entity = $this->creator->getEntity();
if ($entity instanceof Reference && !$this->creator->arguments && !$this->setup) {
$ref = $resolver->normalizeReference($entity);
$this->setCreator([new Reference(Nette\DI\ContainerBuilder::THIS_CONTAINER), 'getService'], [$ref->getValue()]);
}
$this->creator = $resolver->completeStatement($this->creator);
foreach ($this->setup as &$setup) {
if (
is_string($setup->getEntity())
&& strpbrk($setup->getEntity(), ':@?\\') === false
) { // auto-prepend @self
$setup = new Statement([new Reference(Reference::SELF), $setup->getEntity()], $setup->arguments);
}
$setup = $resolver->completeStatement($setup, true);
}
}
public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
{
$entity = $this->creator->getEntity();
$code = $generator->formatStatement($this->creator) . ";\n";
if (!$this->setup) {
$method->setBody('return ' . $code);
return;
}
$code = '$service = ' . $code;
$type = $this->getType();
if (
$type !== $entity
&& !(is_array($entity) && $entity[0] instanceof Reference && $entity[0]->getValue() === Nette\DI\ContainerBuilder::THIS_CONTAINER)
&& !(is_string($entity) && preg_match('#^[\w\\\\]+$#D', $entity) && is_subclass_of($entity, $type))
) {
$code .= (new Nette\PhpGenerator\Dumper)->format(
"if (!\$service instanceof $type) {\n"
. "\tthrow new Nette\\UnexpectedValueException(?);\n}\n",
"Unable to create service '{$this->getName()}', value returned by factory is not $type type."
);
}
foreach ($this->setup as $setup) {
$code .= $generator->formatStatement($setup) . ";\n";
}
$code .= 'return $service;';
$method->setBody($code);
}
public function __clone()
{
parent::__clone();
$this->creator = unserialize(serialize($this->creator));
$this->setup = unserialize(serialize($this->setup));
}
}
class_exists(Nette\DI\ServiceDefinition::class);