PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / symfony / dependency-injection / Container.php

Container.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 19.0, at vendor_prefixed/symfony/dependency-injection/Container.php

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