Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\DependencyInjection;
|
Chris@0
|
13
|
Chris@14
|
14 use Symfony\Component\DependencyInjection\Argument\BoundArgument;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Definition represents a service definition.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 class Definition
|
Chris@0
|
24 {
|
Chris@0
|
25 private $class;
|
Chris@0
|
26 private $file;
|
Chris@0
|
27 private $factory;
|
Chris@0
|
28 private $shared = true;
|
Chris@0
|
29 private $deprecated = false;
|
Chris@12
|
30 private $deprecationTemplate;
|
Chris@17
|
31 private $properties = [];
|
Chris@17
|
32 private $calls = [];
|
Chris@17
|
33 private $instanceof = [];
|
Chris@14
|
34 private $autoconfigured = false;
|
Chris@0
|
35 private $configurator;
|
Chris@17
|
36 private $tags = [];
|
Chris@0
|
37 private $public = true;
|
Chris@14
|
38 private $private = true;
|
Chris@0
|
39 private $synthetic = false;
|
Chris@0
|
40 private $abstract = false;
|
Chris@0
|
41 private $lazy = false;
|
Chris@0
|
42 private $decoratedService;
|
Chris@0
|
43 private $autowired = false;
|
Chris@17
|
44 private $autowiringTypes = [];
|
Chris@17
|
45 private $changes = [];
|
Chris@17
|
46 private $bindings = [];
|
Chris@17
|
47 private $errors = [];
|
Chris@14
|
48
|
Chris@17
|
49 protected $arguments = [];
|
Chris@0
|
50
|
Chris@12
|
51 private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
|
Chris@12
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * @param string|null $class The service class
|
Chris@0
|
55 * @param array $arguments An array of arguments to pass to the service constructor
|
Chris@0
|
56 */
|
Chris@17
|
57 public function __construct($class = null, array $arguments = [])
|
Chris@0
|
58 {
|
Chris@14
|
59 if (null !== $class) {
|
Chris@14
|
60 $this->setClass($class);
|
Chris@14
|
61 }
|
Chris@0
|
62 $this->arguments = $arguments;
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@14
|
66 * Returns all changes tracked for the Definition object.
|
Chris@14
|
67 *
|
Chris@14
|
68 * @return array An array of changes for this Definition
|
Chris@14
|
69 */
|
Chris@14
|
70 public function getChanges()
|
Chris@14
|
71 {
|
Chris@14
|
72 return $this->changes;
|
Chris@14
|
73 }
|
Chris@14
|
74
|
Chris@14
|
75 /**
|
Chris@14
|
76 * Sets the tracked changes for the Definition object.
|
Chris@14
|
77 *
|
Chris@14
|
78 * @param array $changes An array of changes for this Definition
|
Chris@14
|
79 *
|
Chris@14
|
80 * @return $this
|
Chris@14
|
81 */
|
Chris@14
|
82 public function setChanges(array $changes)
|
Chris@14
|
83 {
|
Chris@14
|
84 $this->changes = $changes;
|
Chris@14
|
85
|
Chris@14
|
86 return $this;
|
Chris@14
|
87 }
|
Chris@14
|
88
|
Chris@14
|
89 /**
|
Chris@0
|
90 * Sets a factory.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
|
Chris@0
|
93 *
|
Chris@0
|
94 * @return $this
|
Chris@0
|
95 */
|
Chris@0
|
96 public function setFactory($factory)
|
Chris@0
|
97 {
|
Chris@14
|
98 $this->changes['factory'] = true;
|
Chris@14
|
99
|
Chris@17
|
100 if (\is_string($factory) && false !== strpos($factory, '::')) {
|
Chris@0
|
101 $factory = explode('::', $factory, 2);
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 $this->factory = $factory;
|
Chris@0
|
105
|
Chris@0
|
106 return $this;
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Gets the factory.
|
Chris@0
|
111 *
|
Chris@17
|
112 * @return string|array|null The PHP function or an array containing a class/Reference and a method to call
|
Chris@0
|
113 */
|
Chris@0
|
114 public function getFactory()
|
Chris@0
|
115 {
|
Chris@0
|
116 return $this->factory;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * Sets the service that this service is decorating.
|
Chris@0
|
121 *
|
Chris@17
|
122 * @param string|null $id The decorated service id, use null to remove decoration
|
Chris@17
|
123 * @param string|null $renamedId The new decorated service id
|
Chris@0
|
124 * @param int $priority The priority of decoration
|
Chris@0
|
125 *
|
Chris@0
|
126 * @return $this
|
Chris@0
|
127 *
|
Chris@14
|
128 * @throws InvalidArgumentException in case the decorated service id and the new decorated service id are equals
|
Chris@0
|
129 */
|
Chris@0
|
130 public function setDecoratedService($id, $renamedId = null, $priority = 0)
|
Chris@0
|
131 {
|
Chris@14
|
132 if ($renamedId && $id === $renamedId) {
|
Chris@0
|
133 throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@14
|
136 $this->changes['decorated_service'] = true;
|
Chris@14
|
137
|
Chris@0
|
138 if (null === $id) {
|
Chris@0
|
139 $this->decoratedService = null;
|
Chris@0
|
140 } else {
|
Chris@17
|
141 $this->decoratedService = [$id, $renamedId, (int) $priority];
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 return $this;
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * Gets the service that this service is decorating.
|
Chris@0
|
149 *
|
Chris@17
|
150 * @return array|null An array composed of the decorated service id, the new id for it and the priority of decoration, null if no service is decorated
|
Chris@0
|
151 */
|
Chris@0
|
152 public function getDecoratedService()
|
Chris@0
|
153 {
|
Chris@0
|
154 return $this->decoratedService;
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@0
|
157 /**
|
Chris@0
|
158 * Sets the service class.
|
Chris@0
|
159 *
|
Chris@0
|
160 * @param string $class The service class
|
Chris@0
|
161 *
|
Chris@0
|
162 * @return $this
|
Chris@0
|
163 */
|
Chris@0
|
164 public function setClass($class)
|
Chris@0
|
165 {
|
Chris@14
|
166 $this->changes['class'] = true;
|
Chris@14
|
167
|
Chris@0
|
168 $this->class = $class;
|
Chris@0
|
169
|
Chris@0
|
170 return $this;
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * Gets the service class.
|
Chris@0
|
175 *
|
Chris@0
|
176 * @return string|null The service class
|
Chris@0
|
177 */
|
Chris@0
|
178 public function getClass()
|
Chris@0
|
179 {
|
Chris@0
|
180 return $this->class;
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * Sets the arguments to pass to the service constructor/factory method.
|
Chris@0
|
185 *
|
Chris@0
|
186 * @return $this
|
Chris@0
|
187 */
|
Chris@0
|
188 public function setArguments(array $arguments)
|
Chris@0
|
189 {
|
Chris@0
|
190 $this->arguments = $arguments;
|
Chris@0
|
191
|
Chris@0
|
192 return $this;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@14
|
195 /**
|
Chris@14
|
196 * Sets the properties to define when creating the service.
|
Chris@14
|
197 *
|
Chris@14
|
198 * @return $this
|
Chris@14
|
199 */
|
Chris@0
|
200 public function setProperties(array $properties)
|
Chris@0
|
201 {
|
Chris@0
|
202 $this->properties = $properties;
|
Chris@0
|
203
|
Chris@0
|
204 return $this;
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@14
|
207 /**
|
Chris@14
|
208 * Gets the properties to define when creating the service.
|
Chris@14
|
209 *
|
Chris@14
|
210 * @return array
|
Chris@14
|
211 */
|
Chris@0
|
212 public function getProperties()
|
Chris@0
|
213 {
|
Chris@0
|
214 return $this->properties;
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@14
|
217 /**
|
Chris@14
|
218 * Sets a specific property.
|
Chris@14
|
219 *
|
Chris@14
|
220 * @param string $name
|
Chris@14
|
221 * @param mixed $value
|
Chris@14
|
222 *
|
Chris@14
|
223 * @return $this
|
Chris@14
|
224 */
|
Chris@0
|
225 public function setProperty($name, $value)
|
Chris@0
|
226 {
|
Chris@0
|
227 $this->properties[$name] = $value;
|
Chris@0
|
228
|
Chris@0
|
229 return $this;
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 /**
|
Chris@0
|
233 * Adds an argument to pass to the service constructor/factory method.
|
Chris@0
|
234 *
|
Chris@0
|
235 * @param mixed $argument An argument
|
Chris@0
|
236 *
|
Chris@0
|
237 * @return $this
|
Chris@0
|
238 */
|
Chris@0
|
239 public function addArgument($argument)
|
Chris@0
|
240 {
|
Chris@0
|
241 $this->arguments[] = $argument;
|
Chris@0
|
242
|
Chris@0
|
243 return $this;
|
Chris@0
|
244 }
|
Chris@0
|
245
|
Chris@0
|
246 /**
|
Chris@14
|
247 * Replaces a specific argument.
|
Chris@0
|
248 *
|
Chris@14
|
249 * @param int|string $index
|
Chris@14
|
250 * @param mixed $argument
|
Chris@0
|
251 *
|
Chris@0
|
252 * @return $this
|
Chris@0
|
253 *
|
Chris@0
|
254 * @throws OutOfBoundsException When the replaced argument does not exist
|
Chris@0
|
255 */
|
Chris@0
|
256 public function replaceArgument($index, $argument)
|
Chris@0
|
257 {
|
Chris@17
|
258 if (0 === \count($this->arguments)) {
|
Chris@0
|
259 throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
|
Chris@0
|
260 }
|
Chris@0
|
261
|
Chris@17
|
262 if (\is_int($index) && ($index < 0 || $index > \count($this->arguments) - 1)) {
|
Chris@17
|
263 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, \count($this->arguments) - 1));
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@18
|
266 if (!\array_key_exists($index, $this->arguments)) {
|
Chris@14
|
267 throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
|
Chris@14
|
268 }
|
Chris@14
|
269
|
Chris@0
|
270 $this->arguments[$index] = $argument;
|
Chris@0
|
271
|
Chris@0
|
272 return $this;
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 /**
|
Chris@14
|
276 * Sets a specific argument.
|
Chris@14
|
277 *
|
Chris@14
|
278 * @param int|string $key
|
Chris@14
|
279 * @param mixed $value
|
Chris@14
|
280 *
|
Chris@14
|
281 * @return $this
|
Chris@14
|
282 */
|
Chris@14
|
283 public function setArgument($key, $value)
|
Chris@14
|
284 {
|
Chris@14
|
285 $this->arguments[$key] = $value;
|
Chris@14
|
286
|
Chris@14
|
287 return $this;
|
Chris@14
|
288 }
|
Chris@14
|
289
|
Chris@14
|
290 /**
|
Chris@0
|
291 * Gets the arguments to pass to the service constructor/factory method.
|
Chris@0
|
292 *
|
Chris@0
|
293 * @return array The array of arguments
|
Chris@0
|
294 */
|
Chris@0
|
295 public function getArguments()
|
Chris@0
|
296 {
|
Chris@0
|
297 return $this->arguments;
|
Chris@0
|
298 }
|
Chris@0
|
299
|
Chris@0
|
300 /**
|
Chris@0
|
301 * Gets an argument to pass to the service constructor/factory method.
|
Chris@0
|
302 *
|
Chris@14
|
303 * @param int|string $index
|
Chris@0
|
304 *
|
Chris@0
|
305 * @return mixed The argument value
|
Chris@0
|
306 *
|
Chris@0
|
307 * @throws OutOfBoundsException When the argument does not exist
|
Chris@0
|
308 */
|
Chris@0
|
309 public function getArgument($index)
|
Chris@0
|
310 {
|
Chris@18
|
311 if (!\array_key_exists($index, $this->arguments)) {
|
Chris@14
|
312 throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
|
Chris@0
|
313 }
|
Chris@0
|
314
|
Chris@0
|
315 return $this->arguments[$index];
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 /**
|
Chris@0
|
319 * Sets the methods to call after service initialization.
|
Chris@0
|
320 *
|
Chris@0
|
321 * @return $this
|
Chris@0
|
322 */
|
Chris@17
|
323 public function setMethodCalls(array $calls = [])
|
Chris@0
|
324 {
|
Chris@17
|
325 $this->calls = [];
|
Chris@0
|
326 foreach ($calls as $call) {
|
Chris@0
|
327 $this->addMethodCall($call[0], $call[1]);
|
Chris@0
|
328 }
|
Chris@0
|
329
|
Chris@0
|
330 return $this;
|
Chris@0
|
331 }
|
Chris@0
|
332
|
Chris@0
|
333 /**
|
Chris@0
|
334 * Adds a method to call after service initialization.
|
Chris@0
|
335 *
|
Chris@0
|
336 * @param string $method The method name to call
|
Chris@0
|
337 * @param array $arguments An array of arguments to pass to the method call
|
Chris@0
|
338 *
|
Chris@0
|
339 * @return $this
|
Chris@0
|
340 *
|
Chris@0
|
341 * @throws InvalidArgumentException on empty $method param
|
Chris@0
|
342 */
|
Chris@17
|
343 public function addMethodCall($method, array $arguments = [])
|
Chris@0
|
344 {
|
Chris@0
|
345 if (empty($method)) {
|
Chris@0
|
346 throw new InvalidArgumentException('Method name cannot be empty.');
|
Chris@0
|
347 }
|
Chris@17
|
348 $this->calls[] = [$method, $arguments];
|
Chris@0
|
349
|
Chris@0
|
350 return $this;
|
Chris@0
|
351 }
|
Chris@0
|
352
|
Chris@0
|
353 /**
|
Chris@0
|
354 * Removes a method to call after service initialization.
|
Chris@0
|
355 *
|
Chris@0
|
356 * @param string $method The method name to remove
|
Chris@0
|
357 *
|
Chris@0
|
358 * @return $this
|
Chris@0
|
359 */
|
Chris@0
|
360 public function removeMethodCall($method)
|
Chris@0
|
361 {
|
Chris@0
|
362 foreach ($this->calls as $i => $call) {
|
Chris@0
|
363 if ($call[0] === $method) {
|
Chris@0
|
364 unset($this->calls[$i]);
|
Chris@0
|
365 break;
|
Chris@0
|
366 }
|
Chris@0
|
367 }
|
Chris@0
|
368
|
Chris@0
|
369 return $this;
|
Chris@0
|
370 }
|
Chris@0
|
371
|
Chris@0
|
372 /**
|
Chris@0
|
373 * Check if the current definition has a given method to call after service initialization.
|
Chris@0
|
374 *
|
Chris@0
|
375 * @param string $method The method name to search for
|
Chris@0
|
376 *
|
Chris@0
|
377 * @return bool
|
Chris@0
|
378 */
|
Chris@0
|
379 public function hasMethodCall($method)
|
Chris@0
|
380 {
|
Chris@0
|
381 foreach ($this->calls as $call) {
|
Chris@0
|
382 if ($call[0] === $method) {
|
Chris@0
|
383 return true;
|
Chris@0
|
384 }
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 return false;
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 /**
|
Chris@0
|
391 * Gets the methods to call after service initialization.
|
Chris@0
|
392 *
|
Chris@0
|
393 * @return array An array of method calls
|
Chris@0
|
394 */
|
Chris@0
|
395 public function getMethodCalls()
|
Chris@0
|
396 {
|
Chris@0
|
397 return $this->calls;
|
Chris@0
|
398 }
|
Chris@0
|
399
|
Chris@0
|
400 /**
|
Chris@14
|
401 * Sets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
|
Chris@14
|
402 *
|
Chris@17
|
403 * @param ChildDefinition[] $instanceof
|
Chris@14
|
404 *
|
Chris@14
|
405 * @return $this
|
Chris@14
|
406 */
|
Chris@14
|
407 public function setInstanceofConditionals(array $instanceof)
|
Chris@14
|
408 {
|
Chris@14
|
409 $this->instanceof = $instanceof;
|
Chris@14
|
410
|
Chris@14
|
411 return $this;
|
Chris@14
|
412 }
|
Chris@14
|
413
|
Chris@14
|
414 /**
|
Chris@14
|
415 * Gets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
|
Chris@14
|
416 *
|
Chris@14
|
417 * @return ChildDefinition[]
|
Chris@14
|
418 */
|
Chris@14
|
419 public function getInstanceofConditionals()
|
Chris@14
|
420 {
|
Chris@14
|
421 return $this->instanceof;
|
Chris@14
|
422 }
|
Chris@14
|
423
|
Chris@14
|
424 /**
|
Chris@14
|
425 * Sets whether or not instanceof conditionals should be prepended with a global set.
|
Chris@14
|
426 *
|
Chris@14
|
427 * @param bool $autoconfigured
|
Chris@14
|
428 *
|
Chris@14
|
429 * @return $this
|
Chris@14
|
430 */
|
Chris@14
|
431 public function setAutoconfigured($autoconfigured)
|
Chris@14
|
432 {
|
Chris@14
|
433 $this->changes['autoconfigured'] = true;
|
Chris@14
|
434
|
Chris@14
|
435 $this->autoconfigured = $autoconfigured;
|
Chris@14
|
436
|
Chris@14
|
437 return $this;
|
Chris@14
|
438 }
|
Chris@14
|
439
|
Chris@14
|
440 /**
|
Chris@14
|
441 * @return bool
|
Chris@14
|
442 */
|
Chris@14
|
443 public function isAutoconfigured()
|
Chris@14
|
444 {
|
Chris@14
|
445 return $this->autoconfigured;
|
Chris@14
|
446 }
|
Chris@14
|
447
|
Chris@14
|
448 /**
|
Chris@0
|
449 * Sets tags for this definition.
|
Chris@0
|
450 *
|
Chris@0
|
451 * @return $this
|
Chris@0
|
452 */
|
Chris@0
|
453 public function setTags(array $tags)
|
Chris@0
|
454 {
|
Chris@0
|
455 $this->tags = $tags;
|
Chris@0
|
456
|
Chris@0
|
457 return $this;
|
Chris@0
|
458 }
|
Chris@0
|
459
|
Chris@0
|
460 /**
|
Chris@0
|
461 * Returns all tags.
|
Chris@0
|
462 *
|
Chris@0
|
463 * @return array An array of tags
|
Chris@0
|
464 */
|
Chris@0
|
465 public function getTags()
|
Chris@0
|
466 {
|
Chris@0
|
467 return $this->tags;
|
Chris@0
|
468 }
|
Chris@0
|
469
|
Chris@0
|
470 /**
|
Chris@0
|
471 * Gets a tag by name.
|
Chris@0
|
472 *
|
Chris@0
|
473 * @param string $name The tag name
|
Chris@0
|
474 *
|
Chris@0
|
475 * @return array An array of attributes
|
Chris@0
|
476 */
|
Chris@0
|
477 public function getTag($name)
|
Chris@0
|
478 {
|
Chris@17
|
479 return isset($this->tags[$name]) ? $this->tags[$name] : [];
|
Chris@0
|
480 }
|
Chris@0
|
481
|
Chris@0
|
482 /**
|
Chris@0
|
483 * Adds a tag for this definition.
|
Chris@0
|
484 *
|
Chris@0
|
485 * @param string $name The tag name
|
Chris@0
|
486 * @param array $attributes An array of attributes
|
Chris@0
|
487 *
|
Chris@0
|
488 * @return $this
|
Chris@0
|
489 */
|
Chris@17
|
490 public function addTag($name, array $attributes = [])
|
Chris@0
|
491 {
|
Chris@0
|
492 $this->tags[$name][] = $attributes;
|
Chris@0
|
493
|
Chris@0
|
494 return $this;
|
Chris@0
|
495 }
|
Chris@0
|
496
|
Chris@0
|
497 /**
|
Chris@0
|
498 * Whether this definition has a tag with the given name.
|
Chris@0
|
499 *
|
Chris@0
|
500 * @param string $name
|
Chris@0
|
501 *
|
Chris@0
|
502 * @return bool
|
Chris@0
|
503 */
|
Chris@0
|
504 public function hasTag($name)
|
Chris@0
|
505 {
|
Chris@0
|
506 return isset($this->tags[$name]);
|
Chris@0
|
507 }
|
Chris@0
|
508
|
Chris@0
|
509 /**
|
Chris@0
|
510 * Clears all tags for a given name.
|
Chris@0
|
511 *
|
Chris@0
|
512 * @param string $name The tag name
|
Chris@0
|
513 *
|
Chris@0
|
514 * @return $this
|
Chris@0
|
515 */
|
Chris@0
|
516 public function clearTag($name)
|
Chris@0
|
517 {
|
Chris@0
|
518 unset($this->tags[$name]);
|
Chris@0
|
519
|
Chris@0
|
520 return $this;
|
Chris@0
|
521 }
|
Chris@0
|
522
|
Chris@0
|
523 /**
|
Chris@0
|
524 * Clears the tags for this definition.
|
Chris@0
|
525 *
|
Chris@0
|
526 * @return $this
|
Chris@0
|
527 */
|
Chris@0
|
528 public function clearTags()
|
Chris@0
|
529 {
|
Chris@17
|
530 $this->tags = [];
|
Chris@0
|
531
|
Chris@0
|
532 return $this;
|
Chris@0
|
533 }
|
Chris@0
|
534
|
Chris@0
|
535 /**
|
Chris@0
|
536 * Sets a file to require before creating the service.
|
Chris@0
|
537 *
|
Chris@0
|
538 * @param string $file A full pathname to include
|
Chris@0
|
539 *
|
Chris@0
|
540 * @return $this
|
Chris@0
|
541 */
|
Chris@0
|
542 public function setFile($file)
|
Chris@0
|
543 {
|
Chris@14
|
544 $this->changes['file'] = true;
|
Chris@14
|
545
|
Chris@0
|
546 $this->file = $file;
|
Chris@0
|
547
|
Chris@0
|
548 return $this;
|
Chris@0
|
549 }
|
Chris@0
|
550
|
Chris@0
|
551 /**
|
Chris@0
|
552 * Gets the file to require before creating the service.
|
Chris@0
|
553 *
|
Chris@0
|
554 * @return string|null The full pathname to include
|
Chris@0
|
555 */
|
Chris@0
|
556 public function getFile()
|
Chris@0
|
557 {
|
Chris@0
|
558 return $this->file;
|
Chris@0
|
559 }
|
Chris@0
|
560
|
Chris@0
|
561 /**
|
Chris@0
|
562 * Sets if the service must be shared or not.
|
Chris@0
|
563 *
|
Chris@0
|
564 * @param bool $shared Whether the service must be shared or not
|
Chris@0
|
565 *
|
Chris@0
|
566 * @return $this
|
Chris@0
|
567 */
|
Chris@0
|
568 public function setShared($shared)
|
Chris@0
|
569 {
|
Chris@14
|
570 $this->changes['shared'] = true;
|
Chris@14
|
571
|
Chris@0
|
572 $this->shared = (bool) $shared;
|
Chris@0
|
573
|
Chris@0
|
574 return $this;
|
Chris@0
|
575 }
|
Chris@0
|
576
|
Chris@0
|
577 /**
|
Chris@0
|
578 * Whether this service is shared.
|
Chris@0
|
579 *
|
Chris@0
|
580 * @return bool
|
Chris@0
|
581 */
|
Chris@0
|
582 public function isShared()
|
Chris@0
|
583 {
|
Chris@0
|
584 return $this->shared;
|
Chris@0
|
585 }
|
Chris@0
|
586
|
Chris@0
|
587 /**
|
Chris@0
|
588 * Sets the visibility of this service.
|
Chris@0
|
589 *
|
Chris@0
|
590 * @param bool $boolean
|
Chris@0
|
591 *
|
Chris@0
|
592 * @return $this
|
Chris@0
|
593 */
|
Chris@0
|
594 public function setPublic($boolean)
|
Chris@0
|
595 {
|
Chris@14
|
596 $this->changes['public'] = true;
|
Chris@14
|
597
|
Chris@0
|
598 $this->public = (bool) $boolean;
|
Chris@14
|
599 $this->private = false;
|
Chris@0
|
600
|
Chris@0
|
601 return $this;
|
Chris@0
|
602 }
|
Chris@0
|
603
|
Chris@0
|
604 /**
|
Chris@0
|
605 * Whether this service is public facing.
|
Chris@0
|
606 *
|
Chris@0
|
607 * @return bool
|
Chris@0
|
608 */
|
Chris@0
|
609 public function isPublic()
|
Chris@0
|
610 {
|
Chris@0
|
611 return $this->public;
|
Chris@0
|
612 }
|
Chris@0
|
613
|
Chris@0
|
614 /**
|
Chris@14
|
615 * Sets if this service is private.
|
Chris@14
|
616 *
|
Chris@14
|
617 * When set, the "private" state has a higher precedence than "public".
|
Chris@14
|
618 * In version 3.4, a "private" service always remains publicly accessible,
|
Chris@14
|
619 * but triggers a deprecation notice when accessed from the container,
|
Chris@14
|
620 * so that the service can be made really private in 4.0.
|
Chris@14
|
621 *
|
Chris@14
|
622 * @param bool $boolean
|
Chris@14
|
623 *
|
Chris@14
|
624 * @return $this
|
Chris@14
|
625 */
|
Chris@14
|
626 public function setPrivate($boolean)
|
Chris@14
|
627 {
|
Chris@14
|
628 $this->private = (bool) $boolean;
|
Chris@14
|
629
|
Chris@14
|
630 return $this;
|
Chris@14
|
631 }
|
Chris@14
|
632
|
Chris@14
|
633 /**
|
Chris@14
|
634 * Whether this service is private.
|
Chris@14
|
635 *
|
Chris@14
|
636 * @return bool
|
Chris@14
|
637 */
|
Chris@14
|
638 public function isPrivate()
|
Chris@14
|
639 {
|
Chris@14
|
640 return $this->private;
|
Chris@14
|
641 }
|
Chris@14
|
642
|
Chris@14
|
643 /**
|
Chris@0
|
644 * Sets the lazy flag of this service.
|
Chris@0
|
645 *
|
Chris@0
|
646 * @param bool $lazy
|
Chris@0
|
647 *
|
Chris@0
|
648 * @return $this
|
Chris@0
|
649 */
|
Chris@0
|
650 public function setLazy($lazy)
|
Chris@0
|
651 {
|
Chris@14
|
652 $this->changes['lazy'] = true;
|
Chris@14
|
653
|
Chris@0
|
654 $this->lazy = (bool) $lazy;
|
Chris@0
|
655
|
Chris@0
|
656 return $this;
|
Chris@0
|
657 }
|
Chris@0
|
658
|
Chris@0
|
659 /**
|
Chris@0
|
660 * Whether this service is lazy.
|
Chris@0
|
661 *
|
Chris@0
|
662 * @return bool
|
Chris@0
|
663 */
|
Chris@0
|
664 public function isLazy()
|
Chris@0
|
665 {
|
Chris@0
|
666 return $this->lazy;
|
Chris@0
|
667 }
|
Chris@0
|
668
|
Chris@0
|
669 /**
|
Chris@0
|
670 * Sets whether this definition is synthetic, that is not constructed by the
|
Chris@0
|
671 * container, but dynamically injected.
|
Chris@0
|
672 *
|
Chris@0
|
673 * @param bool $boolean
|
Chris@0
|
674 *
|
Chris@0
|
675 * @return $this
|
Chris@0
|
676 */
|
Chris@0
|
677 public function setSynthetic($boolean)
|
Chris@0
|
678 {
|
Chris@0
|
679 $this->synthetic = (bool) $boolean;
|
Chris@0
|
680
|
Chris@0
|
681 return $this;
|
Chris@0
|
682 }
|
Chris@0
|
683
|
Chris@0
|
684 /**
|
Chris@0
|
685 * Whether this definition is synthetic, that is not constructed by the
|
Chris@0
|
686 * container, but dynamically injected.
|
Chris@0
|
687 *
|
Chris@0
|
688 * @return bool
|
Chris@0
|
689 */
|
Chris@0
|
690 public function isSynthetic()
|
Chris@0
|
691 {
|
Chris@0
|
692 return $this->synthetic;
|
Chris@0
|
693 }
|
Chris@0
|
694
|
Chris@0
|
695 /**
|
Chris@0
|
696 * Whether this definition is abstract, that means it merely serves as a
|
Chris@0
|
697 * template for other definitions.
|
Chris@0
|
698 *
|
Chris@0
|
699 * @param bool $boolean
|
Chris@0
|
700 *
|
Chris@0
|
701 * @return $this
|
Chris@0
|
702 */
|
Chris@0
|
703 public function setAbstract($boolean)
|
Chris@0
|
704 {
|
Chris@0
|
705 $this->abstract = (bool) $boolean;
|
Chris@0
|
706
|
Chris@0
|
707 return $this;
|
Chris@0
|
708 }
|
Chris@0
|
709
|
Chris@0
|
710 /**
|
Chris@0
|
711 * Whether this definition is abstract, that means it merely serves as a
|
Chris@0
|
712 * template for other definitions.
|
Chris@0
|
713 *
|
Chris@0
|
714 * @return bool
|
Chris@0
|
715 */
|
Chris@0
|
716 public function isAbstract()
|
Chris@0
|
717 {
|
Chris@0
|
718 return $this->abstract;
|
Chris@0
|
719 }
|
Chris@0
|
720
|
Chris@0
|
721 /**
|
Chris@0
|
722 * Whether this definition is deprecated, that means it should not be called
|
Chris@0
|
723 * anymore.
|
Chris@0
|
724 *
|
Chris@0
|
725 * @param bool $status
|
Chris@0
|
726 * @param string $template Template message to use if the definition is deprecated
|
Chris@0
|
727 *
|
Chris@0
|
728 * @return $this
|
Chris@0
|
729 *
|
Chris@14
|
730 * @throws InvalidArgumentException when the message template is invalid
|
Chris@0
|
731 */
|
Chris@0
|
732 public function setDeprecated($status = true, $template = null)
|
Chris@0
|
733 {
|
Chris@0
|
734 if (null !== $template) {
|
Chris@0
|
735 if (preg_match('#[\r\n]|\*/#', $template)) {
|
Chris@0
|
736 throw new InvalidArgumentException('Invalid characters found in deprecation template.');
|
Chris@0
|
737 }
|
Chris@0
|
738
|
Chris@0
|
739 if (false === strpos($template, '%service_id%')) {
|
Chris@0
|
740 throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
|
Chris@0
|
741 }
|
Chris@0
|
742
|
Chris@0
|
743 $this->deprecationTemplate = $template;
|
Chris@0
|
744 }
|
Chris@0
|
745
|
Chris@14
|
746 $this->changes['deprecated'] = true;
|
Chris@14
|
747
|
Chris@0
|
748 $this->deprecated = (bool) $status;
|
Chris@0
|
749
|
Chris@0
|
750 return $this;
|
Chris@0
|
751 }
|
Chris@0
|
752
|
Chris@0
|
753 /**
|
Chris@0
|
754 * Whether this definition is deprecated, that means it should not be called
|
Chris@0
|
755 * anymore.
|
Chris@0
|
756 *
|
Chris@0
|
757 * @return bool
|
Chris@0
|
758 */
|
Chris@0
|
759 public function isDeprecated()
|
Chris@0
|
760 {
|
Chris@0
|
761 return $this->deprecated;
|
Chris@0
|
762 }
|
Chris@0
|
763
|
Chris@0
|
764 /**
|
Chris@0
|
765 * Message to use if this definition is deprecated.
|
Chris@0
|
766 *
|
Chris@0
|
767 * @param string $id Service id relying on this definition
|
Chris@0
|
768 *
|
Chris@0
|
769 * @return string
|
Chris@0
|
770 */
|
Chris@0
|
771 public function getDeprecationMessage($id)
|
Chris@0
|
772 {
|
Chris@12
|
773 return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
|
Chris@0
|
774 }
|
Chris@0
|
775
|
Chris@0
|
776 /**
|
Chris@0
|
777 * Sets a configurator to call after the service is fully initialized.
|
Chris@0
|
778 *
|
Chris@0
|
779 * @param string|array $configurator A PHP callable
|
Chris@0
|
780 *
|
Chris@0
|
781 * @return $this
|
Chris@0
|
782 */
|
Chris@0
|
783 public function setConfigurator($configurator)
|
Chris@0
|
784 {
|
Chris@14
|
785 $this->changes['configurator'] = true;
|
Chris@14
|
786
|
Chris@17
|
787 if (\is_string($configurator) && false !== strpos($configurator, '::')) {
|
Chris@0
|
788 $configurator = explode('::', $configurator, 2);
|
Chris@0
|
789 }
|
Chris@0
|
790
|
Chris@0
|
791 $this->configurator = $configurator;
|
Chris@0
|
792
|
Chris@0
|
793 return $this;
|
Chris@0
|
794 }
|
Chris@0
|
795
|
Chris@0
|
796 /**
|
Chris@0
|
797 * Gets the configurator to call after the service is fully initialized.
|
Chris@0
|
798 *
|
Chris@0
|
799 * @return callable|null The PHP callable to call
|
Chris@0
|
800 */
|
Chris@0
|
801 public function getConfigurator()
|
Chris@0
|
802 {
|
Chris@0
|
803 return $this->configurator;
|
Chris@0
|
804 }
|
Chris@0
|
805
|
Chris@0
|
806 /**
|
Chris@0
|
807 * Sets types that will default to this definition.
|
Chris@0
|
808 *
|
Chris@0
|
809 * @param string[] $types
|
Chris@0
|
810 *
|
Chris@0
|
811 * @return $this
|
Chris@14
|
812 *
|
Chris@14
|
813 * @deprecated since version 3.3, to be removed in 4.0.
|
Chris@0
|
814 */
|
Chris@0
|
815 public function setAutowiringTypes(array $types)
|
Chris@0
|
816 {
|
Chris@14
|
817 @trigger_error('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead.', E_USER_DEPRECATED);
|
Chris@14
|
818
|
Chris@17
|
819 $this->autowiringTypes = [];
|
Chris@0
|
820
|
Chris@0
|
821 foreach ($types as $type) {
|
Chris@0
|
822 $this->autowiringTypes[$type] = true;
|
Chris@0
|
823 }
|
Chris@0
|
824
|
Chris@0
|
825 return $this;
|
Chris@0
|
826 }
|
Chris@0
|
827
|
Chris@0
|
828 /**
|
Chris@0
|
829 * Is the definition autowired?
|
Chris@0
|
830 *
|
Chris@0
|
831 * @return bool
|
Chris@0
|
832 */
|
Chris@0
|
833 public function isAutowired()
|
Chris@0
|
834 {
|
Chris@0
|
835 return $this->autowired;
|
Chris@0
|
836 }
|
Chris@0
|
837
|
Chris@0
|
838 /**
|
Chris@14
|
839 * Enables/disables autowiring.
|
Chris@0
|
840 *
|
Chris@0
|
841 * @param bool $autowired
|
Chris@0
|
842 *
|
Chris@0
|
843 * @return $this
|
Chris@0
|
844 */
|
Chris@0
|
845 public function setAutowired($autowired)
|
Chris@0
|
846 {
|
Chris@14
|
847 $this->changes['autowired'] = true;
|
Chris@14
|
848
|
Chris@14
|
849 $this->autowired = (bool) $autowired;
|
Chris@0
|
850
|
Chris@0
|
851 return $this;
|
Chris@0
|
852 }
|
Chris@0
|
853
|
Chris@0
|
854 /**
|
Chris@0
|
855 * Gets autowiring types that will default to this definition.
|
Chris@0
|
856 *
|
Chris@0
|
857 * @return string[]
|
Chris@14
|
858 *
|
Chris@14
|
859 * @deprecated since version 3.3, to be removed in 4.0.
|
Chris@0
|
860 */
|
Chris@14
|
861 public function getAutowiringTypes(/*$triggerDeprecation = true*/)
|
Chris@0
|
862 {
|
Chris@17
|
863 if (1 > \func_num_args() || func_get_arg(0)) {
|
Chris@14
|
864 @trigger_error('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead.', E_USER_DEPRECATED);
|
Chris@14
|
865 }
|
Chris@14
|
866
|
Chris@0
|
867 return array_keys($this->autowiringTypes);
|
Chris@0
|
868 }
|
Chris@0
|
869
|
Chris@0
|
870 /**
|
Chris@0
|
871 * Adds a type that will default to this definition.
|
Chris@0
|
872 *
|
Chris@0
|
873 * @param string $type
|
Chris@0
|
874 *
|
Chris@0
|
875 * @return $this
|
Chris@14
|
876 *
|
Chris@14
|
877 * @deprecated since version 3.3, to be removed in 4.0.
|
Chris@0
|
878 */
|
Chris@0
|
879 public function addAutowiringType($type)
|
Chris@0
|
880 {
|
Chris@14
|
881 @trigger_error(sprintf('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead for "%s".', $type), E_USER_DEPRECATED);
|
Chris@14
|
882
|
Chris@0
|
883 $this->autowiringTypes[$type] = true;
|
Chris@0
|
884
|
Chris@0
|
885 return $this;
|
Chris@0
|
886 }
|
Chris@0
|
887
|
Chris@0
|
888 /**
|
Chris@0
|
889 * Removes a type.
|
Chris@0
|
890 *
|
Chris@0
|
891 * @param string $type
|
Chris@0
|
892 *
|
Chris@0
|
893 * @return $this
|
Chris@14
|
894 *
|
Chris@14
|
895 * @deprecated since version 3.3, to be removed in 4.0.
|
Chris@0
|
896 */
|
Chris@0
|
897 public function removeAutowiringType($type)
|
Chris@0
|
898 {
|
Chris@14
|
899 @trigger_error(sprintf('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead for "%s".', $type), E_USER_DEPRECATED);
|
Chris@14
|
900
|
Chris@0
|
901 unset($this->autowiringTypes[$type]);
|
Chris@0
|
902
|
Chris@0
|
903 return $this;
|
Chris@0
|
904 }
|
Chris@0
|
905
|
Chris@0
|
906 /**
|
Chris@0
|
907 * Will this definition default for the given type?
|
Chris@0
|
908 *
|
Chris@0
|
909 * @param string $type
|
Chris@0
|
910 *
|
Chris@0
|
911 * @return bool
|
Chris@14
|
912 *
|
Chris@14
|
913 * @deprecated since version 3.3, to be removed in 4.0.
|
Chris@0
|
914 */
|
Chris@0
|
915 public function hasAutowiringType($type)
|
Chris@0
|
916 {
|
Chris@14
|
917 @trigger_error(sprintf('Autowiring-types are deprecated since Symfony 3.3 and will be removed in 4.0. Use aliases instead for "%s".', $type), E_USER_DEPRECATED);
|
Chris@14
|
918
|
Chris@0
|
919 return isset($this->autowiringTypes[$type]);
|
Chris@0
|
920 }
|
Chris@14
|
921
|
Chris@14
|
922 /**
|
Chris@14
|
923 * Gets bindings.
|
Chris@14
|
924 *
|
Chris@14
|
925 * @return array
|
Chris@14
|
926 */
|
Chris@14
|
927 public function getBindings()
|
Chris@14
|
928 {
|
Chris@14
|
929 return $this->bindings;
|
Chris@14
|
930 }
|
Chris@14
|
931
|
Chris@14
|
932 /**
|
Chris@14
|
933 * Sets bindings.
|
Chris@14
|
934 *
|
Chris@14
|
935 * Bindings map $named or FQCN arguments to values that should be
|
Chris@14
|
936 * injected in the matching parameters (of the constructor, of methods
|
Chris@14
|
937 * called and of controller actions).
|
Chris@14
|
938 *
|
Chris@14
|
939 * @param array $bindings
|
Chris@14
|
940 *
|
Chris@14
|
941 * @return $this
|
Chris@14
|
942 */
|
Chris@14
|
943 public function setBindings(array $bindings)
|
Chris@14
|
944 {
|
Chris@14
|
945 foreach ($bindings as $key => $binding) {
|
Chris@14
|
946 if (!$binding instanceof BoundArgument) {
|
Chris@14
|
947 $bindings[$key] = new BoundArgument($binding);
|
Chris@14
|
948 }
|
Chris@14
|
949 }
|
Chris@14
|
950
|
Chris@14
|
951 $this->bindings = $bindings;
|
Chris@14
|
952
|
Chris@14
|
953 return $this;
|
Chris@14
|
954 }
|
Chris@14
|
955
|
Chris@14
|
956 /**
|
Chris@14
|
957 * Add an error that occurred when building this Definition.
|
Chris@14
|
958 *
|
Chris@14
|
959 * @param string $error
|
Chris@14
|
960 */
|
Chris@14
|
961 public function addError($error)
|
Chris@14
|
962 {
|
Chris@14
|
963 $this->errors[] = $error;
|
Chris@14
|
964 }
|
Chris@14
|
965
|
Chris@14
|
966 /**
|
Chris@14
|
967 * Returns any errors that occurred while building this Definition.
|
Chris@14
|
968 *
|
Chris@14
|
969 * @return array
|
Chris@14
|
970 */
|
Chris@14
|
971 public function getErrors()
|
Chris@14
|
972 {
|
Chris@14
|
973 return $this->errors;
|
Chris@14
|
974 }
|
Chris@0
|
975 }
|