Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Definition.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\DependencyInjection;
013
014 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
015 use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
016
017 /**
018 * Definition represents a service definition.
019 *
020 * @author Fabien Potencier <fabien@symfony.com>
021 */
022 class Definition
023 {
024 private $class;
025 private $file;
026 private $factory;
027 private $factoryClass;
028 private $factoryMethod;
029 private $factoryService;
030 private $shared = true;
031 private $deprecated = false;
032 private $deprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
033 private $scope = ContainerInterface::SCOPE_CONTAINER;
034 private $properties = array();
035 private $calls = array();
036 private $configurator;
037 private $tags = array();
038 private $public = true;
039 private $synthetic = false;
040 private $abstract = false;
041 private $synchronized = false;
042 private $lazy = false;
043 private $decoratedService;
044 private $autowired = false;
045 private $autowiringTypes = array();
046
047 protected $arguments;
048
049 /**
050 * @param string|null $class The service class
051 * @param array $arguments An array of arguments to pass to the service constructor
052 */
053 public function __construct($class = null, array $arguments = array())
054 {
055 $this->class = $class;
056 $this->arguments = $arguments;
057 }
058
059 /**
060 * Sets a factory.
061 *
062 * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
063 *
064 * @return Definition The current instance
065 */
066 public function setFactory($factory)
067 {
068 if (is_string($factory) && strpos($factory, '::') !== false) {
069 $factory = explode('::', $factory, 2);
070 }
071
072 $this->factory = $factory;
073
074 return $this;
075 }
076
077 /**
078 * Gets the factory.
079 *
080 * @return string|array The PHP function or an array containing a class/Reference and a method to call
081 */
082 public function getFactory()
083 {
084 return $this->factory;
085 }
086
087 /**
088 * Sets the name of the class that acts as a factory using the factory method,
089 * which will be invoked statically.
090 *
091 * @param string $factoryClass The factory class name
092 *
093 * @return Definition The current instance
094 *
095 * @deprecated since version 2.6, to be removed in 3.0.
096 */
097 public function setFactoryClass($factoryClass)
098 {
099 @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 Definition The current instance
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 Definition The current instance
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 decorates this service.
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 Definition The current instance
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 Definition The current instance
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 Definition The current instance
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 Definition The current instance
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 Definition The current instance
306 *
307 * @throws OutOfBoundsException When the replaced argument does not exist
308 */
309 public function replaceArgument($index, $argument)
310 {
311 if ($index < 0 || $index > count($this->arguments) - 1) {
312 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
313 }
314
315 $this->arguments[$index] = $argument;
316
317 return $this;
318 }
319
320 /**
321 * Gets the arguments to pass to the service constructor/factory method.
322 *
323 * @return array The array of arguments
324 */
325 public function getArguments()
326 {
327 return $this->arguments;
328 }
329
330 /**
331 * Gets an argument to pass to the service constructor/factory method.
332 *
333 * @param int $index
334 *
335 * @return mixed The argument value
336 *
337 * @throws OutOfBoundsException When the argument does not exist
338 */
339 public function getArgument($index)
340 {
341 if ($index < 0 || $index > count($this->arguments) - 1) {
342 throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
343 }
344
345 return $this->arguments[$index];
346 }
347
348 /**
349 * Sets the methods to call after service initialization.
350 *
351 * @param array $calls An array of method calls
352 *
353 * @return Definition The current instance
354 */
355 public function setMethodCalls(array $calls = array())
356 {
357 $this->calls = array();
358 foreach ($calls as $call) {
359 $this->addMethodCall($call[0], $call[1]);
360 }
361
362 return $this;
363 }
364
365 /**
366 * Adds a method to call after service initialization.
367 *
368 * @param string $method The method name to call
369 * @param array $arguments An array of arguments to pass to the method call
370 *
371 * @return Definition The current instance
372 *
373 * @throws InvalidArgumentException on empty $method param
374 */
375 public function addMethodCall($method, array $arguments = array())
376 {
377 if (empty($method)) {
378 throw new InvalidArgumentException(sprintf('Method name cannot be empty.'));
379 }
380 $this->calls[] = array($method, $arguments);
381
382 return $this;
383 }
384
385 /**
386 * Removes a method to call after service initialization.
387 *
388 * @param string $method The method name to remove
389 *
390 * @return Definition The current instance
391 */
392 public function removeMethodCall($method)
393 {
394 foreach ($this->calls as $i => $call) {
395 if ($call[0] === $method) {
396 unset($this->calls[$i]);
397 break;
398 }
399 }
400
401 return $this;
402 }
403
404 /**
405 * Check if the current definition has a given method to call after service initialization.
406 *
407 * @param string $method The method name to search for
408 *
409 * @return bool
410 */
411 public function hasMethodCall($method)
412 {
413 foreach ($this->calls as $call) {
414 if ($call[0] === $method) {
415 return true;
416 }
417 }
418
419 return false;
420 }
421
422 /**
423 * Gets the methods to call after service initialization.
424 *
425 * @return array An array of method calls
426 */
427 public function getMethodCalls()
428 {
429 return $this->calls;
430 }
431
432 /**
433 * Sets tags for this definition.
434 *
435 * @param array $tags
436 *
437 * @return Definition the current instance
438 */
439 public function setTags(array $tags)
440 {
441 $this->tags = $tags;
442
443 return $this;
444 }
445
446 /**
447 * Returns all tags.
448 *
449 * @return array An array of tags
450 */
451 public function getTags()
452 {
453 return $this->tags;
454 }
455
456 /**
457 * Gets a tag by name.
458 *
459 * @param string $name The tag name
460 *
461 * @return array An array of attributes
462 */
463 public function getTag($name)
464 {
465 return isset($this->tags[$name]) ? $this->tags[$name] : array();
466 }
467
468 /**
469 * Adds a tag for this definition.
470 *
471 * @param string $name The tag name
472 * @param array $attributes An array of attributes
473 *
474 * @return Definition The current instance
475 */
476 public function addTag($name, array $attributes = array())
477 {
478 $this->tags[$name][] = $attributes;
479
480 return $this;
481 }
482
483 /**
484 * Whether this definition has a tag with the given name.
485 *
486 * @param string $name
487 *
488 * @return bool
489 */
490 public function hasTag($name)
491 {
492 return isset($this->tags[$name]);
493 }
494
495 /**
496 * Clears all tags for a given name.
497 *
498 * @param string $name The tag name
499 *
500 * @return Definition
501 */
502 public function clearTag($name)
503 {
504 unset($this->tags[$name]);
505
506 return $this;
507 }
508
509 /**
510 * Clears the tags for this definition.
511 *
512 * @return Definition The current instance
513 */
514 public function clearTags()
515 {
516 $this->tags = array();
517
518 return $this;
519 }
520
521 /**
522 * Sets a file to require before creating the service.
523 *
524 * @param string $file A full pathname to include
525 *
526 * @return Definition The current instance
527 */
528 public function setFile($file)
529 {
530 $this->file = $file;
531
532 return $this;
533 }
534
535 /**
536 * Gets the file to require before creating the service.
537 *
538 * @return string|null The full pathname to include
539 */
540 public function getFile()
541 {
542 return $this->file;
543 }
544
545 /**
546 * Sets if the service must be shared or not.
547 *
548 * @param bool $shared Whether the service must be shared or not
549 *
550 * @return Definition The current instance
551 */
552 public function setShared($shared)
553 {
554 $this->shared = (bool) $shared;
555
556 return $this;
557 }
558
559 /**
560 * Whether this service is shared.
561 *
562 * @return bool
563 */
564 public function isShared()
565 {
566 return $this->shared;
567 }
568
569 /**
570 * Sets the scope of the service.
571 *
572 * @param string $scope Whether the service must be shared or not
573 *
574 * @return Definition The current instance
575 *
576 * @deprecated since version 2.8, to be removed in 3.0.
577 */
578 public function setScope($scope, $triggerDeprecationError = true)
579 {
580 if ($triggerDeprecationError) {
581 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
582 }
583
584 if (ContainerInterface::SCOPE_PROTOTYPE === $scope) {
585 $this->setShared(false);
586 }
587
588 $this->scope = $scope;
589
590 return $this;
591 }
592
593 /**
594 * Returns the scope of the service.
595 *
596 * @return string
597 *
598 * @deprecated since version 2.8, to be removed in 3.0.
599 */
600 public function getScope($triggerDeprecationError = true)
601 {
602 if ($triggerDeprecationError) {
603 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
604 }
605
606 return $this->scope;
607 }
608
609 /**
610 * Sets the visibility of this service.
611 *
612 * @param bool $boolean
613 *
614 * @return Definition The current instance
615 */
616 public function setPublic($boolean)
617 {
618 $this->public = (bool) $boolean;
619
620 return $this;
621 }
622
623 /**
624 * Whether this service is public facing.
625 *
626 * @return bool
627 */
628 public function isPublic()
629 {
630 return $this->public;
631 }
632
633 /**
634 * Sets the synchronized flag of this service.
635 *
636 * @param bool $boolean
637 *
638 * @return Definition The current instance
639 *
640 * @deprecated since version 2.7, will be removed in 3.0.
641 */
642 public function setSynchronized($boolean, $triggerDeprecationError = true)
643 {
644 if ($triggerDeprecationError) {
645 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
646 }
647
648 $this->synchronized = (bool) $boolean;
649
650 return $this;
651 }
652
653 /**
654 * Whether this service is synchronized.
655 *
656 * @return bool
657 *
658 * @deprecated since version 2.7, will be removed in 3.0.
659 */
660 public function isSynchronized($triggerDeprecationError = true)
661 {
662 if ($triggerDeprecationError) {
663 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
664 }
665
666 return $this->synchronized;
667 }
668
669 /**
670 * Sets the lazy flag of this service.
671 *
672 * @param bool $lazy
673 *
674 * @return Definition The current instance
675 */
676 public function setLazy($lazy)
677 {
678 $this->lazy = (bool) $lazy;
679
680 return $this;
681 }
682
683 /**
684 * Whether this service is lazy.
685 *
686 * @return bool
687 */
688 public function isLazy()
689 {
690 return $this->lazy;
691 }
692
693 /**
694 * Sets whether this definition is synthetic, that is not constructed by the
695 * container, but dynamically injected.
696 *
697 * @param bool $boolean
698 *
699 * @return Definition the current instance
700 */
701 public function setSynthetic($boolean)
702 {
703 $this->synthetic = (bool) $boolean;
704
705 return $this;
706 }
707
708 /**
709 * Whether this definition is synthetic, that is not constructed by the
710 * container, but dynamically injected.
711 *
712 * @return bool
713 */
714 public function isSynthetic()
715 {
716 return $this->synthetic;
717 }
718
719 /**
720 * Whether this definition is abstract, that means it merely serves as a
721 * template for other definitions.
722 *
723 * @param bool $boolean
724 *
725 * @return Definition the current instance
726 */
727 public function setAbstract($boolean)
728 {
729 $this->abstract = (bool) $boolean;
730
731 return $this;
732 }
733
734 /**
735 * Whether this definition is abstract, that means it merely serves as a
736 * template for other definitions.
737 *
738 * @return bool
739 */
740 public function isAbstract()
741 {
742 return $this->abstract;
743 }
744
745 /**
746 * Whether this definition is deprecated, that means it should not be called
747 * anymore.
748 *
749 * @param bool $status
750 * @param string $template Template message to use if the definition is deprecated
751 *
752 * @return Definition the current instance
753 *
754 * @throws InvalidArgumentException When the message template is invalid.
755 */
756 public function setDeprecated($status = true, $template = null)
757 {
758 if (null !== $template) {
759 if (preg_match('#[\r\n]|\*/#', $template)) {
760 throw new InvalidArgumentException('Invalid characters found in deprecation template.');
761 }
762
763 if (false === strpos($template, '%service_id%')) {
764 throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
765 }
766
767 $this->deprecationTemplate = $template;
768 }
769
770 $this->deprecated = (bool) $status;
771
772 return $this;
773 }
774
775 /**
776 * Whether this definition is deprecated, that means it should not be called
777 * anymore.
778 *
779 * @return bool
780 */
781 public function isDeprecated()
782 {
783 return $this->deprecated;
784 }
785
786 /**
787 * Message to use if this definition is deprecated.
788 *
789 * @param string $id Service id relying on this definition
790 *
791 * @return string
792 */
793 public function getDeprecationMessage($id)
794 {
795 return str_replace('%service_id%', $id, $this->deprecationTemplate);
796 }
797
798 /**
799 * Sets a configurator to call after the service is fully initialized.
800 *
801 * @param callable $callable A PHP callable
802 *
803 * @return Definition The current instance
804 */
805 public function setConfigurator($callable)
806 {
807 $this->configurator = $callable;
808
809 return $this;
810 }
811
812 /**
813 * Gets the configurator to call after the service is fully initialized.
814 *
815 * @return callable|null The PHP callable to call
816 */
817 public function getConfigurator()
818 {
819 return $this->configurator;
820 }
821
822 /**
823 * Sets types that will default to this definition.
824 *
825 * @param string[] $types
826 *
827 * @return Definition The current instance
828 */
829 public function setAutowiringTypes(array $types)
830 {
831 $this->autowiringTypes = array();
832
833 foreach ($types as $type) {
834 $this->autowiringTypes[$type] = true;
835 }
836
837 return $this;
838 }
839
840 /**
841 * Is the definition autowired?
842 *
843 * @return bool
844 */
845 public function isAutowired()
846 {
847 return $this->autowired;
848 }
849
850 /**
851 * Sets autowired.
852 *
853 * @param bool $autowired
854 *
855 * @return Definition The current instance
856 */
857 public function setAutowired($autowired)
858 {
859 $this->autowired = $autowired;
860
861 return $this;
862 }
863
864 /**
865 * Gets autowiring types that will default to this definition.
866 *
867 * @return string[]
868 */
869 public function getAutowiringTypes()
870 {
871 return array_keys($this->autowiringTypes);
872 }
873
874 /**
875 * Adds a type that will default to this definition.
876 *
877 * @param string $type
878 *
879 * @return Definition The current instance
880 */
881 public function addAutowiringType($type)
882 {
883 $this->autowiringTypes[$type] = true;
884
885 return $this;
886 }
887
888 /**
889 * Removes a type.
890 *
891 * @param string $type
892 *
893 * @return Definition The current instance
894 */
895 public function removeAutowiringType($type)
896 {
897 unset($this->autowiringTypes[$type]);
898
899 return $this;
900 }
901
902 /**
903 * Will this definition default for the given type?
904 *
905 * @param string $type
906 *
907 * @return bool
908 */
909 public function hasAutowiringType($type)
910 {
911 return isset($this->autowiringTypes[$type]);
912 }
913 }
914