AbstractServiceConfigurator.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  13. abstract class AbstractServiceConfigurator extends AbstractConfigurator
  14. {
  15. protected $parent;
  16. protected $id;
  17. private $defaultTags = array();
  18. public function __construct(ServicesConfigurator $parent, Definition $definition, $id = null, array $defaultTags = array())
  19. {
  20. $this->parent = $parent;
  21. $this->definition = $definition;
  22. $this->id = $id;
  23. $this->defaultTags = $defaultTags;
  24. }
  25. public function __destruct()
  26. {
  27. // default tags should be added last
  28. foreach ($this->defaultTags as $name => $attributes) {
  29. foreach ($attributes as $attributes) {
  30. $this->definition->addTag($name, $attributes);
  31. }
  32. }
  33. $this->defaultTags = array();
  34. }
  35. /**
  36. * Registers a service.
  37. *
  38. * @param string $id
  39. * @param string|null $class
  40. *
  41. * @return ServiceConfigurator
  42. */
  43. final public function set($id, $class = null)
  44. {
  45. $this->__destruct();
  46. return $this->parent->set($id, $class);
  47. }
  48. /**
  49. * Creates an alias.
  50. *
  51. * @param string $id
  52. * @param string $referencedId
  53. *
  54. * @return AliasConfigurator
  55. */
  56. final public function alias($id, $referencedId)
  57. {
  58. $this->__destruct();
  59. return $this->parent->alias($id, $referencedId);
  60. }
  61. /**
  62. * Registers a PSR-4 namespace using a glob pattern.
  63. *
  64. * @param string $namespace
  65. * @param string $resource
  66. *
  67. * @return PrototypeConfigurator
  68. */
  69. final public function load($namespace, $resource)
  70. {
  71. $this->__destruct();
  72. return $this->parent->load($namespace, $resource);
  73. }
  74. /**
  75. * Gets an already defined service definition.
  76. *
  77. * @param string $id
  78. *
  79. * @return ServiceConfigurator
  80. *
  81. * @throws ServiceNotFoundException if the service definition does not exist
  82. */
  83. final public function get($id)
  84. {
  85. $this->__destruct();
  86. return $this->parent->get($id);
  87. }
  88. /**
  89. * Registers a service.
  90. *
  91. * @param string $id
  92. * @param string|null $class
  93. *
  94. * @return ServiceConfigurator
  95. */
  96. final public function __invoke($id, $class = null)
  97. {
  98. $this->__destruct();
  99. return $this->parent->set($id, $class);
  100. }
  101. }