Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\DependencyInjection;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
Chris@0
|
17 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
Chris@0
|
18 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
Chris@0
|
19 use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
|
Chris@0
|
20 use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Container is a dependency injection container.
|
Chris@0
|
24 *
|
Chris@0
|
25 * It gives access to object instances (services).
|
Chris@0
|
26 *
|
Chris@0
|
27 * Services and parameters are simple key/pair stores.
|
Chris@0
|
28 *
|
Chris@0
|
29 * Parameter and service keys are case insensitive.
|
Chris@0
|
30 *
|
Chris@0
|
31 * A service can also be defined by creating a method named
|
Chris@0
|
32 * getXXXService(), where XXX is the camelized version of the id:
|
Chris@0
|
33 *
|
Chris@0
|
34 * <ul>
|
Chris@0
|
35 * <li>request -> getRequestService()</li>
|
Chris@0
|
36 * <li>mysql_session_storage -> getMysqlSessionStorageService()</li>
|
Chris@0
|
37 * <li>symfony.mysql_session_storage -> getSymfony_MysqlSessionStorageService()</li>
|
Chris@0
|
38 * </ul>
|
Chris@0
|
39 *
|
Chris@0
|
40 * The container can have three possible behaviors when a service does not exist:
|
Chris@0
|
41 *
|
Chris@0
|
42 * * EXCEPTION_ON_INVALID_REFERENCE: Throws an exception (the default)
|
Chris@0
|
43 * * NULL_ON_INVALID_REFERENCE: Returns null
|
Chris@0
|
44 * * IGNORE_ON_INVALID_REFERENCE: Ignores the wrapping command asking for the reference
|
Chris@0
|
45 * (for instance, ignore a setter if the service does not exist)
|
Chris@0
|
46 *
|
Chris@0
|
47 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
48 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
Chris@0
|
49 */
|
Chris@0
|
50 class Container implements ResettableContainerInterface
|
Chris@0
|
51 {
|
Chris@0
|
52 /**
|
Chris@0
|
53 * @var ParameterBagInterface
|
Chris@0
|
54 */
|
Chris@0
|
55 protected $parameterBag;
|
Chris@0
|
56
|
Chris@0
|
57 protected $services = array();
|
Chris@0
|
58 protected $methodMap = array();
|
Chris@0
|
59 protected $aliases = array();
|
Chris@0
|
60 protected $loading = array();
|
Chris@0
|
61
|
Chris@12
|
62 /**
|
Chris@12
|
63 * @internal
|
Chris@12
|
64 */
|
Chris@12
|
65 protected $privates = array();
|
Chris@12
|
66
|
Chris@0
|
67 private $underscoreMap = array('_' => '', '.' => '_', '\\' => '_');
|
Chris@0
|
68 private $envCache = array();
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
|
Chris@0
|
72 */
|
Chris@0
|
73 public function __construct(ParameterBagInterface $parameterBag = null)
|
Chris@0
|
74 {
|
Chris@0
|
75 $this->parameterBag = $parameterBag ?: new EnvPlaceholderParameterBag();
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * Compiles the container.
|
Chris@0
|
80 *
|
Chris@0
|
81 * This method does two things:
|
Chris@0
|
82 *
|
Chris@0
|
83 * * Parameter values are resolved;
|
Chris@0
|
84 * * The parameter bag is frozen.
|
Chris@0
|
85 */
|
Chris@0
|
86 public function compile()
|
Chris@0
|
87 {
|
Chris@0
|
88 $this->parameterBag->resolve();
|
Chris@0
|
89
|
Chris@0
|
90 $this->parameterBag = new FrozenParameterBag($this->parameterBag->all());
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Returns true if the container parameter bag are frozen.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @return bool true if the container parameter bag are frozen, false otherwise
|
Chris@0
|
97 */
|
Chris@0
|
98 public function isFrozen()
|
Chris@0
|
99 {
|
Chris@0
|
100 return $this->parameterBag instanceof FrozenParameterBag;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Gets the service container parameter bag.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @return ParameterBagInterface A ParameterBagInterface instance
|
Chris@0
|
107 */
|
Chris@0
|
108 public function getParameterBag()
|
Chris@0
|
109 {
|
Chris@0
|
110 return $this->parameterBag;
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 /**
|
Chris@0
|
114 * Gets a parameter.
|
Chris@0
|
115 *
|
Chris@0
|
116 * @param string $name The parameter name
|
Chris@0
|
117 *
|
Chris@0
|
118 * @return mixed The parameter value
|
Chris@0
|
119 *
|
Chris@0
|
120 * @throws InvalidArgumentException if the parameter is not defined
|
Chris@0
|
121 */
|
Chris@0
|
122 public function getParameter($name)
|
Chris@0
|
123 {
|
Chris@0
|
124 return $this->parameterBag->get($name);
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * Checks if a parameter exists.
|
Chris@0
|
129 *
|
Chris@0
|
130 * @param string $name The parameter name
|
Chris@0
|
131 *
|
Chris@0
|
132 * @return bool The presence of parameter in container
|
Chris@0
|
133 */
|
Chris@0
|
134 public function hasParameter($name)
|
Chris@0
|
135 {
|
Chris@0
|
136 return $this->parameterBag->has($name);
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * Sets a parameter.
|
Chris@0
|
141 *
|
Chris@0
|
142 * @param string $name The parameter name
|
Chris@0
|
143 * @param mixed $value The parameter value
|
Chris@0
|
144 */
|
Chris@0
|
145 public function setParameter($name, $value)
|
Chris@0
|
146 {
|
Chris@0
|
147 $this->parameterBag->set($name, $value);
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 /**
|
Chris@0
|
151 * Sets a service.
|
Chris@0
|
152 *
|
Chris@0
|
153 * Setting a service to null resets the service: has() returns false and get()
|
Chris@0
|
154 * behaves in the same way as if the service was never created.
|
Chris@0
|
155 *
|
Chris@0
|
156 * @param string $id The service identifier
|
Chris@0
|
157 * @param object $service The service instance
|
Chris@0
|
158 */
|
Chris@0
|
159 public function set($id, $service)
|
Chris@0
|
160 {
|
Chris@0
|
161 $id = strtolower($id);
|
Chris@0
|
162
|
Chris@0
|
163 if ('service_container' === $id) {
|
Chris@0
|
164 throw new InvalidArgumentException('You cannot set service "service_container".');
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 if (isset($this->aliases[$id])) {
|
Chris@0
|
168 unset($this->aliases[$id]);
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 $this->services[$id] = $service;
|
Chris@0
|
172
|
Chris@0
|
173 if (null === $service) {
|
Chris@0
|
174 unset($this->services[$id]);
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 if (isset($this->privates[$id])) {
|
Chris@0
|
178 if (null === $service) {
|
Chris@0
|
179 @trigger_error(sprintf('Unsetting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
|
Chris@0
|
180 unset($this->privates[$id]);
|
Chris@0
|
181 } else {
|
Chris@0
|
182 @trigger_error(sprintf('Setting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0. A new public service will be created instead.', $id), E_USER_DEPRECATED);
|
Chris@0
|
183 }
|
Chris@0
|
184 }
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 /**
|
Chris@0
|
188 * Returns true if the given service is defined.
|
Chris@0
|
189 *
|
Chris@0
|
190 * @param string $id The service identifier
|
Chris@0
|
191 *
|
Chris@0
|
192 * @return bool true if the service is defined, false otherwise
|
Chris@0
|
193 */
|
Chris@0
|
194 public function has($id)
|
Chris@0
|
195 {
|
Chris@0
|
196 for ($i = 2;;) {
|
Chris@12
|
197 if (isset($this->privates[$id])) {
|
Chris@12
|
198 @trigger_error(sprintf('Checking for the existence of the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
|
Chris@12
|
199 }
|
Chris@12
|
200
|
Chris@0
|
201 if ('service_container' === $id
|
Chris@0
|
202 || isset($this->aliases[$id])
|
Chris@0
|
203 || isset($this->services[$id])
|
Chris@0
|
204 ) {
|
Chris@0
|
205 return true;
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 if (isset($this->methodMap[$id])) {
|
Chris@0
|
209 return true;
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 if (--$i && $id !== $lcId = strtolower($id)) {
|
Chris@0
|
213 $id = $lcId;
|
Chris@0
|
214 continue;
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 // We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
|
Chris@0
|
218 // and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
|
Chris@0
|
219 if (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, 'get'.strtr($id, $this->underscoreMap).'Service')) {
|
Chris@0
|
220 @trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
|
Chris@0
|
221
|
Chris@0
|
222 return true;
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 return false;
|
Chris@0
|
226 }
|
Chris@0
|
227 }
|
Chris@0
|
228
|
Chris@0
|
229 /**
|
Chris@0
|
230 * Gets a service.
|
Chris@0
|
231 *
|
Chris@0
|
232 * If a service is defined both through a set() method and
|
Chris@0
|
233 * with a get{$id}Service() method, the former has always precedence.
|
Chris@0
|
234 *
|
Chris@0
|
235 * @param string $id The service identifier
|
Chris@0
|
236 * @param int $invalidBehavior The behavior when the service does not exist
|
Chris@0
|
237 *
|
Chris@0
|
238 * @return object The associated service
|
Chris@0
|
239 *
|
Chris@0
|
240 * @throws ServiceCircularReferenceException When a circular reference is detected
|
Chris@0
|
241 * @throws ServiceNotFoundException When the service is not defined
|
Chris@0
|
242 * @throws \Exception if an exception has been thrown when the service has been resolved
|
Chris@0
|
243 *
|
Chris@0
|
244 * @see Reference
|
Chris@0
|
245 */
|
Chris@0
|
246 public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
|
Chris@0
|
247 {
|
Chris@0
|
248 // Attempt to retrieve the service by checking first aliases then
|
Chris@0
|
249 // available services. Service IDs are case insensitive, however since
|
Chris@0
|
250 // this method can be called thousands of times during a request, avoid
|
Chris@0
|
251 // calling strtolower() unless necessary.
|
Chris@0
|
252 for ($i = 2;;) {
|
Chris@12
|
253 if (isset($this->privates[$id])) {
|
Chris@12
|
254 @trigger_error(sprintf('Requesting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
|
Chris@0
|
255 }
|
Chris@0
|
256 if (isset($this->aliases[$id])) {
|
Chris@0
|
257 $id = $this->aliases[$id];
|
Chris@0
|
258 }
|
Chris@12
|
259
|
Chris@0
|
260 // Re-use shared service instance if it exists.
|
Chris@0
|
261 if (isset($this->services[$id])) {
|
Chris@0
|
262 return $this->services[$id];
|
Chris@0
|
263 }
|
Chris@12
|
264 if ('service_container' === $id) {
|
Chris@12
|
265 return $this;
|
Chris@12
|
266 }
|
Chris@0
|
267
|
Chris@0
|
268 if (isset($this->loading[$id])) {
|
Chris@0
|
269 throw new ServiceCircularReferenceException($id, array_keys($this->loading));
|
Chris@0
|
270 }
|
Chris@0
|
271
|
Chris@0
|
272 if (isset($this->methodMap[$id])) {
|
Chris@0
|
273 $method = $this->methodMap[$id];
|
Chris@0
|
274 } elseif (--$i && $id !== $lcId = strtolower($id)) {
|
Chris@0
|
275 $id = $lcId;
|
Chris@0
|
276 continue;
|
Chris@0
|
277 } elseif (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) {
|
Chris@0
|
278 // We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
|
Chris@0
|
279 // and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
|
Chris@0
|
280 @trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
|
Chris@0
|
281 // $method is set to the right value, proceed
|
Chris@0
|
282 } else {
|
Chris@0
|
283 if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
|
Chris@0
|
284 if (!$id) {
|
Chris@0
|
285 throw new ServiceNotFoundException($id);
|
Chris@0
|
286 }
|
Chris@0
|
287
|
Chris@0
|
288 $alternatives = array();
|
Chris@0
|
289 foreach ($this->getServiceIds() as $knownId) {
|
Chris@0
|
290 $lev = levenshtein($id, $knownId);
|
Chris@0
|
291 if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) {
|
Chris@0
|
292 $alternatives[] = $knownId;
|
Chris@0
|
293 }
|
Chris@0
|
294 }
|
Chris@0
|
295
|
Chris@0
|
296 throw new ServiceNotFoundException($id, null, null, $alternatives);
|
Chris@0
|
297 }
|
Chris@0
|
298
|
Chris@0
|
299 return;
|
Chris@0
|
300 }
|
Chris@0
|
301
|
Chris@0
|
302 $this->loading[$id] = true;
|
Chris@0
|
303
|
Chris@0
|
304 try {
|
Chris@0
|
305 $service = $this->$method();
|
Chris@0
|
306 } catch (\Exception $e) {
|
Chris@0
|
307 unset($this->services[$id]);
|
Chris@0
|
308
|
Chris@0
|
309 throw $e;
|
Chris@0
|
310 } finally {
|
Chris@0
|
311 unset($this->loading[$id]);
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 return $service;
|
Chris@0
|
315 }
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 /**
|
Chris@0
|
319 * Returns true if the given service has actually been initialized.
|
Chris@0
|
320 *
|
Chris@0
|
321 * @param string $id The service identifier
|
Chris@0
|
322 *
|
Chris@0
|
323 * @return bool true if service has already been initialized, false otherwise
|
Chris@0
|
324 */
|
Chris@0
|
325 public function initialized($id)
|
Chris@0
|
326 {
|
Chris@0
|
327 $id = strtolower($id);
|
Chris@0
|
328
|
Chris@12
|
329 if (isset($this->aliases[$id])) {
|
Chris@12
|
330 $id = $this->aliases[$id];
|
Chris@12
|
331 }
|
Chris@12
|
332
|
Chris@0
|
333 if ('service_container' === $id) {
|
Chris@0
|
334 return false;
|
Chris@0
|
335 }
|
Chris@0
|
336
|
Chris@0
|
337 return isset($this->services[$id]);
|
Chris@0
|
338 }
|
Chris@0
|
339
|
Chris@0
|
340 /**
|
Chris@0
|
341 * {@inheritdoc}
|
Chris@0
|
342 */
|
Chris@0
|
343 public function reset()
|
Chris@0
|
344 {
|
Chris@0
|
345 $this->services = array();
|
Chris@0
|
346 }
|
Chris@0
|
347
|
Chris@0
|
348 /**
|
Chris@0
|
349 * Gets all service ids.
|
Chris@0
|
350 *
|
Chris@0
|
351 * @return array An array of all defined service ids
|
Chris@0
|
352 */
|
Chris@0
|
353 public function getServiceIds()
|
Chris@0
|
354 {
|
Chris@0
|
355 $ids = array();
|
Chris@0
|
356
|
Chris@0
|
357 if (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class) {
|
Chris@0
|
358 // We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
|
Chris@0
|
359 // and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
|
Chris@0
|
360 @trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
|
Chris@0
|
361
|
Chris@0
|
362 foreach (get_class_methods($this) as $method) {
|
Chris@0
|
363 if (preg_match('/^get(.+)Service$/', $method, $match)) {
|
Chris@0
|
364 $ids[] = self::underscore($match[1]);
|
Chris@0
|
365 }
|
Chris@0
|
366 }
|
Chris@0
|
367 }
|
Chris@0
|
368 $ids[] = 'service_container';
|
Chris@0
|
369
|
Chris@0
|
370 return array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->services)));
|
Chris@0
|
371 }
|
Chris@0
|
372
|
Chris@0
|
373 /**
|
Chris@0
|
374 * Camelizes a string.
|
Chris@0
|
375 *
|
Chris@0
|
376 * @param string $id A string to camelize
|
Chris@0
|
377 *
|
Chris@0
|
378 * @return string The camelized string
|
Chris@0
|
379 */
|
Chris@0
|
380 public static function camelize($id)
|
Chris@0
|
381 {
|
Chris@0
|
382 return strtr(ucwords(strtr($id, array('_' => ' ', '.' => '_ ', '\\' => '_ '))), array(' ' => ''));
|
Chris@0
|
383 }
|
Chris@0
|
384
|
Chris@0
|
385 /**
|
Chris@0
|
386 * A string to underscore.
|
Chris@0
|
387 *
|
Chris@0
|
388 * @param string $id The string to underscore
|
Chris@0
|
389 *
|
Chris@0
|
390 * @return string The underscored string
|
Chris@0
|
391 */
|
Chris@0
|
392 public static function underscore($id)
|
Chris@0
|
393 {
|
Chris@0
|
394 return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), str_replace('_', '.', $id)));
|
Chris@0
|
395 }
|
Chris@0
|
396
|
Chris@0
|
397 /**
|
Chris@0
|
398 * Fetches a variable from the environment.
|
Chris@0
|
399 *
|
Chris@0
|
400 * @param string The name of the environment variable
|
Chris@0
|
401 *
|
Chris@0
|
402 * @return scalar The value to use for the provided environment variable name
|
Chris@0
|
403 *
|
Chris@0
|
404 * @throws EnvNotFoundException When the environment variable is not found and has no default value
|
Chris@0
|
405 */
|
Chris@0
|
406 protected function getEnv($name)
|
Chris@0
|
407 {
|
Chris@0
|
408 if (isset($this->envCache[$name]) || array_key_exists($name, $this->envCache)) {
|
Chris@0
|
409 return $this->envCache[$name];
|
Chris@0
|
410 }
|
Chris@0
|
411 if (isset($_ENV[$name])) {
|
Chris@0
|
412 return $this->envCache[$name] = $_ENV[$name];
|
Chris@0
|
413 }
|
Chris@0
|
414 if (false !== $env = getenv($name)) {
|
Chris@0
|
415 return $this->envCache[$name] = $env;
|
Chris@0
|
416 }
|
Chris@0
|
417 if (!$this->hasParameter("env($name)")) {
|
Chris@0
|
418 throw new EnvNotFoundException($name);
|
Chris@0
|
419 }
|
Chris@0
|
420
|
Chris@0
|
421 return $this->envCache[$name] = $this->getParameter("env($name)");
|
Chris@0
|
422 }
|
Chris@0
|
423
|
Chris@0
|
424 private function __clone()
|
Chris@0
|
425 {
|
Chris@0
|
426 }
|
Chris@0
|
427 }
|