PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php

https://gitlab.com/pr0055/symfonypizza
PHP | 362 lines | 298 code | 8 blank | 56 comment | 0 complexity | b6e489212a5eb4deca5926c59c302324 MD5 | raw file
  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\Exception\InvalidArgumentException;
  12. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  13. use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  16. use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
  17. /**
  18. * Container is a dependency injection container.
  19. *
  20. * It gives access to object instances (services).
  21. *
  22. * Services and parameters are simple key/pair stores.
  23. *
  24. * Parameter and service keys are case insensitive.
  25. *
  26. * A service id can contain lowercased letters, digits, underscores, and dots.
  27. * Underscores are used to separate words, and dots to group services
  28. * under namespaces:
  29. *
  30. * <ul>
  31. * <li>request</li>
  32. * <li>mysql_session_storage</li>
  33. * <li>symfony.mysql_session_storage</li>
  34. * </ul>
  35. *
  36. * A service can also be defined by creating a method named
  37. * getXXXService(), where XXX is the camelized version of the id:
  38. *
  39. * <ul>
  40. * <li>request -> getRequestService()</li>
  41. * <li>mysql_session_storage -> getMysqlSessionStorageService()</li>
  42. * <li>symfony.mysql_session_storage -> getSymfony_MysqlSessionStorageService()</li>
  43. * </ul>
  44. *
  45. * The container can have three possible behaviors when a service does not exist:
  46. *
  47. * * EXCEPTION_ON_INVALID_REFERENCE: Throws an exception (the default)
  48. * * NULL_ON_INVALID_REFERENCE: Returns null
  49. * * IGNORE_ON_INVALID_REFERENCE: Ignores the wrapping command asking for the reference
  50. * (for instance, ignore a setter if the service does not exist)
  51. *
  52. * @author Fabien Potencier <fabien@symfony.com>
  53. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  54. */
  55. class Container implements ResettableContainerInterface
  56. {
  57. /**
  58. * @var ParameterBagInterface
  59. */
  60. protected $parameterBag;
  61. protected $services = array();
  62. protected $methodMap = array();
  63. protected $aliases = array();
  64. protected $loading = array();
  65. private $underscoreMap = array('_' => '', '.' => '_', '\\' => '_');
  66. /**
  67. * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
  68. */
  69. public function __construct(ParameterBagInterface $parameterBag = null)
  70. {
  71. $this->parameterBag = $parameterBag ?: new ParameterBag();
  72. }
  73. /**
  74. * Compiles the container.
  75. *
  76. * This method does two things:
  77. *
  78. * * Parameter values are resolved;
  79. * * The parameter bag is frozen.
  80. */
  81. public function compile()
  82. {
  83. $this->parameterBag->resolve();
  84. $this->parameterBag = new FrozenParameterBag($this->parameterBag->all());
  85. }
  86. /**
  87. * Returns true if the container parameter bag are frozen.
  88. *
  89. * @return bool true if the container parameter bag are frozen, false otherwise
  90. */
  91. public function isFrozen()
  92. {
  93. return $this->parameterBag instanceof FrozenParameterBag;
  94. }
  95. /**
  96. * Gets the service container parameter bag.
  97. *
  98. * @return ParameterBagInterface A ParameterBagInterface instance
  99. */
  100. public function getParameterBag()
  101. {
  102. return $this->parameterBag;
  103. }
  104. /**
  105. * Gets a parameter.
  106. *
  107. * @param string $name The parameter name
  108. *
  109. * @return mixed The parameter value
  110. *
  111. * @throws InvalidArgumentException if the parameter is not defined
  112. */
  113. public function getParameter($name)
  114. {
  115. return $this->parameterBag->get($name);
  116. }
  117. /**
  118. * Checks if a parameter exists.
  119. *
  120. * @param string $name The parameter name
  121. *
  122. * @return bool The presence of parameter in container
  123. */
  124. public function hasParameter($name)
  125. {
  126. return $this->parameterBag->has($name);
  127. }
  128. /**
  129. * Sets a parameter.
  130. *
  131. * @param string $name The parameter name
  132. * @param mixed $value The parameter value
  133. */
  134. public function setParameter($name, $value)
  135. {
  136. $this->parameterBag->set($name, $value);
  137. }
  138. /**
  139. * Sets a service.
  140. *
  141. * Setting a service to null resets the service: has() returns false and get()
  142. * behaves in the same way as if the service was never created.
  143. *
  144. * @param string $id The service identifier
  145. * @param object $service The service instance
  146. */
  147. public function set($id, $service)
  148. {
  149. $id = strtolower($id);
  150. if ('service_container' === $id) {
  151. throw new InvalidArgumentException('You cannot set service "service_container".');
  152. }
  153. if (isset($this->aliases[$id])) {
  154. unset($this->aliases[$id]);
  155. }
  156. $this->services[$id] = $service;
  157. if (null === $service) {
  158. unset($this->services[$id]);
  159. }
  160. }
  161. /**
  162. * Returns true if the given service is defined.
  163. *
  164. * @param string $id The service identifier
  165. *
  166. * @return bool true if the service is defined, false otherwise
  167. */
  168. public function has($id)
  169. {
  170. for ($i = 2;;) {
  171. if ('service_container' === $id
  172. || isset($this->aliases[$id])
  173. || isset($this->services[$id])
  174. ) {
  175. return true;
  176. }
  177. if (--$i && $id !== $lcId = strtolower($id)) {
  178. $id = $lcId;
  179. } else {
  180. return method_exists($this, 'get'.strtr($id, $this->underscoreMap).'Service');
  181. }
  182. }
  183. }
  184. /**
  185. * Gets a service.
  186. *
  187. * If a service is defined both through a set() method and
  188. * with a get{$id}Service() method, the former has always precedence.
  189. *
  190. * @param string $id The service identifier
  191. * @param int $invalidBehavior The behavior when the service does not exist
  192. *
  193. * @return object The associated service
  194. *
  195. * @throws ServiceCircularReferenceException When a circular reference is detected
  196. * @throws ServiceNotFoundException When the service is not defined
  197. * @throws \Exception if an exception has been thrown when the service has been resolved
  198. *
  199. * @see Reference
  200. */
  201. public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
  202. {
  203. // Attempt to retrieve the service by checking first aliases then
  204. // available services. Service IDs are case insensitive, however since
  205. // this method can be called thousands of times during a request, avoid
  206. // calling strtolower() unless necessary.
  207. for ($i = 2;;) {
  208. if ('service_container' === $id) {
  209. return $this;
  210. }
  211. if (isset($this->aliases[$id])) {
  212. $id = $this->aliases[$id];
  213. }
  214. // Re-use shared service instance if it exists.
  215. if (isset($this->services[$id])) {
  216. return $this->services[$id];
  217. }
  218. if (isset($this->loading[$id])) {
  219. throw new ServiceCircularReferenceException($id, array_keys($this->loading));
  220. }
  221. if (isset($this->methodMap[$id])) {
  222. $method = $this->methodMap[$id];
  223. } elseif (--$i && $id !== $lcId = strtolower($id)) {
  224. $id = $lcId;
  225. continue;
  226. } elseif (method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) {
  227. // $method is set to the right value, proceed
  228. } else {
  229. if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
  230. if (!$id) {
  231. throw new ServiceNotFoundException($id);
  232. }
  233. $alternatives = array();
  234. foreach ($this->getServiceIds() as $knownId) {
  235. $lev = levenshtein($id, $knownId);
  236. if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) {
  237. $alternatives[] = $knownId;
  238. }
  239. }
  240. throw new ServiceNotFoundException($id, null, null, $alternatives);
  241. }
  242. return;
  243. }
  244. $this->loading[$id] = true;
  245. try {
  246. $service = $this->$method();
  247. } catch (\Exception $e) {
  248. unset($this->services[$id]);
  249. throw $e;
  250. } finally {
  251. unset($this->loading[$id]);
  252. }
  253. return $service;
  254. }
  255. }
  256. /**
  257. * Returns true if the given service has actually been initialized.
  258. *
  259. * @param string $id The service identifier
  260. *
  261. * @return bool true if service has already been initialized, false otherwise
  262. */
  263. public function initialized($id)
  264. {
  265. $id = strtolower($id);
  266. if ('service_container' === $id) {
  267. return false;
  268. }
  269. if (isset($this->aliases[$id])) {
  270. $id = $this->aliases[$id];
  271. }
  272. return isset($this->services[$id]);
  273. }
  274. /**
  275. * {@inheritdoc}
  276. */
  277. public function reset()
  278. {
  279. $this->services = array();
  280. }
  281. /**
  282. * Gets all service ids.
  283. *
  284. * @return array An array of all defined service ids
  285. */
  286. public function getServiceIds()
  287. {
  288. $ids = array();
  289. foreach (get_class_methods($this) as $method) {
  290. if (preg_match('/^get(.+)Service$/', $method, $match)) {
  291. $ids[] = self::underscore($match[1]);
  292. }
  293. }
  294. $ids[] = 'service_container';
  295. return array_unique(array_merge($ids, array_keys($this->services)));
  296. }
  297. /**
  298. * Camelizes a string.
  299. *
  300. * @param string $id A string to camelize
  301. *
  302. * @return string The camelized string
  303. */
  304. public static function camelize($id)
  305. {
  306. return strtr(ucwords(strtr($id, array('_' => ' ', '.' => '_ ', '\\' => '_ '))), array(' ' => ''));
  307. }
  308. /**
  309. * A string to underscore.
  310. *
  311. * @param string $id The string to underscore
  312. *
  313. * @return string The underscored string
  314. */
  315. public static function underscore($id)
  316. {
  317. return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), str_replace('_', '.', $id)));
  318. }
  319. private function __clone()
  320. {
  321. }
  322. }