ServicesConfigurator.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * @autor ThurData <info@thurdata.ch>
  19. *
  20. * @method InstanceofConfigurator instanceof($fqcn)
  21. */
  22. class ServicesConfigurator extends AbstractConfigurator
  23. {
  24. const FACTORY = 'services';
  25. private $defaults;
  26. private $container;
  27. private $loader;
  28. private $instanceof;
  29. public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof)
  30. {
  31. $this->defaults = new Definition();
  32. $this->container = $container;
  33. $this->loader = $loader;
  34. $this->instanceof = &$instanceof;
  35. $instanceof = array();
  36. }
  37. /**
  38. * Defines a set of defaults for following service definitions.
  39. *
  40. * @return DefaultsConfigurator
  41. */
  42. final public function defaults()
  43. {
  44. return new DefaultsConfigurator($this, $this->defaults = new Definition());
  45. }
  46. /**
  47. * Defines an instanceof-conditional to be applied to following service definitions.
  48. *
  49. * @param string $fqcn
  50. *
  51. * @return InstanceofConfigurator
  52. */
  53. final protected function setInstanceof($fqcn)
  54. {
  55. $this->instanceof[$fqcn] = $definition = new ChildDefinition('');
  56. return new InstanceofConfigurator($this, $definition, $fqcn);
  57. }
  58. /**
  59. * Registers a service.
  60. *
  61. * @param string $id
  62. * @param string|null $class
  63. *
  64. * @return ServiceConfigurator
  65. */
  66. final public function set($id, $class = null)
  67. {
  68. $defaults = $this->defaults;
  69. $allowParent = !$defaults->getChanges() && empty($this->instanceof);
  70. $definition = new Definition();
  71. $definition->setPublic($defaults->isPublic());
  72. $definition->setAutowired($defaults->isAutowired());
  73. $definition->setAutoconfigured($defaults->isAutoconfigured());
  74. $definition->setBindings($defaults->getBindings());
  75. $definition->setChanges(array());
  76. $configurator = new ServiceConfigurator($this->container, $this->instanceof, $allowParent, $this, $definition, $id, $defaults->getTags());
  77. return null !== $class ? $configurator->class($class) : $configurator;
  78. }
  79. /**
  80. * Creates an alias.
  81. *
  82. * @param string $id
  83. * @param string $referencedId
  84. *
  85. * @return AliasConfigurator
  86. */
  87. final public function alias($id, $referencedId)
  88. {
  89. $ref = static::processValue($referencedId, true);
  90. $alias = new Alias((string) $ref, $this->defaults->isPublic());
  91. $this->container->setAlias($id, $alias);
  92. return new AliasConfigurator($this, $alias);
  93. }
  94. /**
  95. * Registers a PSR-4 namespace using a glob pattern.
  96. *
  97. * @param string $namespace
  98. * @param string $resource
  99. *
  100. * @return PrototypeConfigurator
  101. */
  102. final public function load($namespace, $resource)
  103. {
  104. $allowParent = !$this->defaults->getChanges() && empty($this->instanceof);
  105. return new PrototypeConfigurator($this, $this->loader, $this->defaults, $namespace, $resource, $allowParent);
  106. }
  107. /**
  108. * Gets an already defined service definition.
  109. *
  110. * @param string $id
  111. *
  112. * @return ServiceConfigurator
  113. *
  114. * @throws ServiceNotFoundException if the service definition does not exist
  115. */
  116. final public function get($id)
  117. {
  118. $allowParent = !$this->defaults->getChanges() && empty($this->instanceof);
  119. $definition = $this->container->getDefinition($id);
  120. return new ServiceConfigurator($this->container, $definition->getInstanceofConditionals(), $allowParent, $this, $definition, $id, array());
  121. }
  122. /**
  123. * Registers a service.
  124. *
  125. * @param string $id
  126. * @param string|null $class
  127. *
  128. * @return ServiceConfigurator
  129. */
  130. final public function __invoke($id, $class = null)
  131. {
  132. return $this->set($id, $class);
  133. }
  134. }