comparison vendor/symfony/dependency-injection/Loader/Configurator/ServicesConfigurator.php @ 14:1fec387a4317

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