AbstractServiceConfigurator.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = [];
  18. public function __construct(ServicesConfigurator $parent, Definition $definition, string $id = null, array $defaultTags = [])
  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 = [];
  34. }
  35. /**
  36. * Registers a service.
  37. */
  38. final public function set(?string $id, string $class = null): ServiceConfigurator
  39. {
  40. $this->__destruct();
  41. return $this->parent->set($id, $class);
  42. }
  43. /**
  44. * Creates an alias.
  45. */
  46. final public function alias(string $id, string $referencedId): AliasConfigurator
  47. {
  48. $this->__destruct();
  49. return $this->parent->alias($id, $referencedId);
  50. }
  51. /**
  52. * Registers a PSR-4 namespace using a glob pattern.
  53. */
  54. final public function load(string $namespace, string $resource): PrototypeConfigurator
  55. {
  56. $this->__destruct();
  57. return $this->parent->load($namespace, $resource);
  58. }
  59. /**
  60. * Gets an already defined service definition.
  61. *
  62. * @throws ServiceNotFoundException if the service definition does not exist
  63. */
  64. final public function get(string $id): ServiceConfigurator
  65. {
  66. $this->__destruct();
  67. return $this->parent->get($id);
  68. }
  69. /**
  70. * Registers a stack of decorator services.
  71. *
  72. * @param InlineServiceConfigurator[]|ReferenceConfigurator[] $services
  73. */
  74. final public function stack(string $id, array $services): AliasConfigurator
  75. {
  76. $this->__destruct();
  77. return $this->parent->stack($id, $services);
  78. }
  79. /**
  80. * Registers a service.
  81. */
  82. final public function __invoke(string $id, string $class = null): ServiceConfigurator
  83. {
  84. $this->__destruct();
  85. return $this->parent->set($id, $class);
  86. }
  87. }