Container.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. <?php
  2. namespace Drupal\Component\DependencyInjection;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\DependencyInjection\ResettableContainerInterface;
  5. use Symfony\Component\DependencyInjection\Exception\LogicException;
  6. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  7. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  8. use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
  9. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  10. use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
  11. /**
  12. * Provides a container optimized for Drupal's needs.
  13. *
  14. * This container implementation is compatible with the default Symfony
  15. * dependency injection container and similar to the Symfony ContainerBuilder
  16. * class, but optimized for speed.
  17. *
  18. * It is based on a PHP array container definition dumped as a
  19. * performance-optimized machine-readable format.
  20. *
  21. * The best way to initialize this container is to use a Container Builder,
  22. * compile it and then retrieve the definition via
  23. * \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper::getArray().
  24. *
  25. * The retrieved array can be cached safely and then passed to this container
  26. * via the constructor.
  27. *
  28. * As the container is unfrozen by default, a second parameter can be passed to
  29. * the container to "freeze" the parameter bag.
  30. *
  31. * This container is different in behavior from the default Symfony container in
  32. * the following ways:
  33. *
  34. * - It only allows lowercase service and parameter names, though it does only
  35. * enforce it via assertions for performance reasons.
  36. * - The following functions, that are not part of the interface, are explicitly
  37. * not supported: getParameterBag(), isFrozen(), compile(),
  38. * getAServiceWithAnIdByCamelCase().
  39. * - The function getServiceIds() was added as it has a use-case in core and
  40. * contrib.
  41. *
  42. * @ingroup container
  43. */
  44. class Container implements ContainerInterface, ResettableContainerInterface {
  45. /**
  46. * The parameters of the container.
  47. *
  48. * @var array
  49. */
  50. protected $parameters = [];
  51. /**
  52. * The aliases of the container.
  53. *
  54. * @var array
  55. */
  56. protected $aliases = [];
  57. /**
  58. * The service definitions of the container.
  59. *
  60. * @var array
  61. */
  62. protected $serviceDefinitions = [];
  63. /**
  64. * The instantiated services.
  65. *
  66. * @var array
  67. */
  68. protected $services = [];
  69. /**
  70. * The instantiated private services.
  71. *
  72. * @var array
  73. */
  74. protected $privateServices = [];
  75. /**
  76. * The currently loading services.
  77. *
  78. * @var array
  79. */
  80. protected $loading = [];
  81. /**
  82. * Whether the container parameters can still be changed.
  83. *
  84. * For testing purposes the container needs to be changed.
  85. *
  86. * @var bool
  87. */
  88. protected $frozen = TRUE;
  89. /**
  90. * Constructs a new Container instance.
  91. *
  92. * @param array $container_definition
  93. * An array containing the following keys:
  94. * - aliases: The aliases of the container.
  95. * - parameters: The parameters of the container.
  96. * - services: The service definitions of the container.
  97. * - frozen: Whether the container definition came from a frozen
  98. * container builder or not.
  99. * - machine_format: Whether this container definition uses the optimized
  100. * machine-readable container format.
  101. */
  102. public function __construct(array $container_definition = []) {
  103. if (!empty($container_definition) && (!isset($container_definition['machine_format']) || $container_definition['machine_format'] !== TRUE)) {
  104. throw new InvalidArgumentException('The non-optimized format is not supported by this class. Use an optimized machine-readable format instead, e.g. as produced by \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper.');
  105. }
  106. $this->aliases = isset($container_definition['aliases']) ? $container_definition['aliases'] : [];
  107. $this->parameters = isset($container_definition['parameters']) ? $container_definition['parameters'] : [];
  108. $this->serviceDefinitions = isset($container_definition['services']) ? $container_definition['services'] : [];
  109. $this->frozen = isset($container_definition['frozen']) ? $container_definition['frozen'] : FALSE;
  110. // Register the service_container with itself.
  111. $this->services['service_container'] = $this;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function get($id, $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
  117. if (isset($this->aliases[$id])) {
  118. $id = $this->aliases[$id];
  119. }
  120. // Re-use shared service instance if it exists.
  121. if (isset($this->services[$id]) || ($invalid_behavior === ContainerInterface::NULL_ON_INVALID_REFERENCE && array_key_exists($id, $this->services))) {
  122. return $this->services[$id];
  123. }
  124. if (isset($this->loading[$id])) {
  125. throw new ServiceCircularReferenceException($id, array_keys($this->loading));
  126. }
  127. $definition = isset($this->serviceDefinitions[$id]) ? $this->serviceDefinitions[$id] : NULL;
  128. if (!$definition && $invalid_behavior === ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
  129. if (!$id) {
  130. throw new ServiceNotFoundException($id);
  131. }
  132. throw new ServiceNotFoundException($id, NULL, NULL, $this->getServiceAlternatives($id));
  133. }
  134. // In case something else than ContainerInterface::NULL_ON_INVALID_REFERENCE
  135. // is used, the actual wanted behavior is to re-try getting the service at a
  136. // later point.
  137. if (!$definition) {
  138. return;
  139. }
  140. // Definition is a keyed array, so [0] is only defined when it is a
  141. // serialized string.
  142. if (isset($definition[0])) {
  143. $definition = unserialize($definition);
  144. }
  145. // Now create the service.
  146. $this->loading[$id] = TRUE;
  147. try {
  148. $service = $this->createService($definition, $id);
  149. }
  150. catch (\Exception $e) {
  151. unset($this->loading[$id]);
  152. unset($this->services[$id]);
  153. if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalid_behavior) {
  154. return;
  155. }
  156. throw $e;
  157. }
  158. unset($this->loading[$id]);
  159. return $service;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function reset() {
  165. if (!empty($this->scopedServices)) {
  166. throw new LogicException('Resetting the container is not allowed when a scope is active.');
  167. }
  168. $this->services = [];
  169. }
  170. /**
  171. * Creates a service from a service definition.
  172. *
  173. * @param array $definition
  174. * The service definition to create a service from.
  175. * @param string $id
  176. * The service identifier, necessary so it can be shared if its public.
  177. *
  178. * @return object
  179. * The service described by the service definition.
  180. *
  181. * @throws \Symfony\Component\DependencyInjection\Exception\RuntimeException
  182. * Thrown when the service is a synthetic service.
  183. * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  184. * Thrown when the configurator callable in $definition['configurator'] is
  185. * not actually a callable.
  186. * @throws \ReflectionException
  187. * Thrown when the service class takes more than 10 parameters to construct,
  188. * and cannot be instantiated.
  189. */
  190. protected function createService(array $definition, $id) {
  191. if (isset($definition['synthetic']) && $definition['synthetic'] === TRUE) {
  192. throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The service container does not know how to construct this service. The service will need to be set before it is first used.', $id));
  193. }
  194. $arguments = [];
  195. if (isset($definition['arguments'])) {
  196. $arguments = $definition['arguments'];
  197. if ($arguments instanceof \stdClass) {
  198. $arguments = $this->resolveServicesAndParameters($arguments);
  199. }
  200. }
  201. if (isset($definition['file'])) {
  202. $file = $this->frozen ? $definition['file'] : current($this->resolveServicesAndParameters([$definition['file']]));
  203. require_once $file;
  204. }
  205. if (isset($definition['factory'])) {
  206. $factory = $definition['factory'];
  207. if (is_array($factory)) {
  208. $factory = $this->resolveServicesAndParameters([$factory[0], $factory[1]]);
  209. }
  210. elseif (!is_string($factory)) {
  211. throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
  212. }
  213. $service = call_user_func_array($factory, $arguments);
  214. }
  215. else {
  216. $class = $this->frozen ? $definition['class'] : current($this->resolveServicesAndParameters([$definition['class']]));
  217. $length = isset($definition['arguments_count']) ? $definition['arguments_count'] : count($arguments);
  218. // Optimize class instantiation for services with up to 10 parameters as
  219. // ReflectionClass is noticeably slow.
  220. switch ($length) {
  221. case 0:
  222. $service = new $class();
  223. break;
  224. case 1:
  225. $service = new $class($arguments[0]);
  226. break;
  227. case 2:
  228. $service = new $class($arguments[0], $arguments[1]);
  229. break;
  230. case 3:
  231. $service = new $class($arguments[0], $arguments[1], $arguments[2]);
  232. break;
  233. case 4:
  234. $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
  235. break;
  236. case 5:
  237. $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
  238. break;
  239. case 6:
  240. $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5]);
  241. break;
  242. case 7:
  243. $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6]);
  244. break;
  245. case 8:
  246. $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7]);
  247. break;
  248. case 9:
  249. $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7], $arguments[8]);
  250. break;
  251. case 10:
  252. $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7], $arguments[8], $arguments[9]);
  253. break;
  254. default:
  255. $r = new \ReflectionClass($class);
  256. $service = $r->newInstanceArgs($arguments);
  257. break;
  258. }
  259. }
  260. if (!isset($definition['shared']) || $definition['shared'] !== FALSE) {
  261. $this->services[$id] = $service;
  262. }
  263. if (isset($definition['calls'])) {
  264. foreach ($definition['calls'] as $call) {
  265. $method = $call[0];
  266. $arguments = [];
  267. if (!empty($call[1])) {
  268. $arguments = $call[1];
  269. if ($arguments instanceof \stdClass) {
  270. $arguments = $this->resolveServicesAndParameters($arguments);
  271. }
  272. }
  273. call_user_func_array([$service, $method], $arguments);
  274. }
  275. }
  276. if (isset($definition['properties'])) {
  277. if ($definition['properties'] instanceof \stdClass) {
  278. $definition['properties'] = $this->resolveServicesAndParameters($definition['properties']);
  279. }
  280. foreach ($definition['properties'] as $key => $value) {
  281. $service->{$key} = $value;
  282. }
  283. }
  284. if (isset($definition['configurator'])) {
  285. $callable = $definition['configurator'];
  286. if (is_array($callable)) {
  287. $callable = $this->resolveServicesAndParameters($callable);
  288. }
  289. if (!is_callable($callable)) {
  290. throw new InvalidArgumentException(sprintf('The configurator for class "%s" is not a callable.', get_class($service)));
  291. }
  292. call_user_func($callable, $service);
  293. }
  294. return $service;
  295. }
  296. /**
  297. * {@inheritdoc}
  298. */
  299. public function set($id, $service) {
  300. $this->services[$id] = $service;
  301. }
  302. /**
  303. * {@inheritdoc}
  304. */
  305. public function has($id) {
  306. return isset($this->aliases[$id]) || isset($this->services[$id]) || isset($this->serviceDefinitions[$id]);
  307. }
  308. /**
  309. * {@inheritdoc}
  310. */
  311. public function getParameter($name) {
  312. if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters))) {
  313. if (!$name) {
  314. throw new ParameterNotFoundException($name);
  315. }
  316. throw new ParameterNotFoundException($name, NULL, NULL, NULL, $this->getParameterAlternatives($name));
  317. }
  318. return $this->parameters[$name];
  319. }
  320. /**
  321. * {@inheritdoc}
  322. */
  323. public function hasParameter($name) {
  324. return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters);
  325. }
  326. /**
  327. * {@inheritdoc}
  328. */
  329. public function setParameter($name, $value) {
  330. if ($this->frozen) {
  331. throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
  332. }
  333. $this->parameters[$name] = $value;
  334. }
  335. /**
  336. * {@inheritdoc}
  337. */
  338. public function initialized($id) {
  339. if (isset($this->aliases[$id])) {
  340. $id = $this->aliases[$id];
  341. }
  342. return isset($this->services[$id]) || array_key_exists($id, $this->services);
  343. }
  344. /**
  345. * Resolves arguments that represent services or variables to the real values.
  346. *
  347. * @param array|\stdClass $arguments
  348. * The arguments to resolve.
  349. *
  350. * @return array
  351. * The resolved arguments.
  352. *
  353. * @throws \Symfony\Component\DependencyInjection\Exception\RuntimeException
  354. * If a parameter/service could not be resolved.
  355. * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  356. * If an unknown type is met while resolving parameters and services.
  357. */
  358. protected function resolveServicesAndParameters($arguments) {
  359. // Check if this collection needs to be resolved.
  360. if ($arguments instanceof \stdClass) {
  361. if ($arguments->type !== 'collection') {
  362. throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $arguments->type));
  363. }
  364. // In case there is nothing to resolve, we are done here.
  365. if (!$arguments->resolve) {
  366. return $arguments->value;
  367. }
  368. $arguments = $arguments->value;
  369. }
  370. // Process the arguments.
  371. foreach ($arguments as $key => $argument) {
  372. // For this machine-optimized format, only \stdClass arguments are
  373. // processed and resolved. All other values are kept as is.
  374. if ($argument instanceof \stdClass) {
  375. $type = $argument->type;
  376. // Check for parameter.
  377. if ($type == 'parameter') {
  378. $name = $argument->name;
  379. if (!isset($this->parameters[$name])) {
  380. $arguments[$key] = $this->getParameter($name);
  381. // This can never be reached as getParameter() throws an Exception,
  382. // because we already checked that the parameter is not set above.
  383. }
  384. // Update argument.
  385. $argument = $arguments[$key] = $this->parameters[$name];
  386. // In case there is not a machine readable value (e.g. a service)
  387. // behind this resolved parameter, continue.
  388. if (!($argument instanceof \stdClass)) {
  389. continue;
  390. }
  391. // Fall through.
  392. $type = $argument->type;
  393. }
  394. // Create a service.
  395. if ($type == 'service') {
  396. $id = $argument->id;
  397. // Does the service already exist?
  398. if (isset($this->aliases[$id])) {
  399. $id = $this->aliases[$id];
  400. }
  401. if (isset($this->services[$id])) {
  402. $arguments[$key] = $this->services[$id];
  403. continue;
  404. }
  405. // Return the service.
  406. $arguments[$key] = $this->get($id, $argument->invalidBehavior);
  407. continue;
  408. }
  409. // Create private service.
  410. elseif ($type == 'private_service') {
  411. $id = $argument->id;
  412. // Does the private service already exist.
  413. if (isset($this->privateServices[$id])) {
  414. $arguments[$key] = $this->privateServices[$id];
  415. continue;
  416. }
  417. // Create the private service.
  418. $arguments[$key] = $this->createService($argument->value, $id);
  419. if ($argument->shared) {
  420. $this->privateServices[$id] = $arguments[$key];
  421. }
  422. continue;
  423. }
  424. // Check for collection.
  425. elseif ($type == 'collection') {
  426. $value = $argument->value;
  427. // Does this collection need resolving?
  428. if ($argument->resolve) {
  429. $arguments[$key] = $this->resolveServicesAndParameters($value);
  430. }
  431. else {
  432. $arguments[$key] = $value;
  433. }
  434. continue;
  435. }
  436. if ($type !== NULL) {
  437. throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $type));
  438. }
  439. }
  440. }
  441. return $arguments;
  442. }
  443. /**
  444. * Provides alternatives for a given array and key.
  445. *
  446. * @param string $search_key
  447. * The search key to get alternatives for.
  448. * @param array $keys
  449. * The search space to search for alternatives in.
  450. *
  451. * @return string[]
  452. * An array of strings with suitable alternatives.
  453. */
  454. protected function getAlternatives($search_key, array $keys) {
  455. $alternatives = [];
  456. foreach ($keys as $key) {
  457. $lev = levenshtein($search_key, $key);
  458. if ($lev <= strlen($search_key) / 3 || strpos($key, $search_key) !== FALSE) {
  459. $alternatives[] = $key;
  460. }
  461. }
  462. return $alternatives;
  463. }
  464. /**
  465. * Provides alternatives in case a service was not found.
  466. *
  467. * @param string $id
  468. * The service to get alternatives for.
  469. *
  470. * @return string[]
  471. * An array of strings with suitable alternatives.
  472. */
  473. protected function getServiceAlternatives($id) {
  474. $all_service_keys = array_unique(array_merge(array_keys($this->services), array_keys($this->serviceDefinitions)));
  475. return $this->getAlternatives($id, $all_service_keys);
  476. }
  477. /**
  478. * Provides alternatives in case a parameter was not found.
  479. *
  480. * @param string $name
  481. * The parameter to get alternatives for.
  482. *
  483. * @return string[]
  484. * An array of strings with suitable alternatives.
  485. */
  486. protected function getParameterAlternatives($name) {
  487. return $this->getAlternatives($name, array_keys($this->parameters));
  488. }
  489. /**
  490. * Gets all defined service IDs.
  491. *
  492. * @return array
  493. * An array of all defined service IDs.
  494. */
  495. public function getServiceIds() {
  496. return array_keys($this->serviceDefinitions + $this->services);
  497. }
  498. /**
  499. * Ensure that cloning doesn't work.
  500. */
  501. private function __clone() {
  502. }
  503. }