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