vendor/symfony/dependency-injection/Container.php line 215

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