Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 /*
|
Chris@14
|
4 * This file is part of the Symfony package.
|
Chris@14
|
5 *
|
Chris@14
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@14
|
7 *
|
Chris@14
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
9 * file that was distributed with this source code.
|
Chris@14
|
10 */
|
Chris@14
|
11
|
Chris@14
|
12 namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
Chris@14
|
13
|
Chris@14
|
14 use Symfony\Component\DependencyInjection\Alias;
|
Chris@14
|
15 use Symfony\Component\DependencyInjection\ChildDefinition;
|
Chris@14
|
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@14
|
17 use Symfony\Component\DependencyInjection\Definition;
|
Chris@14
|
18 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
Chris@14
|
19 use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
Chris@14
|
20
|
Chris@14
|
21 /**
|
Chris@14
|
22 * @author Nicolas Grekas <p@tchwork.com>
|
Chris@14
|
23 *
|
Chris@14
|
24 * @method InstanceofConfigurator instanceof($fqcn)
|
Chris@14
|
25 */
|
Chris@14
|
26 class ServicesConfigurator extends AbstractConfigurator
|
Chris@14
|
27 {
|
Chris@14
|
28 const FACTORY = 'services';
|
Chris@14
|
29
|
Chris@14
|
30 private $defaults;
|
Chris@14
|
31 private $container;
|
Chris@14
|
32 private $loader;
|
Chris@14
|
33 private $instanceof;
|
Chris@14
|
34
|
Chris@14
|
35 public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof)
|
Chris@14
|
36 {
|
Chris@14
|
37 $this->defaults = new Definition();
|
Chris@14
|
38 $this->container = $container;
|
Chris@14
|
39 $this->loader = $loader;
|
Chris@14
|
40 $this->instanceof = &$instanceof;
|
Chris@17
|
41 $instanceof = [];
|
Chris@14
|
42 }
|
Chris@14
|
43
|
Chris@14
|
44 /**
|
Chris@14
|
45 * Defines a set of defaults for following service definitions.
|
Chris@14
|
46 *
|
Chris@14
|
47 * @return DefaultsConfigurator
|
Chris@14
|
48 */
|
Chris@14
|
49 final public function defaults()
|
Chris@14
|
50 {
|
Chris@14
|
51 return new DefaultsConfigurator($this, $this->defaults = new Definition());
|
Chris@14
|
52 }
|
Chris@14
|
53
|
Chris@14
|
54 /**
|
Chris@14
|
55 * Defines an instanceof-conditional to be applied to following service definitions.
|
Chris@14
|
56 *
|
Chris@14
|
57 * @param string $fqcn
|
Chris@14
|
58 *
|
Chris@14
|
59 * @return InstanceofConfigurator
|
Chris@14
|
60 */
|
Chris@14
|
61 final protected function setInstanceof($fqcn)
|
Chris@14
|
62 {
|
Chris@14
|
63 $this->instanceof[$fqcn] = $definition = new ChildDefinition('');
|
Chris@14
|
64
|
Chris@14
|
65 return new InstanceofConfigurator($this, $definition, $fqcn);
|
Chris@14
|
66 }
|
Chris@14
|
67
|
Chris@14
|
68 /**
|
Chris@14
|
69 * Registers a service.
|
Chris@14
|
70 *
|
Chris@14
|
71 * @param string $id
|
Chris@14
|
72 * @param string|null $class
|
Chris@14
|
73 *
|
Chris@14
|
74 * @return ServiceConfigurator
|
Chris@14
|
75 */
|
Chris@14
|
76 final public function set($id, $class = null)
|
Chris@14
|
77 {
|
Chris@14
|
78 $defaults = $this->defaults;
|
Chris@14
|
79 $allowParent = !$defaults->getChanges() && empty($this->instanceof);
|
Chris@14
|
80
|
Chris@14
|
81 $definition = new Definition();
|
Chris@14
|
82 $definition->setPublic($defaults->isPublic());
|
Chris@14
|
83 $definition->setAutowired($defaults->isAutowired());
|
Chris@14
|
84 $definition->setAutoconfigured($defaults->isAutoconfigured());
|
Chris@14
|
85 $definition->setBindings($defaults->getBindings());
|
Chris@17
|
86 $definition->setChanges([]);
|
Chris@14
|
87
|
Chris@14
|
88 $configurator = new ServiceConfigurator($this->container, $this->instanceof, $allowParent, $this, $definition, $id, $defaults->getTags());
|
Chris@14
|
89
|
Chris@14
|
90 return null !== $class ? $configurator->class($class) : $configurator;
|
Chris@14
|
91 }
|
Chris@14
|
92
|
Chris@14
|
93 /**
|
Chris@14
|
94 * Creates an alias.
|
Chris@14
|
95 *
|
Chris@14
|
96 * @param string $id
|
Chris@14
|
97 * @param string $referencedId
|
Chris@14
|
98 *
|
Chris@14
|
99 * @return AliasConfigurator
|
Chris@14
|
100 */
|
Chris@14
|
101 final public function alias($id, $referencedId)
|
Chris@14
|
102 {
|
Chris@14
|
103 $ref = static::processValue($referencedId, true);
|
Chris@14
|
104 $alias = new Alias((string) $ref, $this->defaults->isPublic());
|
Chris@14
|
105 $this->container->setAlias($id, $alias);
|
Chris@14
|
106
|
Chris@14
|
107 return new AliasConfigurator($this, $alias);
|
Chris@14
|
108 }
|
Chris@14
|
109
|
Chris@14
|
110 /**
|
Chris@14
|
111 * Registers a PSR-4 namespace using a glob pattern.
|
Chris@14
|
112 *
|
Chris@14
|
113 * @param string $namespace
|
Chris@14
|
114 * @param string $resource
|
Chris@14
|
115 *
|
Chris@14
|
116 * @return PrototypeConfigurator
|
Chris@14
|
117 */
|
Chris@14
|
118 final public function load($namespace, $resource)
|
Chris@14
|
119 {
|
Chris@14
|
120 $allowParent = !$this->defaults->getChanges() && empty($this->instanceof);
|
Chris@14
|
121
|
Chris@14
|
122 return new PrototypeConfigurator($this, $this->loader, $this->defaults, $namespace, $resource, $allowParent);
|
Chris@14
|
123 }
|
Chris@14
|
124
|
Chris@14
|
125 /**
|
Chris@14
|
126 * Gets an already defined service definition.
|
Chris@14
|
127 *
|
Chris@14
|
128 * @param string $id
|
Chris@14
|
129 *
|
Chris@14
|
130 * @return ServiceConfigurator
|
Chris@14
|
131 *
|
Chris@14
|
132 * @throws ServiceNotFoundException if the service definition does not exist
|
Chris@14
|
133 */
|
Chris@14
|
134 final public function get($id)
|
Chris@14
|
135 {
|
Chris@14
|
136 $allowParent = !$this->defaults->getChanges() && empty($this->instanceof);
|
Chris@14
|
137 $definition = $this->container->getDefinition($id);
|
Chris@14
|
138
|
Chris@17
|
139 return new ServiceConfigurator($this->container, $definition->getInstanceofConditionals(), $allowParent, $this, $definition, $id, []);
|
Chris@14
|
140 }
|
Chris@14
|
141
|
Chris@14
|
142 /**
|
Chris@14
|
143 * Registers a service.
|
Chris@14
|
144 *
|
Chris@14
|
145 * @param string $id
|
Chris@14
|
146 * @param string|null $class
|
Chris@14
|
147 *
|
Chris@14
|
148 * @return ServiceConfigurator
|
Chris@14
|
149 */
|
Chris@14
|
150 final public function __invoke($id, $class = null)
|
Chris@14
|
151 {
|
Chris@14
|
152 return $this->set($id, $class);
|
Chris@14
|
153 }
|
Chris@14
|
154 }
|