PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/zendframework/zf2
PHP | 273 lines | 128 code | 35 blank | 110 comment | 17 complexity | ee201c6557bfc6f1fdab4d534292d7e3 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Tool\Framework\Provider;
  25. use Countable,
  26. IteratorAggregate,
  27. Zend\Tool\Framework\Provider,
  28. Zend\Tool\Framework\Registry,
  29. Zend\Tool\Framework\RegistryEnabled;
  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 Repository implements RegistryEnabled, IteratorAggregate, Countable
  37. {
  38. /**
  39. * @var Registry
  40. */
  41. protected $_registry = null;
  42. /**
  43. * @var bool
  44. */
  45. protected $_processOnAdd = false;
  46. /**
  47. * @var Provider[]
  48. */
  49. protected $_unprocessedProviders = array();
  50. /**
  51. * @var Provider\Signature[]
  52. */
  53. protected $_providerSignatures = array();
  54. /**
  55. * @var array Array of Provider\Inteface
  56. */
  57. protected $_providers = array();
  58. /**
  59. * setRegistry()
  60. *
  61. * @param Registry $registry
  62. * @return unknown
  63. */
  64. public function setRegistry(Registry $registry)
  65. {
  66. $this->_registry = $registry;
  67. return $this;
  68. }
  69. /**
  70. * Set the ProcessOnAdd flag
  71. *
  72. * @param unknown_type $processOnAdd
  73. * @return unknown
  74. */
  75. public function setProcessOnAdd($processOnAdd = true)
  76. {
  77. $this->_processOnAdd = (bool) $processOnAdd;
  78. return $this;
  79. }
  80. /**
  81. * Add a provider to the repository for processing
  82. *
  83. * @param Provider $provider
  84. * @return Provider\Repository
  85. */
  86. public function addProvider(Provider $provider, $overwriteExistingProvider = false)
  87. {
  88. if ($provider instanceof RegistryEnabled) {
  89. $provider->setRegistry($this->_registry);
  90. }
  91. if (method_exists($provider, 'getName')) {
  92. $providerName = $provider->getName();
  93. } else {
  94. $providerName = $this->_parseName($provider);
  95. }
  96. // if a provider by the given name already exist, and its not set as overwritable, throw exception
  97. if (!$overwriteExistingProvider &&
  98. (array_key_exists($providerName, $this->_unprocessedProviders)
  99. || array_key_exists($providerName, $this->_providers)))
  100. {
  101. throw new Exception\InvalidArgumentException(sprintf(
  102. 'A provider by the name "%s" is already registered and $overrideExistingProvider is set to false',
  103. $providerName
  104. ));
  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 Provider) {
  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 Signature($provider);
  148. if ($providerSignature instanceof RegistryEnabled) {
  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 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 Provider
  185. */
  186. public function getProvider($providerName)
  187. {
  188. return $this->_providers[strtolower($providerName)];
  189. }
  190. /**
  191. * getProviderSignature()
  192. *
  193. * @param string $providerName
  194. * @return 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 Provider $action
  222. * @return string
  223. */
  224. protected function _parseName(Provider $provider)
  225. {
  226. $className = get_class($provider);
  227. $providerName = $className;
  228. if (strpos($providerName, '_') !== false) {
  229. $providerName = substr($providerName, strrpos($providerName, '_')+1);
  230. }
  231. $providerName = substr($className, strrpos($className, '\\')+1);
  232. if (substr($providerName, -8) == 'Provider') {
  233. $providerName = substr($providerName, 0, strlen($providerName)-8);
  234. }
  235. return $providerName;
  236. }
  237. }