Container.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace Symfony\Component\DependencyInjection;
  3. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  5. use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
  6. /*
  7. * This file is part of the Symfony framework.
  8. *
  9. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  10. *
  11. * This source file is subject to the MIT license that is bundled
  12. * with this source code in the file LICENSE.
  13. */
  14. /**
  15. * Container is a dependency injection container.
  16. *
  17. * It gives access to object instances (services).
  18. *
  19. * Services and parameters are simple key/pair stores.
  20. *
  21. * Parameter and service keys are case insensitive.
  22. *
  23. * A service id can contain lowercased letters, digits, underscores, and dots.
  24. * Underscores are used to separate words, and dots to group services
  25. * under namespaces:
  26. *
  27. * <ul>
  28. * <li>request</li>
  29. * <li>mysql_session_storage</li>
  30. * <li>symfony.mysql_session_storage</li>
  31. * </ul>
  32. *
  33. * A service can also be defined by creating a method named
  34. * getXXXService(), where XXX is the camelized version of the id:
  35. *
  36. * <ul>
  37. * <li>request -> getRequestService()</li>
  38. * <li>mysql_session_storage -> getMysqlSessionStorageService()</li>
  39. * <li>symfony.mysql_session_storage -> getSymfony_MysqlSessionStorageService()</li>
  40. * </ul>
  41. *
  42. * The container can have three possible behaviors when a service does not exist:
  43. *
  44. * * EXCEPTION_ON_INVALID_REFERENCE: Throws an exception (the default)
  45. * * NULL_ON_INVALID_REFERENCE: Returns null
  46. * * IGNORE_ON_INVALID_REFERENCE: Ignores the wrapping command asking for the reference
  47. * (for instance, ignore a setter if the service does not exist)
  48. *
  49. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  50. */
  51. class Container implements ContainerInterface
  52. {
  53. protected $parameterBag;
  54. protected $services;
  55. /**
  56. * Constructor.
  57. *
  58. * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
  59. */
  60. public function __construct(ParameterBagInterface $parameterBag = null)
  61. {
  62. $this->parameterBag = null === $parameterBag ? new ParameterBag() : $parameterBag;
  63. $this->services = array();
  64. $this->set('service_container', $this);
  65. }
  66. /**
  67. * Freezes the container.
  68. *
  69. * This method does two things:
  70. *
  71. * * Parameter values are resolved;
  72. * * The parameter bag is freezed.
  73. */
  74. public function freeze()
  75. {
  76. $this->parameterBag->resolve();
  77. $this->parameterBag = new FrozenParameterBag($this->parameterBag->all());
  78. }
  79. /**
  80. * Returns true if the container parameter bag are frozen.
  81. *
  82. * @return Boolean true if the container parameter bag are frozen, false otherwise
  83. */
  84. public function isFrozen()
  85. {
  86. return $this->parameterBag instanceof FrozenParameterBag;
  87. }
  88. /**
  89. * Gets the service container parameter bag.
  90. *
  91. * @return ParameterBagInterface A ParameterBagInterface instance
  92. */
  93. public function getParameterBag()
  94. {
  95. return $this->parameterBag;
  96. }
  97. /**
  98. * Gets a parameter.
  99. *
  100. * @param string $name The parameter name
  101. *
  102. * @return mixed The parameter value
  103. *
  104. * @throws \InvalidArgumentException if the parameter is not defined
  105. */
  106. public function getParameter($name)
  107. {
  108. return $this->parameterBag->get($name);
  109. }
  110. /**
  111. * Checks if a parameter exists.
  112. *
  113. * @param string $name The parameter name
  114. *
  115. * @return boolean The presence of parameter in container
  116. */
  117. public function hasParameter($name)
  118. {
  119. return $this->parameterBag->has($name);
  120. }
  121. /**
  122. * Sets a parameter.
  123. *
  124. * @param string $name The parameter name
  125. * @param mixed $parameters The parameter value
  126. */
  127. public function setParameter($name, $value)
  128. {
  129. $this->parameterBag->set($name, $value);
  130. }
  131. /**
  132. * Sets a service.
  133. *
  134. * @param string $id The service identifier
  135. * @param object $service The service instance
  136. */
  137. public function set($id, $service)
  138. {
  139. $this->services[strtolower($id)] = $service;
  140. }
  141. /**
  142. * Returns true if the given service is defined.
  143. *
  144. * @param string $id The service identifier
  145. *
  146. * @return Boolean true if the service is defined, false otherwise
  147. */
  148. public function has($id)
  149. {
  150. $id = strtolower($id);
  151. return isset($this->services[$id]) || method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service');
  152. }
  153. /**
  154. * Gets a service.
  155. *
  156. * If a service is both defined through a set() method and
  157. * with a set*Service() method, the former has always precedence.
  158. *
  159. * @param string $id The service identifier
  160. * @param int $invalidBehavior The behavior when the service does not exist
  161. *
  162. * @return object The associated service
  163. *
  164. * @throws \InvalidArgumentException if the service is not defined
  165. *
  166. * @see Reference
  167. */
  168. public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
  169. {
  170. static $loading = array();
  171. $id = strtolower($id);
  172. if (isset($this->services[$id])) {
  173. return $this->services[$id];
  174. }
  175. if (isset($loading[$id])) {
  176. throw new \LogicException(sprintf('Circular reference detected for service "%s" (services currently loading: %s).', $id, implode(', ', array_keys($loading))));
  177. }
  178. if (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_')).'Service')) {
  179. $loading[$id] = true;
  180. $service = $this->$method();
  181. unset($loading[$id]);
  182. return $service;
  183. }
  184. if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
  185. throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $id));
  186. }
  187. }
  188. /**
  189. * Gets all service ids.
  190. *
  191. * @return array An array of all defined service ids
  192. */
  193. public function getServiceIds()
  194. {
  195. $ids = array();
  196. $r = new \ReflectionClass($this);
  197. foreach ($r->getMethods() as $method) {
  198. if (preg_match('/^get(.+)Service$/', $name = $method->getName(), $match)) {
  199. $ids[] = self::underscore($match[1]);
  200. }
  201. }
  202. return array_merge($ids, array_keys($this->services));
  203. }
  204. static public function camelize($id)
  205. {
  206. return preg_replace(array('/(?:^|_)+(.)/e', '/\.(.)/e'), array("strtoupper('\\1')", "'_'.strtoupper('\\1')"), $id);
  207. }
  208. static public function underscore($id)
  209. {
  210. return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), strtr($id, '_', '.')));
  211. }
  212. }