-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
ServiceDefinition.php
217 lines (163 loc) 路 4.8 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
<?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-deprecated string|null $class
* @property-deprecated Statement $factory
* @property-deprecated Statement[] $setup
*/
final class ServiceDefinition extends Definition
{
use Nette\SmartObject;
private Statement $creator;
/** @var Statement[] */
private array $setup = [];
public function __construct()
{
$this->creator = new Statement(null);
}
public function getDescriptor(): string
{
$entity = $this->getEntity();
if ($entity && $this->isAnonymous()) {
return 'Service ' . (is_string($entity) ? "of type $entity" : Nette\DI\Helpers::entityToString($entity));
}
return parent::getDescriptor();
}
public function setType(?string $type): static
{
return parent::setType($type);
}
/**
* Alias for setCreator()
*/
public function setFactory(string|array|Definition|Reference|Statement $factory, array $args = []): static
{
return $this->setCreator($factory, $args);
}
/**
* Alias for getCreator()
*/
public function getFactory(): Statement
{
return $this->getCreator();
}
public function setCreator(string|array|Definition|Reference|Statement $creator, array $args = []): static
{
$this->creator = $creator instanceof Statement
? $creator
: new Statement($creator, $args);
return $this;
}
public function getCreator(): Statement
{
return $this->creator;
}
public function getEntity(): string|array|Definition|Reference|null
{
return $this->creator->getEntity();
}
public function setArguments(array $args = []): static
{
$this->creator->arguments = $args;
return $this;
}
public function setArgument($key, $value): static
{
$this->creator->arguments[$key] = $value;
return $this;
}
/**
* @param Statement[] $setup
*/
public function setSetup(array $setup): static
{
foreach ($setup as &$entity) {
if (!$entity instanceof Statement) {
throw new Nette\InvalidArgumentException('Argument must be Nette\DI\Definitions\Statement[].');
}
$entity = $this->prependSelf($entity);
}
$this->setup = $setup;
return $this;
}
/** @return Statement[] */
public function getSetup(): array
{
return $this->setup;
}
public function addSetup(string|array|Definition|Reference|Statement $entity, array $args = []): static
{
$entity = $entity instanceof Statement
? $entity
: new Statement($entity, $args);
$this->setup[] = $this->prependSelf($entity);
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 method.');
}
$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::ThisContainer), 'getService'], [$ref->getValue()]);
}
$this->creator = $resolver->completeStatement($this->creator);
foreach ($this->setup as &$setup) {
$setup = $resolver->completeStatement($setup, true);
}
}
private function prependSelf(Statement $setup): Statement
{
return is_string($setup->getEntity()) && strpbrk($setup->getEntity(), ':@?\\') === false
? new Statement([new Reference(Reference::Self), $setup->getEntity()], $setup->arguments)
: $setup;
}
public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
{
$code = $generator->formatStatement($this->creator) . ";\n";
if (!$this->setup) {
$method->setBody('return ' . $code);
return;
}
$code = '$service = ' . $code;
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));
}
}