ServicesConfigurator.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\InvalidArgumentException;
  16. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  17. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  18. /**
  19. * @author Nicolas Grekas <p@tchwork.com>
  20. */
  21. class ServicesConfigurator extends AbstractConfigurator
  22. {
  23. public const FACTORY = 'services';
  24. private $defaults;
  25. private $container;
  26. private $loader;
  27. private $instanceof;
  28. private $path;
  29. private $anonymousHash;
  30. private $anonymousCount;
  31. public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path = null, int &$anonymousCount = 0)
  32. {
  33. $this->defaults = new Definition();
  34. $this->container = $container;
  35. $this->loader = $loader;
  36. $this->instanceof = &$instanceof;
  37. $this->path = $path;
  38. $this->anonymousHash = ContainerBuilder::hash($path ?: mt_rand());
  39. $this->anonymousCount = &$anonymousCount;
  40. $instanceof = [];
  41. }
  42. /**
  43. * Defines a set of defaults for following service definitions.
  44. */
  45. final public function defaults(): DefaultsConfigurator
  46. {
  47. return new DefaultsConfigurator($this, $this->defaults = new Definition(), $this->path);
  48. }
  49. /**
  50. * Defines an instanceof-conditional to be applied to following service definitions.
  51. */
  52. final public function instanceof(string $fqcn): InstanceofConfigurator
  53. {
  54. $this->instanceof[$fqcn] = $definition = new ChildDefinition('');
  55. return new InstanceofConfigurator($this, $definition, $fqcn, $this->path);
  56. }
  57. /**
  58. * Registers a service.
  59. *
  60. * @param string|null $id The service id, or null to create an anonymous service
  61. * @param string|null $class The class of the service, or null when $id is also the class name
  62. */
  63. final public function set(?string $id, string $class = null): ServiceConfigurator
  64. {
  65. $defaults = $this->defaults;
  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. } elseif (!$defaults->isPublic() || !$defaults->isPrivate()) {
  73. $definition->setPublic($defaults->isPublic() && !$defaults->isPrivate());
  74. }
  75. $definition->setAutowired($defaults->isAutowired());
  76. $definition->setAutoconfigured($defaults->isAutoconfigured());
  77. // deep clone, to avoid multiple process of the same instance in the passes
  78. $definition->setBindings(unserialize(serialize($defaults->getBindings())));
  79. $definition->setChanges([]);
  80. $configurator = new ServiceConfigurator($this->container, $this->instanceof, true, $this, $definition, $id, $defaults->getTags(), $this->path);
  81. return null !== $class ? $configurator->class($class) : $configurator;
  82. }
  83. /**
  84. * Creates an alias.
  85. */
  86. final public function alias(string $id, string $referencedId): AliasConfigurator
  87. {
  88. $ref = static::processValue($referencedId, true);
  89. $alias = new Alias((string) $ref);
  90. if (!$this->defaults->isPublic() || !$this->defaults->isPrivate()) {
  91. $alias->setPublic($this->defaults->isPublic());
  92. }
  93. $this->container->setAlias($id, $alias);
  94. return new AliasConfigurator($this, $alias);
  95. }
  96. /**
  97. * Registers a PSR-4 namespace using a glob pattern.
  98. */
  99. final public function load(string $namespace, string $resource): PrototypeConfigurator
  100. {
  101. return new PrototypeConfigurator($this, $this->loader, $this->defaults, $namespace, $resource, true);
  102. }
  103. /**
  104. * Gets an already defined service definition.
  105. *
  106. * @throws ServiceNotFoundException if the service definition does not exist
  107. */
  108. final public function get(string $id): ServiceConfigurator
  109. {
  110. $definition = $this->container->getDefinition($id);
  111. return new ServiceConfigurator($this->container, $definition->getInstanceofConditionals(), true, $this, $definition, $id, []);
  112. }
  113. /**
  114. * Registers a stack of decorator services.
  115. *
  116. * @param InlineServiceConfigurator[]|ReferenceConfigurator[] $services
  117. */
  118. final public function stack(string $id, array $services): AliasConfigurator
  119. {
  120. foreach ($services as $i => $service) {
  121. if ($service instanceof InlineServiceConfigurator) {
  122. $definition = $service->definition->setInstanceofConditionals($this->instanceof);
  123. $changes = $definition->getChanges();
  124. $definition->setAutowired((isset($changes['autowired']) ? $definition : $this->defaults)->isAutowired());
  125. $definition->setAutoconfigured((isset($changes['autoconfigured']) ? $definition : $this->defaults)->isAutoconfigured());
  126. $definition->setBindings(array_merge($this->defaults->getBindings(), $definition->getBindings()));
  127. $definition->setChanges($changes);
  128. $services[$i] = $definition;
  129. } elseif (!$service instanceof ReferenceConfigurator) {
  130. throw new InvalidArgumentException(sprintf('"%s()" expects a list of definitions as returned by "%s()" or "%s()", "%s" given at index "%s" for service "%s".', __METHOD__, InlineServiceConfigurator::FACTORY, ReferenceConfigurator::FACTORY, $service instanceof AbstractConfigurator ? $service::FACTORY.'()' : get_debug_type($service), $i, $id));
  131. }
  132. }
  133. $alias = $this->alias($id, '');
  134. $alias->definition = $this->set($id)
  135. ->parent('')
  136. ->args($services)
  137. ->tag('container.stack')
  138. ->definition;
  139. return $alias;
  140. }
  141. /**
  142. * Registers a service.
  143. */
  144. final public function __invoke(string $id, string $class = null): ServiceConfigurator
  145. {
  146. return $this->set($id, $class);
  147. }
  148. public function __destruct()
  149. {
  150. $this->loader->registerAliasesForSinglyImplementedInterfaces();
  151. }
  152. }