comparison vendor/symfony/dependency-injection/ServiceLocator.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
20 * @author Nicolas Grekas <p@tchwork.com> 20 * @author Nicolas Grekas <p@tchwork.com>
21 */ 21 */
22 class ServiceLocator implements PsrContainerInterface 22 class ServiceLocator implements PsrContainerInterface
23 { 23 {
24 private $factories; 24 private $factories;
25 private $loading = array(); 25 private $loading = [];
26 private $externalId; 26 private $externalId;
27 private $container; 27 private $container;
28 28
29 /** 29 /**
30 * @param callable[] $factories 30 * @param callable[] $factories
46 * {@inheritdoc} 46 * {@inheritdoc}
47 */ 47 */
48 public function get($id) 48 public function get($id)
49 { 49 {
50 if (!isset($this->factories[$id])) { 50 if (!isset($this->factories[$id])) {
51 throw new ServiceNotFoundException($id, end($this->loading) ?: null, null, array(), $this->createServiceNotFoundMessage($id)); 51 throw new ServiceNotFoundException($id, end($this->loading) ?: null, null, [], $this->createServiceNotFoundMessage($id));
52 } 52 }
53 53
54 if (isset($this->loading[$id])) { 54 if (isset($this->loading[$id])) {
55 $ids = array_values($this->loading); 55 $ids = array_values($this->loading);
56 $ids = array_slice($this->loading, array_search($id, $ids)); 56 $ids = \array_slice($this->loading, array_search($id, $ids));
57 $ids[] = $id; 57 $ids[] = $id;
58 58
59 throw new ServiceCircularReferenceException($id, $ids); 59 throw new ServiceCircularReferenceException($id, $ids);
60 } 60 }
61 61
89 if ($this->loading) { 89 if ($this->loading) {
90 return sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $this->formatAlternatives()); 90 return sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $this->formatAlternatives());
91 } 91 }
92 92
93 $class = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 3); 93 $class = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 3);
94 $class = isset($class[2]['object']) ? get_class($class[2]['object']) : null; 94 $class = isset($class[2]['object']) ? \get_class($class[2]['object']) : null;
95 $externalId = $this->externalId ?: $class; 95 $externalId = $this->externalId ?: $class;
96 96
97 $msg = sprintf('Service "%s" not found: ', $id); 97 $msg = [];
98 $msg[] = sprintf('Service "%s" not found:', $id);
98 99
99 if (!$this->container) { 100 if (!$this->container) {
100 $class = null; 101 $class = null;
101 } elseif ($this->container->has($id) || isset($this->container->getRemovedIds()[$id])) { 102 } elseif ($this->container->has($id) || isset($this->container->getRemovedIds()[$id])) {
102 $msg .= 'even though it exists in the app\'s container, '; 103 $msg[] = 'even though it exists in the app\'s container,';
103 } else { 104 } else {
104 try { 105 try {
105 $this->container->get($id); 106 $this->container->get($id);
106 $class = null; 107 $class = null;
107 } catch (ServiceNotFoundException $e) { 108 } catch (ServiceNotFoundException $e) {
108 if ($e->getAlternatives()) { 109 if ($e->getAlternatives()) {
109 $msg .= sprintf(' did you mean %s? Anyway, ', $this->formatAlternatives($e->getAlternatives(), 'or')); 110 $msg[] = sprintf('did you mean %s? Anyway,', $this->formatAlternatives($e->getAlternatives(), 'or'));
110 } else { 111 } else {
111 $class = null; 112 $class = null;
112 } 113 }
113 } 114 }
114 } 115 }
115 if ($externalId) { 116 if ($externalId) {
116 $msg .= sprintf('the container inside "%s" is a smaller service locator that %s', $externalId, $this->formatAlternatives()); 117 $msg[] = sprintf('the container inside "%s" is a smaller service locator that %s', $externalId, $this->formatAlternatives());
117 } else { 118 } else {
118 $msg .= sprintf('the current service locator %s', $this->formatAlternatives()); 119 $msg[] = sprintf('the current service locator %s', $this->formatAlternatives());
119 } 120 }
120 121
121 if (!$class) { 122 if (!$class) {
122 // no-op 123 // no-op
123 } elseif (is_subclass_of($class, ServiceSubscriberInterface::class)) { 124 } elseif (is_subclass_of($class, ServiceSubscriberInterface::class)) {
124 $msg .= sprintf(' Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "%s::getSubscribedServices()".', preg_replace('/([^\\\\]++\\\\)++/', '', $class)); 125 $msg[] = sprintf('Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "%s::getSubscribedServices()".', preg_replace('/([^\\\\]++\\\\)++/', '', $class));
125 } else { 126 } else {
126 $msg .= 'Try using dependency injection instead.'; 127 $msg[] = 'Try using dependency injection instead.';
127 } 128 }
128 129
129 return $msg; 130 return implode(' ', $msg);
130 } 131 }
131 132
132 private function formatAlternatives(array $alternatives = null, $separator = 'and') 133 private function formatAlternatives(array $alternatives = null, $separator = 'and')
133 { 134 {
134 $format = '"%s"%s'; 135 $format = '"%s"%s';
135 if (null === $alternatives) { 136 if (null === $alternatives) {
136 if (!$alternatives = array_keys($this->factories)) { 137 if (!$alternatives = array_keys($this->factories)) {
137 return 'is empty...'; 138 return 'is empty...';
138 } 139 }
139 $format = sprintf('only knows about the %s service%s.', $format, 1 < count($alternatives) ? 's' : ''); 140 $format = sprintf('only knows about the %s service%s.', $format, 1 < \count($alternatives) ? 's' : '');
140 } 141 }
141 $last = array_pop($alternatives); 142 $last = array_pop($alternatives);
142 143
143 return sprintf($format, $alternatives ? implode('", "', $alternatives) : $last, $alternatives ? sprintf(' %s "%s"', $separator, $last) : ''); 144 return sprintf($format, $alternatives ? implode('", "', $alternatives) : $last, $alternatives ? sprintf(' %s "%s"', $separator, $last) : '');
144 } 145 }