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 / service-contracts / ServiceLocatorTrait.php

ServiceLocatorTrait.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at vendor_prefixed/symfony/service-contracts/ServiceLocatorTrait.php

114 lines 3.9 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 <[email protected]>
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\Contracts\Service;
12
13 use YoastSEO_Vendor\Psr\Container\ContainerExceptionInterface;
14 use YoastSEO_Vendor\Psr\Container\NotFoundExceptionInterface;
15 // Help opcache.preload discover always-needed symbols
16 \class_exists(\YoastSEO_Vendor\Psr\Container\ContainerExceptionInterface::class);
17 \class_exists(\YoastSEO_Vendor\Psr\Container\NotFoundExceptionInterface::class);
18 /**
19 * A trait to help implement ServiceProviderInterface.
20 *
21 * @author Robin Chalas <[email protected]>
22 * @author Nicolas Grekas <[email protected]>
23 */
24 trait ServiceLocatorTrait
25 {
26 private $factories;
27 private $loading = [];
28 private $providedTypes;
29 /**
30 * @param callable[] $factories
31 */
32 public function __construct(array $factories)
33 {
34 $this->factories = $factories;
35 }
36 /**
37 * {@inheritdoc}
38 *
39 * @return bool
40 */
41 public function has(string $id)
42 {
43 return isset($this->factories[$id]);
44 }
45 /**
46 * {@inheritdoc}
47 *
48 * @return mixed
49 */
50 public function get(string $id)
51 {
52 if (!isset($this->factories[$id])) {
53 throw $this->createNotFoundException($id);
54 }
55 if (isset($this->loading[$id])) {
56 $ids = \array_values($this->loading);
57 $ids = \array_slice($this->loading, \array_search($id, $ids));
58 $ids[] = $id;
59 throw $this->createCircularReferenceException($id, $ids);
60 }
61 $this->loading[$id] = $id;
62 try {
63 return $this->factories[$id]($this);
64 } finally {
65 unset($this->loading[$id]);
66 }
67 }
68 /**
69 * {@inheritdoc}
70 */
71 public function getProvidedServices() : array
72 {
73 if (null === $this->providedTypes) {
74 $this->providedTypes = [];
75 foreach ($this->factories as $name => $factory) {
76 if (!\is_callable($factory)) {
77 $this->providedTypes[$name] = '?';
78 } else {
79 $type = (new \ReflectionFunction($factory))->getReturnType();
80 $this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '') . ($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?';
81 }
82 }
83 }
84 return $this->providedTypes;
85 }
86 private function createNotFoundException(string $id) : \YoastSEO_Vendor\Psr\Container\NotFoundExceptionInterface
87 {
88 if (!($alternatives = \array_keys($this->factories))) {
89 $message = 'is empty...';
90 } else {
91 $last = \array_pop($alternatives);
92 if ($alternatives) {
93 $message = \sprintf('only knows about the "%s" and "%s" services.', \implode('", "', $alternatives), $last);
94 } else {
95 $message = \sprintf('only knows about the "%s" service.', $last);
96 }
97 }
98 if ($this->loading) {
99 $message = \sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', \end($this->loading), $id, $message);
100 } else {
101 $message = \sprintf('Service "%s" not found: the current service locator %s', $id, $message);
102 }
103 return new class($message) extends \InvalidArgumentException implements \YoastSEO_Vendor\Psr\Container\NotFoundExceptionInterface
104 {
105 };
106 }
107 private function createCircularReferenceException(string $id, array $path) : \YoastSEO_Vendor\Psr\Container\ContainerExceptionInterface
108 {
109 return new class(\sprintf('Circular reference detected for service "%s", path: "%s".', $id, \implode(' -> ', $path))) extends \RuntimeException implements \YoastSEO_Vendor\Psr\Container\ContainerExceptionInterface
110 {
111 };
112 }
113 }
114