ServicesConfigurator.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Alias;
  12. use Symfony\Component\DependencyInjection\ChildDefinition;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  16. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  17. /**
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. class ServicesConfigurator extends AbstractConfigurator
  21. {
  22. const FACTORY = 'services';
  23. private $defaults;
  24. private $container;
  25. private $loader;
  26. private $instanceof;
  27. private $path;
  28. private $anonymousHash;
  29. private $anonymousCount;
  30. public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path = null, int &$anonymousCount = 0)
  31. {
  32. $this->defaults = new Definition();
  33. $this->container = $container;
  34. $this->loader = $loader;
  35. $this->instanceof = &$instanceof;
  36. $this->path = $path;
  37. $this->anonymousHash = ContainerBuilder::hash($path ?: mt_rand());
  38. $this->anonymousCount = &$anonymousCount;
  39. $instanceof = [];
  40. }
  41. /**
  42. * Defines a set of defaults for following service definitions.
  43. */
  44. final public function defaults(): DefaultsConfigurator
  45. {
  46. return new DefaultsConfigurator($this, $this->defaults = new Definition(), $this->path);
  47. }
  48. /**
  49. * Defines an instanceof-conditional to be applied to following service definitions.
  50. */
  51. final public function instanceof(string $fqcn): InstanceofConfigurator
  52. {
  53. $this->instanceof[$fqcn] = $definition = new ChildDefinition('');
  54. return new InstanceofConfigurator($this, $definition, $fqcn, $this->path);
  55. }
  56. /**
  57. * Registers a service.
  58. *
  59. * @param string|null $id The service id, or null to create an anonymous service
  60. * @param string|null $class The class of the service, or null when $id is also the class name
  61. */
  62. final public function set(?string $id, string $class = null): ServiceConfigurator
  63. {
  64. $defaults = $this->defaults;
  65. $allowParent = !$defaults->getChanges() && empty($this->instanceof);
  66. $definition = new Definition();
  67. if (null === $id) {
  68. if (!$class) {
  69. throw new \LogicException('Anonymous services must have a class name.');
  70. }
  71. $id = sprintf('.%d_%s', ++$this->anonymousCount, preg_replace('/^.*\\\\/', '', $class).'~'.$this->anonymousHash);
  72. $definition->setPublic(false);
  73. } elseif (!$defaults->isPublic() || !$defaults->isPrivate()) {
  74. $definition->setPublic($defaults->isPublic() && !$defaults->isPrivate());
  75. }
  76. $definition->setAutowired($defaults->isAutowired());
  77. $definition->setAutoconfigured($defaults->isAutoconfigured());
  78. // deep clone, to avoid multiple process of the same instance in the passes
  79. $definition->setBindings(unserialize(serialize($defaults->getBindings())));
  80. $definition->setChanges([]);
  81. $configurator = new ServiceConfigurator($this->container, $this->instanceof, $allowParent, $this, $definition, $id, $defaults->getTags(), $this->path);
  82. return null !== $class ? $configurator->class($class) : $configurator;
  83. }
  84. /**
  85. * Creates an alias.
  86. */
  87. final public function alias(string $id, string $referencedId): AliasConfigurator
  88. {
  89. $ref = static::processValue($referencedId, true);
  90. $alias = new Alias((string) $ref);
  91. if (!$this->defaults->isPublic() || !$this->defaults->isPrivate()) {
  92. $alias->setPublic($this->defaults->isPublic());
  93. }
  94. $this->container->setAlias($id, $alias);
  95. return new AliasConfigurator($this, $alias);
  96. }
  97. /**
  98. * Registers a PSR-4 namespace using a glob pattern.
  99. */
  100. final public function load(string $namespace, string $resource): PrototypeConfigurator
  101. {
  102. $allowParent = !$this->defaults->getChanges() && empty($this->instanceof);
  103. return new PrototypeConfigurator($this, $this->loader, $this->defaults, $namespace, $resource, $allowParent);
  104. }
  105. /**
  106. * Gets an already defined service definition.
  107. *
  108. * @throws ServiceNotFoundException if the service definition does not exist
  109. */
  110. final public function get(string $id): ServiceConfigurator
  111. {
  112. $allowParent = !$this->defaults->getChanges() && empty($this->instanceof);
  113. $definition = $this->container->getDefinition($id);
  114. return new ServiceConfigurator($this->container, $definition->getInstanceofConditionals(), $allowParent, $this, $definition, $id, []);
  115. }
  116. /**
  117. * Registers a service.
  118. */
  119. final public function __invoke(string $id, string $class = null): ServiceConfigurator
  120. {
  121. return $this->set($id, $class);
  122. }
  123. public function __destruct()
  124. {
  125. $this->loader->registerAliasesForSinglyImplementedInterfaces();
  126. }
  127. }