PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
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 trunk, at vendor_prefixed/symfony/dependency-injection/Container.php

390 lines 15.1 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\Argument\RewindableGenerator;
14 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Argument\ServiceLocator as ArgumentServiceLocator;
15 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
16 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
18 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\RuntimeException;
19 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
20 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
21 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
22 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
23 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
24 use YoastSEO_Vendor\Symfony\Contracts\Service\ResetInterface;
25 // Help opcache.preload discover always-needed symbols
26 \class_exists(\YoastSEO_Vendor\Symfony\Component\DependencyInjection\Argument\RewindableGenerator::class);
27 \class_exists(\YoastSEO_Vendor\Symfony\Component\DependencyInjection\Argument\ServiceLocator::class);
28 /**
29 * Container is a dependency injection container.
30 *
31 * It gives access to object instances (services).
32 * Services and parameters are simple key/pair stores.
33 * The container can have four possible behaviors when a service
34 * does not exist (or is not initialized for the last case):
35 *
36 * * EXCEPTION_ON_INVALID_REFERENCE: Throws an exception at compilation time (the default)
37 * * NULL_ON_INVALID_REFERENCE: Returns null
38 * * IGNORE_ON_INVALID_REFERENCE: Ignores the wrapping command asking for the reference
39 * (for instance, ignore a setter if the service does not exist)
40 * * IGNORE_ON_UNINITIALIZED_REFERENCE: Ignores/returns null for uninitialized services or invalid references
41 * * RUNTIME_EXCEPTION_ON_INVALID_REFERENCE: Throws an exception at runtime
42 *
43 * @author Fabien Potencier <fabien@symfony.com>
44 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
45 */
46 class Container implements \YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface, \YoastSEO_Vendor\Symfony\Contracts\Service\ResetInterface
47 {
48 protected $parameterBag;
49 protected $services = [];
50 protected $privates = [];
51 protected $fileMap = [];
52 protected $methodMap = [];
53 protected $factories = [];
54 protected $aliases = [];
55 protected $loading = [];
56 protected $resolving = [];
57 protected $syntheticIds = [];
58 private $envCache = [];
59 private $compiled = \false;
60 private $getEnv;
61 public function __construct(?\YoastSEO_Vendor\Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface $parameterBag = null)
62 {
63 $this->parameterBag = $parameterBag ?? new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag();
64 }
65 /**
66 * Compiles the container.
67 *
68 * This method does two things:
69 *
70 * * Parameter values are resolved;
71 * * The parameter bag is frozen.
72 */
73 public function compile()
74 {
75 $this->parameterBag->resolve();
76 $this->parameterBag = new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag($this->parameterBag->all());
77 $this->compiled = \true;
78 }
79 /**
80 * Returns true if the container is compiled.
81 *
82 * @return bool
83 */
84 public function isCompiled()
85 {
86 return $this->compiled;
87 }
88 /**
89 * Gets the service container parameter bag.
90 *
91 * @return ParameterBagInterface
92 */
93 public function getParameterBag()
94 {
95 return $this->parameterBag;
96 }
97 /**
98 * Gets a parameter.
99 *
100 * @return array|bool|string|int|float|\UnitEnum|null
101 *
102 * @throws InvalidArgumentException if the parameter is not defined
103 */
104 public function getParameter(string $name)
105 {
106 return $this->parameterBag->get($name);
107 }
108 /**
109 * @return bool
110 */
111 public function hasParameter(string $name)
112 {
113 return $this->parameterBag->has($name);
114 }
115 /**
116 * Sets a parameter.
117 *
118 * @param string $name The parameter name
119 * @param array|bool|string|int|float|\UnitEnum|null $value The parameter value
120 */
121 public function setParameter(string $name, $value)
122 {
123 $this->parameterBag->set($name, $value);
124 }
125 /**
126 * Sets a service.
127 *
128 * Setting a synthetic service to null resets it: has() returns false and get()
129 * behaves in the same way as if the service was never created.
130 */
131 public function set(string $id, ?object $service)
132 {
133 // Runs the internal initializer; used by the dumped container to include always-needed files
134 if (isset($this->privates['service_container']) && $this->privates['service_container'] instanceof \Closure) {
135 $initialize = $this->privates['service_container'];
136 unset($this->privates['service_container']);
137 $initialize();
138 }
139 if ('service_container' === $id) {
140 throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException('You cannot set service "service_container".');
141 }
142 if (!(isset($this->fileMap[$id]) || isset($this->methodMap[$id]))) {
143 if (isset($this->syntheticIds[$id]) || !isset($this->getRemovedIds()[$id])) {
144 // no-op
145 } elseif (null === $service) {
146 throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException(\sprintf('The "%s" service is private, you cannot unset it.', $id));
147 } else {
148 throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException(\sprintf('The "%s" service is private, you cannot replace it.', $id));
149 }
150 } elseif (isset($this->services[$id])) {
151 throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException(\sprintf('The "%s" service is already initialized, you cannot replace it.', $id));
152 }
153 if (isset($this->aliases[$id])) {
154 unset($this->aliases[$id]);
155 }
156 if (null === $service) {
157 unset($this->services[$id]);
158 return;
159 }
160 $this->services[$id] = $service;
161 }
162 /**
163 * Returns true if the given service is defined.
164 *
165 * @param string $id The service identifier
166 *
167 * @return bool
168 */
169 public function has(string $id)
170 {
171 if (isset($this->aliases[$id])) {
172 $id = $this->aliases[$id];
173 }
174 if (isset($this->services[$id])) {
175 return \true;
176 }
177 if ('service_container' === $id) {
178 return \true;
179 }
180 return isset($this->fileMap[$id]) || isset($this->methodMap[$id]);
181 }
182 /**
183 * Gets a service.
184 *
185 * @return object|null
186 *
187 * @throws ServiceCircularReferenceException When a circular reference is detected
188 * @throws ServiceNotFoundException When the service is not defined
189 * @throws \Exception if an exception has been thrown when the service has been resolved
190 *
191 * @see Reference
192 */
193 public function get(string $id, int $invalidBehavior = 1)
194 {
195 return $this->services[$id] ?? $this->services[$id = $this->aliases[$id] ?? $id] ?? ('service_container' === $id ? $this : ($this->factories[$id] ?? [$this, 'make'])($id, $invalidBehavior));
196 }
197 /**
198 * Creates a service.
199 *
200 * As a separate method to allow "get()" to use the really fast `??` operator.
201 */
202 private function make(string $id, int $invalidBehavior)
203 {
204 if (isset($this->loading[$id])) {
205 throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException($id, \array_merge(\array_keys($this->loading), [$id]));
206 }
207 $this->loading[$id] = \true;
208 try {
209 if (isset($this->fileMap[$id])) {
210 return 4 === $invalidBehavior ? null : $this->load($this->fileMap[$id]);
211 } elseif (isset($this->methodMap[$id])) {
212 return 4 === $invalidBehavior ? null : $this->{$this->methodMap[$id]}();
213 }
214 } catch (\Exception $e) {
215 unset($this->services[$id]);
216 throw $e;
217 } finally {
218 unset($this->loading[$id]);
219 }
220 if (1 === $invalidBehavior) {
221 if (!$id) {
222 throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException($id);
223 }
224 if (isset($this->syntheticIds[$id])) {
225 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));
226 }
227 if (isset($this->getRemovedIds()[$id])) {
228 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));
229 }
230 $alternatives = [];
231 foreach ($this->getServiceIds() as $knownId) {
232 if ('' === $knownId || '.' === $knownId[0]) {
233 continue;
234 }
235 $lev = \levenshtein($id, $knownId);
236 if ($lev <= \strlen($id) / 3 || \str_contains($knownId, $id)) {
237 $alternatives[] = $knownId;
238 }
239 }
240 throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException($id, null, null, $alternatives);
241 }
242 return null;
243 }
244 /**
245 * Returns true if the given service has actually been initialized.
246 *
247 * @return bool
248 */
249 public function initialized(string $id)
250 {
251 if (isset($this->aliases[$id])) {
252 $id = $this->aliases[$id];
253 }
254 if ('service_container' === $id) {
255 return \false;
256 }
257 return isset($this->services[$id]);
258 }
259 /**
260 * {@inheritdoc}
261 */
262 public function reset()
263 {
264 $services = $this->services + $this->privates;
265 foreach ($services as $service) {
266 try {
267 if ($service instanceof \YoastSEO_Vendor\Symfony\Contracts\Service\ResetInterface) {
268 $service->reset();
269 }
270 } catch (\Throwable $e) {
271 continue;
272 }
273 }
274 $this->services = $this->factories = $this->privates = [];
275 }
276 /**
277 * Gets all service ids.
278 *
279 * @return string[]
280 */
281 public function getServiceIds()
282 {
283 return \array_map('strval', \array_unique(\array_merge(['service_container'], \array_keys($this->fileMap), \array_keys($this->methodMap), \array_keys($this->aliases), \array_keys($this->services))));
284 }
285 /**
286 * Gets service ids that existed at compile time.
287 *
288 * @return array
289 */
290 public function getRemovedIds()
291 {
292 return [];
293 }
294 /**
295 * Camelizes a string.
296 *
297 * @return string
298 */
299 public static function camelize(string $id)
300 {
301 return \strtr(\ucwords(\strtr($id, ['_' => ' ', '.' => '_ ', '\\' => '_ '])), [' ' => '']);
302 }
303 /**
304 * A string to underscore.
305 *
306 * @return string
307 */
308 public static function underscore(string $id)
309 {
310 return \strtolower(\preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\\d])([A-Z])/'], ['\\1_\\2', '\\1_\\2'], \str_replace('_', '.', $id)));
311 }
312 /**
313 * Creates a service by requiring its factory file.
314 */
315 protected function load(string $file)
316 {
317 return require $file;
318 }
319 /**
320 * Fetches a variable from the environment.
321 *
322 * @return mixed
323 *
324 * @throws EnvNotFoundException When the environment variable is not found and has no default value
325 */
326 protected function getEnv(string $name)
327 {
328 if (isset($this->resolving[$envName = "env({$name})"])) {
329 throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException(\array_keys($this->resolving));
330 }
331 if (isset($this->envCache[$name]) || \array_key_exists($name, $this->envCache)) {
332 return $this->envCache[$name];
333 }
334 if (!$this->has($id = 'container.env_var_processors_locator')) {
335 $this->set($id, new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\ServiceLocator([]));
336 }
337 if (!$this->getEnv) {
338 $this->getEnv = \Closure::fromCallable([$this, 'getEnv']);
339 }
340 $processors = $this->get($id);
341 if (\false !== ($i = \strpos($name, ':'))) {
342 $prefix = \substr($name, 0, $i);
343 $localName = \substr($name, 1 + $i);
344 } else {
345 $prefix = 'string';
346 $localName = $name;
347 }
348 $processor = $processors->has($prefix) ? $processors->get($prefix) : new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\EnvVarProcessor($this);
349 if (\false === $i) {
350 $prefix = '';
351 }
352 $this->resolving[$envName] = \true;
353 try {
354 return $this->envCache[$name] = $processor->getEnv($prefix, $localName, $this->getEnv);
355 } finally {
356 unset($this->resolving[$envName]);
357 }
358 }
359 /**
360 * @param string|false $registry
361 * @param string|bool $load
362 *
363 * @return mixed
364 *
365 * @internal
366 */
367 protected final function getService($registry, string $id, ?string $method, $load)
368 {
369 if ('service_container' === $id) {
370 return $this;
371 }
372 if (\is_string($load)) {
373 throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\RuntimeException($load);
374 }
375 if (null === $method) {
376 return \false !== $registry ? $this->{$registry}[$id] ?? null : null;
377 }
378 if (\false !== $registry) {
379 return $this->{$registry}[$id] ?? ($this->{$registry}[$id] = $load ? $this->load($method) : $this->{$method}());
380 }
381 if (!$load) {
382 return $this->{$method}();
383 }
384 return ($factory = $this->factories[$id] ?? $this->factories['service_container'][$id] ?? null) ? $factory() : $this->load($method);
385 }
386 private function __clone()
387 {
388 }
389 }
390