PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Tool/Framework/Provider/Repository.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 275 lines | 119 code | 39 blank | 117 comment | 19 complexity | 06ac616d5eca5d9769ad7d1d1a7ca9bc MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Tool
  17. * @subpackage Framework
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Tool_Framework_Provider_Signature
  24. */
  25. #require_once 'Zend/Tool/Framework/Provider/Signature.php';
  26. /**
  27. * @see Zend_Tool_Framework_Registry_EnabledInterface
  28. */
  29. #require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Tool
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Tool_Framework_Provider_Repository
  37. implements Zend_Tool_Framework_Registry_EnabledInterface, IteratorAggregate, Countable
  38. {
  39. /**
  40. * @var Zend_Tool_Framework_Registry
  41. */
  42. protected $_registry = null;
  43. /**
  44. * @var bool
  45. */
  46. protected $_processOnAdd = false;
  47. /**
  48. * @var Zend_Tool_Framework_Provider_Interface[]
  49. */
  50. protected $_unprocessedProviders = array();
  51. /**
  52. * @var Zend_Tool_Framework_Provider_Signature[]
  53. */
  54. protected $_providerSignatures = array();
  55. /**
  56. * @var array Array of Zend_Tool_Framework_Provider_Inteface
  57. */
  58. protected $_providers = array();
  59. /**
  60. * setRegistry()
  61. *
  62. * @param Zend_Tool_Framework_Registry_Interface $registry
  63. * @return unknown
  64. */
  65. public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
  66. {
  67. $this->_registry = $registry;
  68. return $this;
  69. }
  70. /**
  71. * Set the ProcessOnAdd flag
  72. *
  73. * @param unknown_type $processOnAdd
  74. * @return unknown
  75. */
  76. public function setProcessOnAdd($processOnAdd = true)
  77. {
  78. $this->_processOnAdd = (bool) $processOnAdd;
  79. return $this;
  80. }
  81. /**
  82. * Add a provider to the repository for processing
  83. *
  84. * @param Zend_Tool_Framework_Provider_Interface $provider
  85. * @return Zend_Tool_Framework_Provider_Repository
  86. */
  87. public function addProvider(Zend_Tool_Framework_Provider_Interface $provider, $overwriteExistingProvider = false)
  88. {
  89. if ($provider instanceof Zend_Tool_Framework_Registry_EnabledInterface) {
  90. $provider->setRegistry($this->_registry);
  91. }
  92. if (method_exists($provider, 'getName')) {
  93. $providerName = $provider->getName();
  94. } else {
  95. $providerName = $this->_parseName($provider);
  96. }
  97. // if a provider by the given name already exist, and its not set as overwritable, throw exception
  98. if (!$overwriteExistingProvider &&
  99. (array_key_exists($providerName, $this->_unprocessedProviders)
  100. || array_key_exists($providerName, $this->_providers)))
  101. {
  102. #require_once 'Zend/Tool/Framework/Provider/Exception.php';
  103. throw new Zend_Tool_Framework_Provider_Exception('A provider by the name ' . $providerName
  104. . ' is already registered and $overrideExistingProvider is set to false.');
  105. }
  106. $this->_unprocessedProviders[$providerName] = $provider;
  107. // if process has already been called, process immediately.
  108. if ($this->_processOnAdd) {
  109. $this->process();
  110. }
  111. return $this;
  112. }
  113. public function hasProvider($providerOrClassName, $processedOnly = true)
  114. {
  115. if ($providerOrClassName instanceof Zend_Tool_Framework_Provider_Interface) {
  116. $targetProviderClassName = get_class($providerOrClassName);
  117. } else {
  118. $targetProviderClassName = (string) $providerOrClassName;
  119. }
  120. if (!$processedOnly) {
  121. foreach ($this->_unprocessedProviders as $unprocessedProvider) {
  122. if (get_class($unprocessedProvider) == $targetProviderClassName) {
  123. return true;
  124. }
  125. }
  126. }
  127. foreach ($this->_providers as $processedProvider) {
  128. if (get_class($processedProvider) == $targetProviderClassName) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. /**
  135. * Process all of the unprocessed providers
  136. *
  137. */
  138. public function process()
  139. {
  140. // process all providers in the unprocessedProviders array
  141. //foreach ($this->_unprocessedProviders as $providerName => $provider) {
  142. reset($this->_unprocessedProviders);
  143. while ($this->_unprocessedProviders) {
  144. $providerName = key($this->_unprocessedProviders);
  145. $provider = array_shift($this->_unprocessedProviders);
  146. // create a signature for the provided provider
  147. $providerSignature = new Zend_Tool_Framework_Provider_Signature($provider);
  148. if ($providerSignature instanceof Zend_Tool_Framework_Registry_EnabledInterface) {
  149. $providerSignature->setRegistry($this->_registry);
  150. }
  151. $providerSignature->process();
  152. // ensure the name is lowercased for easier searching
  153. $providerName = strtolower($providerName);
  154. // add to the appropraite place
  155. $this->_providerSignatures[$providerName] = $providerSignature;
  156. $this->_providers[$providerName] = $providerSignature->getProvider();
  157. if ($provider instanceof Zend_Tool_Framework_Provider_Initializable) {
  158. $provider->initialize();
  159. }
  160. }
  161. }
  162. /**
  163. * getProviders() Get all the providers in the repository
  164. *
  165. * @return array
  166. */
  167. public function getProviders()
  168. {
  169. return $this->_providers;
  170. }
  171. /**
  172. * getProviderSignatures() Get all the provider signatures
  173. *
  174. * @return array
  175. */
  176. public function getProviderSignatures()
  177. {
  178. return $this->_providerSignatures;
  179. }
  180. /**
  181. * getProvider()
  182. *
  183. * @param string $providerName
  184. * @return Zend_Tool_Framework_Provider_Interface
  185. */
  186. public function getProvider($providerName)
  187. {
  188. return $this->_providers[strtolower($providerName)];
  189. }
  190. /**
  191. * getProviderSignature()
  192. *
  193. * @param string $providerName
  194. * @return Zend_Tool_Framework_Provider_Signature
  195. */
  196. public function getProviderSignature($providerName)
  197. {
  198. return $this->_providerSignatures[strtolower($providerName)];
  199. }
  200. /**
  201. * count() - return the number of providers
  202. *
  203. * @return int
  204. */
  205. public function count()
  206. {
  207. return count($this->_providers);
  208. }
  209. /**
  210. * getIterator() - Required by the IteratorAggregate Interface
  211. *
  212. * @return ArrayIterator
  213. */
  214. public function getIterator()
  215. {
  216. return new ArrayIterator($this->getProviders());
  217. }
  218. /**
  219. * _parseName - internal method to determine the name of an action when one is not explicity provided.
  220. *
  221. * @param Zend_Tool_Framework_Action_Interface $action
  222. * @return string
  223. */
  224. protected function _parseName(Zend_Tool_Framework_Provider_Interface $provider)
  225. {
  226. $className = get_class($provider);
  227. $providerName = $className;
  228. if (strpos($providerName, '_') !== false) {
  229. $providerName = substr($providerName, strrpos($providerName, '_')+1);
  230. }
  231. if (substr($providerName, -8) == 'Provider') {
  232. $providerName = substr($providerName, 0, strlen($providerName)-8);
  233. }
  234. return $providerName;
  235. }
  236. }