aee2edb81debf8dda388d0312c4bbea606a3ac4c
[yaffs-website] / vendor / symfony / dependency-injection / Definition.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection;
13
14 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
15 use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
16
17 /**
18  * Definition represents a service definition.
19  *
20  * @author Fabien Potencier <fabien@symfony.com>
21  */
22 class Definition
23 {
24     private $class;
25     private $file;
26     private $factory;
27     private $shared = true;
28     private $deprecated = false;
29     private $deprecationTemplate;
30     private $properties = array();
31     private $calls = array();
32     private $configurator;
33     private $tags = array();
34     private $public = true;
35     private $synthetic = false;
36     private $abstract = false;
37     private $lazy = false;
38     private $decoratedService;
39     private $autowired = false;
40     private $autowiringTypes = array();
41
42     private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
43
44     protected $arguments;
45
46     /**
47      * @param string|null $class     The service class
48      * @param array       $arguments An array of arguments to pass to the service constructor
49      */
50     public function __construct($class = null, array $arguments = array())
51     {
52         $this->class = $class;
53         $this->arguments = $arguments;
54     }
55
56     /**
57      * Sets a factory.
58      *
59      * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
60      *
61      * @return $this
62      */
63     public function setFactory($factory)
64     {
65         if (is_string($factory) && strpos($factory, '::') !== false) {
66             $factory = explode('::', $factory, 2);
67         }
68
69         $this->factory = $factory;
70
71         return $this;
72     }
73
74     /**
75      * Gets the factory.
76      *
77      * @return string|array The PHP function or an array containing a class/Reference and a method to call
78      */
79     public function getFactory()
80     {
81         return $this->factory;
82     }
83
84     /**
85      * Sets the service that this service is decorating.
86      *
87      * @param null|string $id        The decorated service id, use null to remove decoration
88      * @param null|string $renamedId The new decorated service id
89      * @param int         $priority  The priority of decoration
90      *
91      * @return $this
92      *
93      * @throws InvalidArgumentException In case the decorated service id and the new decorated service id are equals.
94      */
95     public function setDecoratedService($id, $renamedId = null, $priority = 0)
96     {
97         if ($renamedId && $id == $renamedId) {
98             throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
99         }
100
101         if (null === $id) {
102             $this->decoratedService = null;
103         } else {
104             $this->decoratedService = array($id, $renamedId, (int) $priority);
105         }
106
107         return $this;
108     }
109
110     /**
111      * Gets the service that this service is decorating.
112      *
113      * @return null|array An array composed of the decorated service id, the new id for it and the priority of decoration, null if no service is decorated
114      */
115     public function getDecoratedService()
116     {
117         return $this->decoratedService;
118     }
119
120     /**
121      * Sets the service class.
122      *
123      * @param string $class The service class
124      *
125      * @return $this
126      */
127     public function setClass($class)
128     {
129         $this->class = $class;
130
131         return $this;
132     }
133
134     /**
135      * Gets the service class.
136      *
137      * @return string|null The service class
138      */
139     public function getClass()
140     {
141         return $this->class;
142     }
143
144     /**
145      * Sets the arguments to pass to the service constructor/factory method.
146      *
147      * @param array $arguments An array of arguments
148      *
149      * @return $this
150      */
151     public function setArguments(array $arguments)
152     {
153         $this->arguments = $arguments;
154
155         return $this;
156     }
157
158     public function setProperties(array $properties)
159     {
160         $this->properties = $properties;
161
162         return $this;
163     }
164
165     public function getProperties()
166     {
167         return $this->properties;
168     }
169
170     public function setProperty($name, $value)
171     {
172         $this->properties[$name] = $value;
173
174         return $this;
175     }
176
177     /**
178      * Adds an argument to pass to the service constructor/factory method.
179      *
180      * @param mixed $argument An argument
181      *
182      * @return $this
183      */
184     public function addArgument($argument)
185     {
186         $this->arguments[] = $argument;
187
188         return $this;
189     }
190
191     /**
192      * Sets a specific argument.
193      *
194      * @param int   $index
195      * @param mixed $argument
196      *
197      * @return $this
198      *
199      * @throws OutOfBoundsException When the replaced argument does not exist
200      */
201     public function replaceArgument($index, $argument)
202     {
203         if (0 === count($this->arguments)) {
204             throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
205         }
206
207         if ($index < 0 || $index > count($this->arguments) - 1) {
208             throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
209         }
210
211         $this->arguments[$index] = $argument;
212
213         return $this;
214     }
215
216     /**
217      * Gets the arguments to pass to the service constructor/factory method.
218      *
219      * @return array The array of arguments
220      */
221     public function getArguments()
222     {
223         return $this->arguments;
224     }
225
226     /**
227      * Gets an argument to pass to the service constructor/factory method.
228      *
229      * @param int $index
230      *
231      * @return mixed The argument value
232      *
233      * @throws OutOfBoundsException When the argument does not exist
234      */
235     public function getArgument($index)
236     {
237         if ($index < 0 || $index > count($this->arguments) - 1) {
238             throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
239         }
240
241         return $this->arguments[$index];
242     }
243
244     /**
245      * Sets the methods to call after service initialization.
246      *
247      * @param array $calls An array of method calls
248      *
249      * @return $this
250      */
251     public function setMethodCalls(array $calls = array())
252     {
253         $this->calls = array();
254         foreach ($calls as $call) {
255             $this->addMethodCall($call[0], $call[1]);
256         }
257
258         return $this;
259     }
260
261     /**
262      * Adds a method to call after service initialization.
263      *
264      * @param string $method    The method name to call
265      * @param array  $arguments An array of arguments to pass to the method call
266      *
267      * @return $this
268      *
269      * @throws InvalidArgumentException on empty $method param
270      */
271     public function addMethodCall($method, array $arguments = array())
272     {
273         if (empty($method)) {
274             throw new InvalidArgumentException('Method name cannot be empty.');
275         }
276         $this->calls[] = array($method, $arguments);
277
278         return $this;
279     }
280
281     /**
282      * Removes a method to call after service initialization.
283      *
284      * @param string $method The method name to remove
285      *
286      * @return $this
287      */
288     public function removeMethodCall($method)
289     {
290         foreach ($this->calls as $i => $call) {
291             if ($call[0] === $method) {
292                 unset($this->calls[$i]);
293                 break;
294             }
295         }
296
297         return $this;
298     }
299
300     /**
301      * Check if the current definition has a given method to call after service initialization.
302      *
303      * @param string $method The method name to search for
304      *
305      * @return bool
306      */
307     public function hasMethodCall($method)
308     {
309         foreach ($this->calls as $call) {
310             if ($call[0] === $method) {
311                 return true;
312             }
313         }
314
315         return false;
316     }
317
318     /**
319      * Gets the methods to call after service initialization.
320      *
321      * @return array An array of method calls
322      */
323     public function getMethodCalls()
324     {
325         return $this->calls;
326     }
327
328     /**
329      * Sets tags for this definition.
330      *
331      * @param array $tags
332      *
333      * @return $this
334      */
335     public function setTags(array $tags)
336     {
337         $this->tags = $tags;
338
339         return $this;
340     }
341
342     /**
343      * Returns all tags.
344      *
345      * @return array An array of tags
346      */
347     public function getTags()
348     {
349         return $this->tags;
350     }
351
352     /**
353      * Gets a tag by name.
354      *
355      * @param string $name The tag name
356      *
357      * @return array An array of attributes
358      */
359     public function getTag($name)
360     {
361         return isset($this->tags[$name]) ? $this->tags[$name] : array();
362     }
363
364     /**
365      * Adds a tag for this definition.
366      *
367      * @param string $name       The tag name
368      * @param array  $attributes An array of attributes
369      *
370      * @return $this
371      */
372     public function addTag($name, array $attributes = array())
373     {
374         $this->tags[$name][] = $attributes;
375
376         return $this;
377     }
378
379     /**
380      * Whether this definition has a tag with the given name.
381      *
382      * @param string $name
383      *
384      * @return bool
385      */
386     public function hasTag($name)
387     {
388         return isset($this->tags[$name]);
389     }
390
391     /**
392      * Clears all tags for a given name.
393      *
394      * @param string $name The tag name
395      *
396      * @return $this
397      */
398     public function clearTag($name)
399     {
400         unset($this->tags[$name]);
401
402         return $this;
403     }
404
405     /**
406      * Clears the tags for this definition.
407      *
408      * @return $this
409      */
410     public function clearTags()
411     {
412         $this->tags = array();
413
414         return $this;
415     }
416
417     /**
418      * Sets a file to require before creating the service.
419      *
420      * @param string $file A full pathname to include
421      *
422      * @return $this
423      */
424     public function setFile($file)
425     {
426         $this->file = $file;
427
428         return $this;
429     }
430
431     /**
432      * Gets the file to require before creating the service.
433      *
434      * @return string|null The full pathname to include
435      */
436     public function getFile()
437     {
438         return $this->file;
439     }
440
441     /**
442      * Sets if the service must be shared or not.
443      *
444      * @param bool $shared Whether the service must be shared or not
445      *
446      * @return $this
447      */
448     public function setShared($shared)
449     {
450         $this->shared = (bool) $shared;
451
452         return $this;
453     }
454
455     /**
456      * Whether this service is shared.
457      *
458      * @return bool
459      */
460     public function isShared()
461     {
462         return $this->shared;
463     }
464
465     /**
466      * Sets the visibility of this service.
467      *
468      * @param bool $boolean
469      *
470      * @return $this
471      */
472     public function setPublic($boolean)
473     {
474         $this->public = (bool) $boolean;
475
476         return $this;
477     }
478
479     /**
480      * Whether this service is public facing.
481      *
482      * @return bool
483      */
484     public function isPublic()
485     {
486         return $this->public;
487     }
488
489     /**
490      * Sets the lazy flag of this service.
491      *
492      * @param bool $lazy
493      *
494      * @return $this
495      */
496     public function setLazy($lazy)
497     {
498         $this->lazy = (bool) $lazy;
499
500         return $this;
501     }
502
503     /**
504      * Whether this service is lazy.
505      *
506      * @return bool
507      */
508     public function isLazy()
509     {
510         return $this->lazy;
511     }
512
513     /**
514      * Sets whether this definition is synthetic, that is not constructed by the
515      * container, but dynamically injected.
516      *
517      * @param bool $boolean
518      *
519      * @return $this
520      */
521     public function setSynthetic($boolean)
522     {
523         $this->synthetic = (bool) $boolean;
524
525         return $this;
526     }
527
528     /**
529      * Whether this definition is synthetic, that is not constructed by the
530      * container, but dynamically injected.
531      *
532      * @return bool
533      */
534     public function isSynthetic()
535     {
536         return $this->synthetic;
537     }
538
539     /**
540      * Whether this definition is abstract, that means it merely serves as a
541      * template for other definitions.
542      *
543      * @param bool $boolean
544      *
545      * @return $this
546      */
547     public function setAbstract($boolean)
548     {
549         $this->abstract = (bool) $boolean;
550
551         return $this;
552     }
553
554     /**
555      * Whether this definition is abstract, that means it merely serves as a
556      * template for other definitions.
557      *
558      * @return bool
559      */
560     public function isAbstract()
561     {
562         return $this->abstract;
563     }
564
565     /**
566      * Whether this definition is deprecated, that means it should not be called
567      * anymore.
568      *
569      * @param bool   $status
570      * @param string $template Template message to use if the definition is deprecated
571      *
572      * @return $this
573      *
574      * @throws InvalidArgumentException When the message template is invalid.
575      */
576     public function setDeprecated($status = true, $template = null)
577     {
578         if (null !== $template) {
579             if (preg_match('#[\r\n]|\*/#', $template)) {
580                 throw new InvalidArgumentException('Invalid characters found in deprecation template.');
581             }
582
583             if (false === strpos($template, '%service_id%')) {
584                 throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
585             }
586
587             $this->deprecationTemplate = $template;
588         }
589
590         $this->deprecated = (bool) $status;
591
592         return $this;
593     }
594
595     /**
596      * Whether this definition is deprecated, that means it should not be called
597      * anymore.
598      *
599      * @return bool
600      */
601     public function isDeprecated()
602     {
603         return $this->deprecated;
604     }
605
606     /**
607      * Message to use if this definition is deprecated.
608      *
609      * @param string $id Service id relying on this definition
610      *
611      * @return string
612      */
613     public function getDeprecationMessage($id)
614     {
615         return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
616     }
617
618     /**
619      * Sets a configurator to call after the service is fully initialized.
620      *
621      * @param string|array $configurator A PHP callable
622      *
623      * @return $this
624      */
625     public function setConfigurator($configurator)
626     {
627         if (is_string($configurator) && strpos($configurator, '::') !== false) {
628             $configurator = explode('::', $configurator, 2);
629         }
630
631         $this->configurator = $configurator;
632
633         return $this;
634     }
635
636     /**
637      * Gets the configurator to call after the service is fully initialized.
638      *
639      * @return callable|null The PHP callable to call
640      */
641     public function getConfigurator()
642     {
643         return $this->configurator;
644     }
645
646     /**
647      * Sets types that will default to this definition.
648      *
649      * @param string[] $types
650      *
651      * @return $this
652      */
653     public function setAutowiringTypes(array $types)
654     {
655         $this->autowiringTypes = array();
656
657         foreach ($types as $type) {
658             $this->autowiringTypes[$type] = true;
659         }
660
661         return $this;
662     }
663
664     /**
665      * Is the definition autowired?
666      *
667      * @return bool
668      */
669     public function isAutowired()
670     {
671         return $this->autowired;
672     }
673
674     /**
675      * Sets autowired.
676      *
677      * @param bool $autowired
678      *
679      * @return $this
680      */
681     public function setAutowired($autowired)
682     {
683         $this->autowired = $autowired;
684
685         return $this;
686     }
687
688     /**
689      * Gets autowiring types that will default to this definition.
690      *
691      * @return string[]
692      */
693     public function getAutowiringTypes()
694     {
695         return array_keys($this->autowiringTypes);
696     }
697
698     /**
699      * Adds a type that will default to this definition.
700      *
701      * @param string $type
702      *
703      * @return $this
704      */
705     public function addAutowiringType($type)
706     {
707         $this->autowiringTypes[$type] = true;
708
709         return $this;
710     }
711
712     /**
713      * Removes a type.
714      *
715      * @param string $type
716      *
717      * @return $this
718      */
719     public function removeAutowiringType($type)
720     {
721         unset($this->autowiringTypes[$type]);
722
723         return $this;
724     }
725
726     /**
727      * Will this definition default for the given type?
728      *
729      * @param string $type
730      *
731      * @return bool
732      */
733     public function hasAutowiringType($type)
734     {
735         return isset($this->autowiringTypes[$type]);
736     }
737 }