/application/libraries/Zend/Tool/Framework/Provider/Repository.php

https://github.com/grandison/budo16 · PHP · 265 lines · 111 code · 37 blank · 117 comment · 17 complexity · 800ad8292ba4f4e49b141a8a259dcaa0 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-2009 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 16972 2009-07-22 18:44:24Z ralph $
  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-2009 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. // create a signature for the provided provider
  143. $providerSignature = new Zend_Tool_Framework_Provider_Signature($provider);
  144. if ($providerSignature instanceof Zend_Tool_Framework_Registry_EnabledInterface) {
  145. $providerSignature->setRegistry($this->_registry);
  146. }
  147. $providerSignature->process();
  148. // ensure the name is lowercased for easier searching
  149. $providerName = strtolower($providerName);
  150. // add to the appropraite place
  151. $this->_providerSignatures[$providerName] = $providerSignature;
  152. $this->_providers[$providerName] = $providerSignature->getProvider();
  153. // remove from unprocessed array
  154. unset($this->_unprocessedProviders[$providerName]);
  155. }
  156. }
  157. /**
  158. * getProviders() Get all the providers in the repository
  159. *
  160. * @return array
  161. */
  162. public function getProviders()
  163. {
  164. return $this->_providers;
  165. }
  166. /**
  167. * getProviderSignatures() Get all the provider signatures
  168. *
  169. * @return array
  170. */
  171. public function getProviderSignatures()
  172. {
  173. return $this->_providerSignatures;
  174. }
  175. /**
  176. * getProvider()
  177. *
  178. * @param string $providerName
  179. * @return Zend_Tool_Framework_Provider_Interface
  180. */
  181. public function getProvider($providerName)
  182. {
  183. return $this->_providers[strtolower($providerName)];
  184. }
  185. /**
  186. * getProviderSignature()
  187. *
  188. * @param string $providerName
  189. * @return Zend_Tool_Framework_Provider_Signature
  190. */
  191. public function getProviderSignature($providerName)
  192. {
  193. return $this->_providerSignatures[strtolower($providerName)];
  194. }
  195. /**
  196. * count() - return the number of providers
  197. *
  198. * @return int
  199. */
  200. public function count()
  201. {
  202. return count($this->_providers);
  203. }
  204. /**
  205. * getIterator() - Required by the IteratorAggregate Interface
  206. *
  207. * @return ArrayIterator
  208. */
  209. public function getIterator()
  210. {
  211. return new ArrayIterator($this->getProviders());
  212. }
  213. /**
  214. * _parseName - internal method to determine the name of an action when one is not explicity provided.
  215. *
  216. * @param Zend_Tool_Framework_Action_Interface $action
  217. * @return string
  218. */
  219. protected function _parseName(Zend_Tool_Framework_Provider_Interface $provider)
  220. {
  221. $className = get_class($provider);
  222. $providerName = substr($className, strrpos($className, '_')+1);
  223. if (substr($providerName, -8) == 'Provider') {
  224. $providerName = substr($providerName, 0, strlen($providerName)-8);
  225. }
  226. return $providerName;
  227. }
  228. }