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