DependencyContainer.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Dependency Injection container.
  11. * @package Swift
  12. * @author Chris Corbyn
  13. */
  14. class Swift_DependencyContainer
  15. {
  16. /** Constant for literal value types */
  17. const TYPE_VALUE = 0x0001;
  18. /** Constant for new instance types */
  19. const TYPE_INSTANCE = 0x0010;
  20. /** Constant for shared instance types */
  21. const TYPE_SHARED = 0x0100;
  22. /** Constant for aliases */
  23. const TYPE_ALIAS = 0x1000;
  24. /** Singleton instance */
  25. private static $_instance = null;
  26. /** The data container */
  27. private $_store = array();
  28. /** The current endpoint in the data container */
  29. private $_endPoint;
  30. /**
  31. * Constructor should not be used.
  32. * Use {@link getInstance()} instead.
  33. */
  34. public function __construct() { }
  35. /**
  36. * Returns a singleton of the DependencyContainer.
  37. * @return Swift_DependencyContainer
  38. */
  39. public static function getInstance()
  40. {
  41. if (!isset(self::$_instance)) {
  42. self::$_instance = new self();
  43. }
  44. return self::$_instance;
  45. }
  46. /**
  47. * List the names of all items stored in the Container.
  48. * @return array
  49. */
  50. public function listItems()
  51. {
  52. return array_keys($this->_store);
  53. }
  54. /**
  55. * Test if an item is registered in this container with the given name.
  56. * @param string $itemName
  57. * @return boolean
  58. * @see register()
  59. */
  60. public function has($itemName)
  61. {
  62. return array_key_exists($itemName, $this->_store)
  63. && isset($this->_store[$itemName]['lookupType']);
  64. }
  65. /**
  66. * Lookup the item with the given $itemName.
  67. * @param string $itemName
  68. * @return mixed
  69. * @throws Swift_DependencyException If the dependency is not found
  70. * @see register()
  71. */
  72. public function lookup($itemName)
  73. {
  74. if (!$this->has($itemName)) {
  75. throw new Swift_DependencyException(
  76. 'Cannot lookup dependency "' . $itemName . '" since it is not registered.'
  77. );
  78. }
  79. switch ($this->_store[$itemName]['lookupType']) {
  80. case self::TYPE_ALIAS:
  81. return $this->_createAlias($itemName);
  82. case self::TYPE_VALUE:
  83. return $this->_getValue($itemName);
  84. case self::TYPE_INSTANCE:
  85. return $this->_createNewInstance($itemName);
  86. case self::TYPE_SHARED:
  87. return $this->_createSharedInstance($itemName);
  88. }
  89. }
  90. /**
  91. * Create an array of arguments passed to the constructor of $itemName.
  92. * @param string $itemName
  93. * @return array
  94. */
  95. public function createDependenciesFor($itemName)
  96. {
  97. $args = array();
  98. if (isset($this->_store[$itemName]['args'])) {
  99. $args = $this->_resolveArgs($this->_store[$itemName]['args']);
  100. }
  101. return $args;
  102. }
  103. /**
  104. * Register a new dependency with $itemName.
  105. * This method returns the current DependencyContainer instance because it
  106. * requires the use of the fluid interface to set the specific details for the
  107. * dependency.
  108. *
  109. * @param string $itemName
  110. * @return Swift_DependencyContainer
  111. * @see asNewInstanceOf(), asSharedInstanceOf(), asValue()
  112. */
  113. public function register($itemName)
  114. {
  115. $this->_store[$itemName] = array();
  116. $this->_endPoint =& $this->_store[$itemName];
  117. return $this;
  118. }
  119. /**
  120. * Specify the previously registered item as a literal value.
  121. * {@link register()} must be called before this will work.
  122. *
  123. * @param mixed $value
  124. * @return Swift_DependencyContainer
  125. */
  126. public function asValue($value)
  127. {
  128. $endPoint =& $this->_getEndPoint();
  129. $endPoint['lookupType'] = self::TYPE_VALUE;
  130. $endPoint['value'] = $value;
  131. return $this;
  132. }
  133. /**
  134. * Specify the previously registered item as an alias of another item.
  135. * @param string $lookup
  136. * @return Swift_DependencyContainer
  137. */
  138. public function asAliasOf($lookup)
  139. {
  140. $endPoint =& $this->_getEndPoint();
  141. $endPoint['lookupType'] = self::TYPE_ALIAS;
  142. $endPoint['ref'] = $lookup;
  143. return $this;
  144. }
  145. /**
  146. * Specify the previously registered item as a new instance of $className.
  147. * {@link register()} must be called before this will work.
  148. * Any arguments can be set with {@link withDependencies()},
  149. * {@link addConstructorValue()} or {@link addConstructorLookup()}.
  150. *
  151. * @param string $className
  152. * @return Swift_DependencyContainer
  153. * @see withDependencies(), addConstructorValue(), addConstructorLookup()
  154. */
  155. public function asNewInstanceOf($className)
  156. {
  157. $endPoint =& $this->_getEndPoint();
  158. $endPoint['lookupType'] = self::TYPE_INSTANCE;
  159. $endPoint['className'] = $className;
  160. return $this;
  161. }
  162. /**
  163. * Specify the previously registered item as a shared instance of $className.
  164. * {@link register()} must be called before this will work.
  165. * @param string $className
  166. * @return Swift_DependencyContainer
  167. */
  168. public function asSharedInstanceOf($className)
  169. {
  170. $endPoint =& $this->_getEndPoint();
  171. $endPoint['lookupType'] = self::TYPE_SHARED;
  172. $endPoint['className'] = $className;
  173. return $this;
  174. }
  175. /**
  176. * Specify a list of injected dependencies for the previously registered item.
  177. * This method takes an array of lookup names.
  178. *
  179. * @param array $lookups
  180. * @return Swift_DependencyContainer
  181. * @see addConstructorValue(), addConstructorLookup()
  182. */
  183. public function withDependencies(array $lookups)
  184. {
  185. $endPoint =& $this->_getEndPoint();
  186. $endPoint['args'] = array();
  187. foreach ($lookups as $lookup) {
  188. $this->addConstructorLookup($lookup);
  189. }
  190. return $this;
  191. }
  192. /**
  193. * Specify a literal (non looked up) value for the constructor of the
  194. * previously registered item.
  195. *
  196. * @param mixed $value
  197. * @return Swift_DependencyContainer
  198. * @see withDependencies(), addConstructorLookup()
  199. */
  200. public function addConstructorValue($value)
  201. {
  202. $endPoint =& $this->_getEndPoint();
  203. if (!isset($endPoint['args'])) {
  204. $endPoint['args'] = array();
  205. }
  206. $endPoint['args'][] = array('type' => 'value', 'item' => $value);
  207. return $this;
  208. }
  209. /**
  210. * Specify a dependency lookup for the constructor of the previously
  211. * registered item.
  212. *
  213. * @param string $lookup
  214. * @return Swift_DependencyContainer
  215. * @see withDependencies(), addConstructorValue()
  216. */
  217. public function addConstructorLookup($lookup)
  218. {
  219. $endPoint =& $this->_getEndPoint();
  220. if (!isset($this->_endPoint['args'])) {
  221. $endPoint['args'] = array();
  222. }
  223. $endPoint['args'][] = array('type' => 'lookup', 'item' => $lookup);
  224. return $this;
  225. }
  226. // -- Private methods
  227. /** Get the literal value with $itemName */
  228. private function _getValue($itemName)
  229. {
  230. return $this->_store[$itemName]['value'];
  231. }
  232. /** Resolve an alias to another item */
  233. private function _createAlias($itemName)
  234. {
  235. return $this->lookup($this->_store[$itemName]['ref']);
  236. }
  237. /** Create a fresh instance of $itemName */
  238. private function _createNewInstance($itemName)
  239. {
  240. $reflector = new ReflectionClass($this->_store[$itemName]['className']);
  241. if ($reflector->getConstructor()) {
  242. return $reflector->newInstanceArgs(
  243. $this->createDependenciesFor($itemName)
  244. );
  245. } else {
  246. return $reflector->newInstance();
  247. }
  248. }
  249. /** Create and register a shared instance of $itemName */
  250. private function _createSharedInstance($itemName)
  251. {
  252. if (!isset($this->_store[$itemName]['instance'])) {
  253. $this->_store[$itemName]['instance'] = $this->_createNewInstance($itemName);
  254. }
  255. return $this->_store[$itemName]['instance'];
  256. }
  257. /** Get the current endpoint in the store */
  258. private function &_getEndPoint()
  259. {
  260. if (!isset($this->_endPoint)) {
  261. throw new BadMethodCallException(
  262. 'Component must first be registered by calling register()'
  263. );
  264. }
  265. return $this->_endPoint;
  266. }
  267. /** Get an argument list with dependencies resolved */
  268. private function _resolveArgs(array $args)
  269. {
  270. $resolved = array();
  271. foreach ($args as $argDefinition) {
  272. switch ($argDefinition['type']) {
  273. case 'lookup':
  274. $resolved[] = $this->_lookupRecursive($argDefinition['item']);
  275. break;
  276. case 'value':
  277. $resolved[] = $argDefinition['item'];
  278. break;
  279. }
  280. }
  281. return $resolved;
  282. }
  283. /** Resolve a single dependency with an collections */
  284. private function _lookupRecursive($item)
  285. {
  286. if (is_array($item)) {
  287. $collection = array();
  288. foreach ($item as $k => $v) {
  289. $collection[$k] = $this->_lookupRecursive($v);
  290. }
  291. return $collection;
  292. } else {
  293. return $this->lookup($item);
  294. }
  295. }
  296. }