PageRenderTime 33ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/user/plugins/email/vendor/swiftmailer/swiftmailer/lib/classes/Swift/DependencyContainer.php

https://gitlab.com/3dplex/3d-plex-main-site
PHP | 448 lines | 232 code | 47 blank | 169 comment | 14 complexity | 8f337cfccd9a8e5b2c87d057e9dd823c MD5 | raw file
  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. *
  12. <<<<<<< HEAD
  13. * @package Swift
  14. * @author Chris Corbyn
  15. =======
  16. * @author Chris Corbyn
  17. >>>>>>> update grav cms
  18. */
  19. class Swift_DependencyContainer
  20. {
  21. /** Constant for literal value types */
  22. const TYPE_VALUE = 0x0001;
  23. /** Constant for new instance types */
  24. const TYPE_INSTANCE = 0x0010;
  25. /** Constant for shared instance types */
  26. const TYPE_SHARED = 0x0100;
  27. /** Constant for aliases */
  28. const TYPE_ALIAS = 0x1000;
  29. /** Singleton instance */
  30. private static $_instance = null;
  31. /** The data container */
  32. private $_store = array();
  33. /** The current endpoint in the data container */
  34. private $_endPoint;
  35. /**
  36. * Constructor should not be used.
  37. *
  38. * Use {@link getInstance()} instead.
  39. */
  40. <<<<<<< HEAD
  41. public function __construct() { }
  42. =======
  43. public function __construct()
  44. {
  45. }
  46. >>>>>>> update grav cms
  47. /**
  48. * Returns a singleton of the DependencyContainer.
  49. *
  50. * @return Swift_DependencyContainer
  51. */
  52. public static function getInstance()
  53. {
  54. if (!isset(self::$_instance)) {
  55. self::$_instance = new self();
  56. }
  57. return self::$_instance;
  58. }
  59. /**
  60. * List the names of all items stored in the Container.
  61. *
  62. * @return array
  63. */
  64. public function listItems()
  65. {
  66. return array_keys($this->_store);
  67. }
  68. /**
  69. * Test if an item is registered in this container with the given name.
  70. *
  71. * @see register()
  72. *
  73. * @param string $itemName
  74. *
  75. <<<<<<< HEAD
  76. * @return boolean
  77. =======
  78. * @return bool
  79. >>>>>>> update grav cms
  80. */
  81. public function has($itemName)
  82. {
  83. return array_key_exists($itemName, $this->_store)
  84. && isset($this->_store[$itemName]['lookupType']);
  85. }
  86. /**
  87. * Lookup the item with the given $itemName.
  88. *
  89. * @see register()
  90. *
  91. * @param string $itemName
  92. *
  93. <<<<<<< HEAD
  94. * @return mixed
  95. *
  96. * @throws Swift_DependencyException If the dependency is not found
  97. =======
  98. * @throws Swift_DependencyException If the dependency is not found
  99. *
  100. * @return mixed
  101. >>>>>>> update grav cms
  102. */
  103. public function lookup($itemName)
  104. {
  105. if (!$this->has($itemName)) {
  106. throw new Swift_DependencyException(
  107. <<<<<<< HEAD
  108. 'Cannot lookup dependency "' . $itemName . '" since it is not registered.'
  109. =======
  110. 'Cannot lookup dependency "'.$itemName.'" since it is not registered.'
  111. >>>>>>> update grav cms
  112. );
  113. }
  114. switch ($this->_store[$itemName]['lookupType']) {
  115. case self::TYPE_ALIAS:
  116. return $this->_createAlias($itemName);
  117. case self::TYPE_VALUE:
  118. return $this->_getValue($itemName);
  119. case self::TYPE_INSTANCE:
  120. return $this->_createNewInstance($itemName);
  121. case self::TYPE_SHARED:
  122. return $this->_createSharedInstance($itemName);
  123. }
  124. }
  125. /**
  126. * Create an array of arguments passed to the constructor of $itemName.
  127. *
  128. * @param string $itemName
  129. *
  130. * @return array
  131. */
  132. public function createDependenciesFor($itemName)
  133. {
  134. $args = array();
  135. if (isset($this->_store[$itemName]['args'])) {
  136. $args = $this->_resolveArgs($this->_store[$itemName]['args']);
  137. }
  138. return $args;
  139. }
  140. /**
  141. * Register a new dependency with $itemName.
  142. *
  143. * This method returns the current DependencyContainer instance because it
  144. * requires the use of the fluid interface to set the specific details for the
  145. * dependency.
  146. <<<<<<< HEAD
  147. =======
  148. *
  149. >>>>>>> update grav cms
  150. * @see asNewInstanceOf(), asSharedInstanceOf(), asValue()
  151. *
  152. * @param string $itemName
  153. *
  154. * @return Swift_DependencyContainer
  155. */
  156. public function register($itemName)
  157. {
  158. $this->_store[$itemName] = array();
  159. <<<<<<< HEAD
  160. $this->_endPoint =& $this->_store[$itemName];
  161. =======
  162. $this->_endPoint = &$this->_store[$itemName];
  163. >>>>>>> update grav cms
  164. return $this;
  165. }
  166. /**
  167. * Specify the previously registered item as a literal value.
  168. *
  169. * {@link register()} must be called before this will work.
  170. *
  171. * @param mixed $value
  172. *
  173. * @return Swift_DependencyContainer
  174. */
  175. public function asValue($value)
  176. {
  177. <<<<<<< HEAD
  178. $endPoint =& $this->_getEndPoint();
  179. =======
  180. $endPoint = &$this->_getEndPoint();
  181. >>>>>>> update grav cms
  182. $endPoint['lookupType'] = self::TYPE_VALUE;
  183. $endPoint['value'] = $value;
  184. return $this;
  185. }
  186. /**
  187. * Specify the previously registered item as an alias of another item.
  188. *
  189. * @param string $lookup
  190. *
  191. * @return Swift_DependencyContainer
  192. */
  193. public function asAliasOf($lookup)
  194. {
  195. <<<<<<< HEAD
  196. $endPoint =& $this->_getEndPoint();
  197. =======
  198. $endPoint = &$this->_getEndPoint();
  199. >>>>>>> update grav cms
  200. $endPoint['lookupType'] = self::TYPE_ALIAS;
  201. $endPoint['ref'] = $lookup;
  202. return $this;
  203. }
  204. /**
  205. * Specify the previously registered item as a new instance of $className.
  206. *
  207. * {@link register()} must be called before this will work.
  208. * Any arguments can be set with {@link withDependencies()},
  209. * {@link addConstructorValue()} or {@link addConstructorLookup()}.
  210. *
  211. * @see withDependencies(), addConstructorValue(), addConstructorLookup()
  212. *
  213. * @param string $className
  214. *
  215. * @return Swift_DependencyContainer
  216. */
  217. public function asNewInstanceOf($className)
  218. {
  219. <<<<<<< HEAD
  220. $endPoint =& $this->_getEndPoint();
  221. =======
  222. $endPoint = &$this->_getEndPoint();
  223. >>>>>>> update grav cms
  224. $endPoint['lookupType'] = self::TYPE_INSTANCE;
  225. $endPoint['className'] = $className;
  226. return $this;
  227. }
  228. /**
  229. * Specify the previously registered item as a shared instance of $className.
  230. *
  231. * {@link register()} must be called before this will work.
  232. *
  233. * @param string $className
  234. *
  235. * @return Swift_DependencyContainer
  236. */
  237. public function asSharedInstanceOf($className)
  238. {
  239. <<<<<<< HEAD
  240. $endPoint =& $this->_getEndPoint();
  241. =======
  242. $endPoint = &$this->_getEndPoint();
  243. >>>>>>> update grav cms
  244. $endPoint['lookupType'] = self::TYPE_SHARED;
  245. $endPoint['className'] = $className;
  246. return $this;
  247. }
  248. /**
  249. * Specify a list of injected dependencies for the previously registered item.
  250. *
  251. * This method takes an array of lookup names.
  252. *
  253. * @see addConstructorValue(), addConstructorLookup()
  254. *
  255. * @param array $lookups
  256. *
  257. * @return Swift_DependencyContainer
  258. */
  259. public function withDependencies(array $lookups)
  260. {
  261. <<<<<<< HEAD
  262. $endPoint =& $this->_getEndPoint();
  263. =======
  264. $endPoint = &$this->_getEndPoint();
  265. >>>>>>> update grav cms
  266. $endPoint['args'] = array();
  267. foreach ($lookups as $lookup) {
  268. $this->addConstructorLookup($lookup);
  269. }
  270. return $this;
  271. }
  272. /**
  273. * Specify a literal (non looked up) value for the constructor of the
  274. * previously registered item.
  275. *
  276. * @see withDependencies(), addConstructorLookup()
  277. *
  278. * @param mixed $value
  279. *
  280. * @return Swift_DependencyContainer
  281. */
  282. public function addConstructorValue($value)
  283. {
  284. <<<<<<< HEAD
  285. $endPoint =& $this->_getEndPoint();
  286. =======
  287. $endPoint = &$this->_getEndPoint();
  288. >>>>>>> update grav cms
  289. if (!isset($endPoint['args'])) {
  290. $endPoint['args'] = array();
  291. }
  292. $endPoint['args'][] = array('type' => 'value', 'item' => $value);
  293. return $this;
  294. }
  295. /**
  296. * Specify a dependency lookup for the constructor of the previously
  297. * registered item.
  298. *
  299. * @see withDependencies(), addConstructorValue()
  300. *
  301. * @param string $lookup
  302. *
  303. * @return Swift_DependencyContainer
  304. */
  305. public function addConstructorLookup($lookup)
  306. {
  307. <<<<<<< HEAD
  308. $endPoint =& $this->_getEndPoint();
  309. =======
  310. $endPoint = &$this->_getEndPoint();
  311. >>>>>>> update grav cms
  312. if (!isset($this->_endPoint['args'])) {
  313. $endPoint['args'] = array();
  314. }
  315. $endPoint['args'][] = array('type' => 'lookup', 'item' => $lookup);
  316. return $this;
  317. }
  318. <<<<<<< HEAD
  319. // -- Private methods
  320. =======
  321. >>>>>>> update grav cms
  322. /** Get the literal value with $itemName */
  323. private function _getValue($itemName)
  324. {
  325. return $this->_store[$itemName]['value'];
  326. }
  327. /** Resolve an alias to another item */
  328. private function _createAlias($itemName)
  329. {
  330. return $this->lookup($this->_store[$itemName]['ref']);
  331. }
  332. /** Create a fresh instance of $itemName */
  333. private function _createNewInstance($itemName)
  334. {
  335. $reflector = new ReflectionClass($this->_store[$itemName]['className']);
  336. if ($reflector->getConstructor()) {
  337. return $reflector->newInstanceArgs(
  338. $this->createDependenciesFor($itemName)
  339. );
  340. <<<<<<< HEAD
  341. } else {
  342. return $reflector->newInstance();
  343. }
  344. =======
  345. }
  346. return $reflector->newInstance();
  347. >>>>>>> update grav cms
  348. }
  349. /** Create and register a shared instance of $itemName */
  350. private function _createSharedInstance($itemName)
  351. {
  352. if (!isset($this->_store[$itemName]['instance'])) {
  353. $this->_store[$itemName]['instance'] = $this->_createNewInstance($itemName);
  354. }
  355. return $this->_store[$itemName]['instance'];
  356. }
  357. /** Get the current endpoint in the store */
  358. private function &_getEndPoint()
  359. {
  360. if (!isset($this->_endPoint)) {
  361. throw new BadMethodCallException(
  362. 'Component must first be registered by calling register()'
  363. );
  364. }
  365. return $this->_endPoint;
  366. }
  367. /** Get an argument list with dependencies resolved */
  368. private function _resolveArgs(array $args)
  369. {
  370. $resolved = array();
  371. foreach ($args as $argDefinition) {
  372. switch ($argDefinition['type']) {
  373. case 'lookup':
  374. $resolved[] = $this->_lookupRecursive($argDefinition['item']);
  375. break;
  376. case 'value':
  377. $resolved[] = $argDefinition['item'];
  378. break;
  379. }
  380. }
  381. return $resolved;
  382. }
  383. /** Resolve a single dependency with an collections */
  384. private function _lookupRecursive($item)
  385. {
  386. if (is_array($item)) {
  387. $collection = array();
  388. foreach ($item as $k => $v) {
  389. $collection[$k] = $this->_lookupRecursive($v);
  390. }
  391. return $collection;
  392. <<<<<<< HEAD
  393. } else {
  394. return $this->lookup($item);
  395. }
  396. =======
  397. }
  398. return $this->lookup($item);
  399. >>>>>>> update grav cms
  400. }
  401. }