PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Tool/Framework/Provider/Repository.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 284 lines | 122 code | 42 blank | 120 comment | 19 complexity | 00a80b702ebc989ae66350f43661c487 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Repository.php 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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. *
  64. * @return unknown
  65. */
  66. public function setRegistry (Zend_Tool_Framework_Registry_Interface $registry)
  67. {
  68. $this->_registry = $registry;
  69. return $this;
  70. }
  71. /**
  72. * Set the ProcessOnAdd flag
  73. *
  74. * @param unknown_type $processOnAdd
  75. *
  76. * @return unknown
  77. */
  78. public function setProcessOnAdd ($processOnAdd = true)
  79. {
  80. $this->_processOnAdd = (bool)$processOnAdd;
  81. return $this;
  82. }
  83. /**
  84. * Add a provider to the repository for processing
  85. *
  86. * @param Zend_Tool_Framework_Provider_Interface $provider
  87. *
  88. * @return Zend_Tool_Framework_Provider_Repository
  89. */
  90. public function addProvider (Zend_Tool_Framework_Provider_Interface $provider, $overwriteExistingProvider = false)
  91. {
  92. if ($provider instanceof Zend_Tool_Framework_Registry_EnabledInterface) {
  93. $provider->setRegistry($this->_registry);
  94. }
  95. if (method_exists($provider, 'getName')) {
  96. $providerName = $provider->getName();
  97. } else {
  98. $providerName = $this->_parseName($provider);
  99. }
  100. // if a provider by the given name already exist, and its not set as overwritable, throw exception
  101. if (!$overwriteExistingProvider &&
  102. ( array_key_exists($providerName, $this->_unprocessedProviders)
  103. || array_key_exists($providerName, $this->_providers) )
  104. ) {
  105. require_once 'Zend/Tool/Framework/Provider/Exception.php';
  106. throw new Zend_Tool_Framework_Provider_Exception( 'A provider by the name ' . $providerName
  107. . ' is already registered and $overrideExistingProvider is set to false.' );
  108. }
  109. $this->_unprocessedProviders[$providerName] = $provider;
  110. // if process has already been called, process immediately.
  111. if ($this->_processOnAdd) {
  112. $this->process();
  113. }
  114. return $this;
  115. }
  116. public function hasProvider ($providerOrClassName, $processedOnly = true)
  117. {
  118. if ($providerOrClassName instanceof Zend_Tool_Framework_Provider_Interface) {
  119. $targetProviderClassName = get_class($providerOrClassName);
  120. } else {
  121. $targetProviderClassName = (string)$providerOrClassName;
  122. }
  123. if (!$processedOnly) {
  124. foreach ($this->_unprocessedProviders as $unprocessedProvider) {
  125. if (get_class($unprocessedProvider) == $targetProviderClassName) {
  126. return true;
  127. }
  128. }
  129. }
  130. foreach ($this->_providers as $processedProvider) {
  131. if (get_class($processedProvider) == $targetProviderClassName) {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. /**
  138. * Process all of the unprocessed providers
  139. *
  140. */
  141. public function process ()
  142. {
  143. // process all providers in the unprocessedProviders array
  144. //foreach ($this->_unprocessedProviders as $providerName => $provider) {
  145. reset($this->_unprocessedProviders);
  146. while ($this->_unprocessedProviders) {
  147. $providerName = key($this->_unprocessedProviders);
  148. $provider = array_shift($this->_unprocessedProviders);
  149. // create a signature for the provided provider
  150. $providerSignature = new Zend_Tool_Framework_Provider_Signature( $provider );
  151. if ($providerSignature instanceof Zend_Tool_Framework_Registry_EnabledInterface) {
  152. $providerSignature->setRegistry($this->_registry);
  153. }
  154. $providerSignature->process();
  155. // ensure the name is lowercased for easier searching
  156. $providerName = strtolower($providerName);
  157. // add to the appropraite place
  158. $this->_providerSignatures[$providerName] = $providerSignature;
  159. $this->_providers[$providerName] = $providerSignature->getProvider();
  160. if ($provider instanceof Zend_Tool_Framework_Provider_Initializable) {
  161. $provider->initialize();
  162. }
  163. }
  164. }
  165. /**
  166. * getProviders() Get all the providers in the repository
  167. *
  168. * @return array
  169. */
  170. public function getProviders ()
  171. {
  172. return $this->_providers;
  173. }
  174. /**
  175. * getProviderSignatures() Get all the provider signatures
  176. *
  177. * @return array
  178. */
  179. public function getProviderSignatures ()
  180. {
  181. return $this->_providerSignatures;
  182. }
  183. /**
  184. * getProvider()
  185. *
  186. * @param string $providerName
  187. *
  188. * @return Zend_Tool_Framework_Provider_Interface
  189. */
  190. public function getProvider ($providerName)
  191. {
  192. return $this->_providers[strtolower($providerName)];
  193. }
  194. /**
  195. * getProviderSignature()
  196. *
  197. * @param string $providerName
  198. *
  199. * @return Zend_Tool_Framework_Provider_Signature
  200. */
  201. public function getProviderSignature ($providerName)
  202. {
  203. return $this->_providerSignatures[strtolower($providerName)];
  204. }
  205. /**
  206. * count() - return the number of providers
  207. *
  208. * @return int
  209. */
  210. public function count ()
  211. {
  212. return count($this->_providers);
  213. }
  214. /**
  215. * getIterator() - Required by the IteratorAggregate Interface
  216. *
  217. * @return ArrayIterator
  218. */
  219. public function getIterator ()
  220. {
  221. return new ArrayIterator( $this->getProviders() );
  222. }
  223. /**
  224. * _parseName - internal method to determine the name of an action when one is not explicity provided.
  225. *
  226. * @param Zend_Tool_Framework_Action_Interface $action
  227. *
  228. * @return string
  229. */
  230. protected function _parseName (Zend_Tool_Framework_Provider_Interface $provider)
  231. {
  232. $className = get_class($provider);
  233. $providerName = $className;
  234. if (strpos($providerName, '_') !== false) {
  235. $providerName = substr($providerName, strrpos($providerName, '_') + 1);
  236. }
  237. if (substr($providerName, -8) == 'Provider') {
  238. $providerName = substr($providerName, 0, strlen($providerName) - 8);
  239. }
  240. return $providerName;
  241. }
  242. }