File indexing completed on 2024-12-29 05:28:08
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Tool 0017 * @subpackage Framework 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * @see Zend_Tool_Framework_Provider_Signature 0025 */ 0026 // require_once 'Zend/Tool/Framework/Provider/Signature.php'; 0027 0028 /** 0029 * @see Zend_Tool_Framework_Registry_EnabledInterface 0030 */ 0031 // require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php'; 0032 0033 /** 0034 * @category Zend 0035 * @package Zend_Tool 0036 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0037 * @license http://framework.zend.com/license/new-bsd New BSD License 0038 */ 0039 class Zend_Tool_Framework_Provider_Repository 0040 implements Zend_Tool_Framework_Registry_EnabledInterface, IteratorAggregate, Countable 0041 { 0042 0043 /** 0044 * @var Zend_Tool_Framework_Registry 0045 */ 0046 protected $_registry = null; 0047 0048 /** 0049 * @var bool 0050 */ 0051 protected $_processOnAdd = false; 0052 0053 /** 0054 * @var Zend_Tool_Framework_Provider_Interface[] 0055 */ 0056 protected $_unprocessedProviders = array(); 0057 0058 /** 0059 * @var Zend_Tool_Framework_Provider_Signature[] 0060 */ 0061 protected $_providerSignatures = array(); 0062 0063 /** 0064 * @var array Array of Zend_Tool_Framework_Provider_Inteface 0065 */ 0066 protected $_providers = array(); 0067 0068 /** 0069 * setRegistry() 0070 * 0071 * @param Zend_Tool_Framework_Registry_Interface $registry 0072 * @return unknown 0073 */ 0074 public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry) 0075 { 0076 $this->_registry = $registry; 0077 return $this; 0078 } 0079 0080 /** 0081 * Set the ProcessOnAdd flag 0082 * 0083 * @param unknown_type $processOnAdd 0084 * @return unknown 0085 */ 0086 public function setProcessOnAdd($processOnAdd = true) 0087 { 0088 $this->_processOnAdd = (bool) $processOnAdd; 0089 return $this; 0090 } 0091 0092 /** 0093 * Add a provider to the repository for processing 0094 * 0095 * @param Zend_Tool_Framework_Provider_Interface $provider 0096 * @return Zend_Tool_Framework_Provider_Repository 0097 */ 0098 public function addProvider(Zend_Tool_Framework_Provider_Interface $provider, $overwriteExistingProvider = false) 0099 { 0100 if ($provider instanceof Zend_Tool_Framework_Registry_EnabledInterface) { 0101 $provider->setRegistry($this->_registry); 0102 } 0103 0104 if (method_exists($provider, 'getName')) { 0105 $providerName = $provider->getName(); 0106 } else { 0107 $providerName = $this->_parseName($provider); 0108 } 0109 0110 // if a provider by the given name already exist, and its not set as overwritable, throw exception 0111 if (!$overwriteExistingProvider && 0112 (array_key_exists($providerName, $this->_unprocessedProviders) 0113 || array_key_exists($providerName, $this->_providers))) 0114 { 0115 // require_once 'Zend/Tool/Framework/Provider/Exception.php'; 0116 throw new Zend_Tool_Framework_Provider_Exception('A provider by the name ' . $providerName 0117 . ' is already registered and $overrideExistingProvider is set to false.'); 0118 } 0119 0120 $this->_unprocessedProviders[$providerName] = $provider; 0121 0122 // if process has already been called, process immediately. 0123 if ($this->_processOnAdd) { 0124 $this->process(); 0125 } 0126 0127 return $this; 0128 } 0129 0130 public function hasProvider($providerOrClassName, $processedOnly = true) 0131 { 0132 if ($providerOrClassName instanceof Zend_Tool_Framework_Provider_Interface) { 0133 $targetProviderClassName = get_class($providerOrClassName); 0134 } else { 0135 $targetProviderClassName = (string) $providerOrClassName; 0136 } 0137 0138 if (!$processedOnly) { 0139 foreach ($this->_unprocessedProviders as $unprocessedProvider) { 0140 if (get_class($unprocessedProvider) == $targetProviderClassName) { 0141 return true; 0142 } 0143 } 0144 } 0145 0146 foreach ($this->_providers as $processedProvider) { 0147 if (get_class($processedProvider) == $targetProviderClassName) { 0148 return true; 0149 } 0150 } 0151 0152 return false; 0153 } 0154 0155 /** 0156 * Process all of the unprocessed providers 0157 * 0158 */ 0159 public function process() 0160 { 0161 0162 // process all providers in the unprocessedProviders array 0163 //foreach ($this->_unprocessedProviders as $providerName => $provider) { 0164 reset($this->_unprocessedProviders); 0165 while ($this->_unprocessedProviders) { 0166 0167 $providerName = key($this->_unprocessedProviders); 0168 $provider = array_shift($this->_unprocessedProviders); 0169 0170 // create a signature for the provided provider 0171 $providerSignature = new Zend_Tool_Framework_Provider_Signature($provider); 0172 0173 if ($providerSignature instanceof Zend_Tool_Framework_Registry_EnabledInterface) { 0174 $providerSignature->setRegistry($this->_registry); 0175 } 0176 0177 $providerSignature->process(); 0178 0179 // ensure the name is lowercased for easier searching 0180 $providerName = strtolower($providerName); 0181 0182 // add to the appropraite place 0183 $this->_providerSignatures[$providerName] = $providerSignature; 0184 $this->_providers[$providerName] = $providerSignature->getProvider(); 0185 0186 if ($provider instanceof Zend_Tool_Framework_Provider_Initializable) { 0187 $provider->initialize(); 0188 } 0189 0190 } 0191 0192 } 0193 0194 /** 0195 * getProviders() Get all the providers in the repository 0196 * 0197 * @return array 0198 */ 0199 public function getProviders() 0200 { 0201 return $this->_providers; 0202 } 0203 0204 /** 0205 * getProviderSignatures() Get all the provider signatures 0206 * 0207 * @return array 0208 */ 0209 public function getProviderSignatures() 0210 { 0211 return $this->_providerSignatures; 0212 } 0213 0214 /** 0215 * getProvider() 0216 * 0217 * @param string $providerName 0218 * @return Zend_Tool_Framework_Provider_Interface 0219 */ 0220 public function getProvider($providerName) 0221 { 0222 return $this->_providers[strtolower($providerName)]; 0223 } 0224 0225 /** 0226 * getProviderSignature() 0227 * 0228 * @param string $providerName 0229 * @return Zend_Tool_Framework_Provider_Signature 0230 */ 0231 public function getProviderSignature($providerName) 0232 { 0233 return $this->_providerSignatures[strtolower($providerName)]; 0234 } 0235 0236 /** 0237 * count() - return the number of providers 0238 * 0239 * @return int 0240 */ 0241 public function count() 0242 { 0243 return count($this->_providers); 0244 } 0245 0246 /** 0247 * getIterator() - Required by the IteratorAggregate Interface 0248 * 0249 * @return ArrayIterator 0250 */ 0251 public function getIterator() 0252 { 0253 return new ArrayIterator($this->getProviders()); 0254 } 0255 0256 /** 0257 * _parseName - internal method to determine the name of an action when one is not explicity provided. 0258 * 0259 * @param Zend_Tool_Framework_Action_Interface $action 0260 * @return string 0261 */ 0262 protected function _parseName(Zend_Tool_Framework_Provider_Interface $provider) 0263 { 0264 $className = get_class($provider); 0265 $providerName = $className; 0266 if (strpos($providerName, '_') !== false) { 0267 $providerName = substr($providerName, strrpos($providerName, '_')+1); 0268 } 0269 if (substr($providerName, -8) == 'Provider') { 0270 $providerName = substr($providerName, 0, strlen($providerName)-8); 0271 } 0272 return $providerName; 0273 } 0274 0275 }