File indexing completed on 2025-01-19 05:21:35
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_Registry_EnabledInterface 0025 */ 0026 // require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php'; 0027 0028 /** 0029 * @category Zend 0030 * @package Zend_Tool 0031 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0032 * @license http://framework.zend.com/license/new-bsd New BSD License 0033 */ 0034 class Zend_Tool_Framework_Manifest_Repository 0035 implements Zend_Tool_Framework_Registry_EnabledInterface, IteratorAggregate, Countable 0036 { 0037 0038 /** 0039 * @var Zend_Tool_Framework_Provider_Registry_Interface 0040 */ 0041 protected $_registry = null; 0042 0043 /** 0044 * @var array 0045 */ 0046 protected $_manifests = array(); 0047 0048 /** 0049 * @var array Array of Zend_Tool_Framework_Metadata_Interface 0050 */ 0051 protected $_metadatas = array(); 0052 0053 /** 0054 * setRegistry() 0055 * 0056 * @param Zend_Tool_Framework_Registry_Interface $registry 0057 * @return unknown 0058 */ 0059 public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry) 0060 { 0061 $this->_registry = $registry; 0062 return $this; 0063 } 0064 0065 /** 0066 * addManifest() - Add a manifest for later processing 0067 * 0068 * @param Zend_Tool_Framework_Manifest_Interface $manifest 0069 * @return Zend_Tool_Framework_Manifest_Repository 0070 */ 0071 public function addManifest(Zend_Tool_Framework_Manifest_Interface $manifest) 0072 { 0073 // we need to get an index number so that manifests with 0074 // higher indexes have priority over others 0075 $index = count($this->_manifests); 0076 0077 if ($manifest instanceof Zend_Tool_Framework_Registry_EnabledInterface) { 0078 $manifest->setRegistry($this->_registry); 0079 } 0080 0081 // if the manifest supplies a getIndex() method, use it 0082 if ($manifest instanceof Zend_Tool_Framework_Manifest_Indexable) { 0083 $index = $manifest->getIndex(); 0084 } 0085 0086 // get the required objects from the framework registry 0087 $actionRepository = $this->_registry->getActionRepository(); 0088 $providerRepository = $this->_registry->getProviderRepository(); 0089 0090 // load providers if interface supports that method 0091 if ($manifest instanceof Zend_Tool_Framework_Manifest_ProviderManifestable) { 0092 $providers = $manifest->getProviders(); 0093 if (!is_array($providers)) { 0094 $providers = array($providers); 0095 } 0096 0097 foreach ($providers as $provider) { 0098 0099 // if provider is a string, try and load it as an object 0100 if (is_string($provider)) { 0101 $provider = new $provider(); 0102 } 0103 0104 if (!$provider instanceof Zend_Tool_Framework_Provider_Interface) { 0105 // require_once 'Zend/Tool/Framework/Manifest/Exception.php'; 0106 throw new Zend_Tool_Framework_Manifest_Exception( 0107 'A provider provided by the ' . get_class($manifest) 0108 . ' does not implement Zend_Tool_Framework_Provider_Interface' 0109 ); 0110 } 0111 if (!$providerRepository->hasProvider($provider, false)) { 0112 $providerRepository->addProvider($provider); 0113 } 0114 } 0115 0116 } 0117 0118 // load actions if interface supports that method 0119 if ($manifest instanceof Zend_Tool_Framework_Manifest_ActionManifestable) { 0120 $actions = $manifest->getActions(); 0121 if (!is_array($actions)) { 0122 $actions = array($actions); 0123 } 0124 0125 foreach ($actions as $action) { 0126 if (is_string($action)) { 0127 $action = new Zend_Tool_Framework_Action_Base($action); 0128 } 0129 $actionRepository->addAction($action); 0130 } 0131 } 0132 0133 // should we detect collisions here? does it even matter? 0134 $this->_manifests[$index] = $manifest; 0135 ksort($this->_manifests); 0136 0137 return $this; 0138 } 0139 0140 /** 0141 * getManifests() 0142 * 0143 * @return Zend_Tool_Framework_Manifest_Interface[] 0144 */ 0145 public function getManifests() 0146 { 0147 return $this->_manifests; 0148 } 0149 0150 /** 0151 * addMetadata() - add a metadata peice by peice 0152 * 0153 * @param Zend_Tool_Framework_Manifest_Metadata $metadata 0154 * @return Zend_Tool_Framework_Manifest_Repository 0155 */ 0156 public function addMetadata(Zend_Tool_Framework_Metadata_Interface $metadata) 0157 { 0158 $this->_metadatas[] = $metadata; 0159 return $this; 0160 } 0161 0162 /** 0163 * process() - Process is expected to be called at the end of client construction time. 0164 * By this time, the loader has run and loaded any found manifests into the repository 0165 * for loading 0166 * 0167 * @return Zend_Tool_Framework_Manifest_Repository 0168 */ 0169 public function process() 0170 { 0171 0172 foreach ($this->_manifests as $manifest) { 0173 if ($manifest instanceof Zend_Tool_Framework_Manifest_MetadataManifestable) { 0174 $metadatas = $manifest->getMetadata(); 0175 if (!is_array($metadatas)) { 0176 $metadatas = array($metadatas); 0177 } 0178 0179 foreach ($metadatas as $metadata) { 0180 if (is_array($metadata)) { 0181 if (!class_exists('Zend_Tool_Framework_Metadata_Dynamic')) { 0182 // require_once 'Zend/Tool/Framework/Metadata/Dynamic.php'; 0183 } 0184 $metadata = new Zend_Tool_Framework_Metadata_Dynamic($metadata); 0185 } 0186 0187 if (!$metadata instanceof Zend_Tool_Framework_Metadata_Interface) { 0188 // require_once 'Zend/Tool/Framework/Manifest/Exception.php'; 0189 throw new Zend_Tool_Framework_Manifest_Exception( 0190 'A Zend_Tool_Framework_Metadata_Interface object was not found in manifest ' . get_class($manifest) 0191 ); 0192 } 0193 0194 $this->addMetadata($metadata); 0195 } 0196 0197 } 0198 } 0199 0200 return $this; 0201 } 0202 0203 /** 0204 * getMetadatas() - This is the main search function for the repository. 0205 * 0206 * example: This will retrieve all metadata that matches the following criteria 0207 * $manifestRepo->getMetadatas(array( 0208 * 'providerName' => 'Version', 0209 * 'actionName' => 'show' 0210 * )); 0211 * 0212 * @param array $searchProperties 0213 * @param bool $includeNonExistentProperties 0214 * @return Zend_Tool_Framework_Manifest_Metadata[] 0215 */ 0216 public function getMetadatas(Array $searchProperties = array(), $includeNonExistentProperties = true) 0217 { 0218 0219 $returnMetadatas = array(); 0220 0221 // loop through the metadatas so that we can search each individual one 0222 foreach ($this->_metadatas as $metadata) { 0223 0224 // each value will be retrieved from the metadata, each metadata should 0225 // implement a getter method to retrieve the value 0226 foreach ($searchProperties as $searchPropertyName => $searchPropertyValue) { 0227 if (method_exists($metadata, 'get' . $searchPropertyName)) { 0228 if ($metadata->{'get' . $searchPropertyName}() != $searchPropertyValue) { 0229 // if the metadata supports a specific property but the value does not 0230 // match, move on 0231 continue 2; 0232 } 0233 } elseif (!$includeNonExistentProperties) { 0234 // if the option $includeNonExitentProperties is false, then move on as 0235 // we dont want to include this metadata if non existent 0236 // search properties are not inside the target (current) metadata 0237 continue 2; 0238 } 0239 } 0240 0241 // all searching has been accounted for, if we reach this point, then the metadata 0242 // is good and we can return it 0243 $returnMetadatas[] = $metadata; 0244 0245 } 0246 0247 return $returnMetadatas; 0248 } 0249 0250 /** 0251 * getMetadata() - This will proxy to getMetadatas(), but will only return a single metadata. This method 0252 * should be used in situations where the search criteria is known to only find a single metadata object 0253 * 0254 * @param array $searchProperties 0255 * @param bool $includeNonExistentProperties 0256 * @return Zend_Tool_Framework_Manifest_Metadata 0257 */ 0258 public function getMetadata(Array $searchProperties = array(), $includeNonExistentProperties = true) 0259 { 0260 $metadatas = $this->getMetadatas($searchProperties, $includeNonExistentProperties); 0261 return array_shift($metadatas); 0262 } 0263 0264 /** 0265 * __toString() - cast to string 0266 * 0267 * @return string 0268 */ 0269 public function __toString() 0270 { 0271 $metadatasByType = array(); 0272 0273 foreach ($this->_metadatas as $metadata) { 0274 if (!array_key_exists($metadata->getType(), $metadatasByType)) { 0275 $metadatasByType[$metadata->getType()] = array(); 0276 } 0277 $metadatasByType[$metadata->getType()][] = $metadata; 0278 } 0279 0280 $string = ''; 0281 foreach ($metadatasByType as $type => $metadatas) { 0282 $string .= $type . PHP_EOL; 0283 foreach ($metadatas as $metadata) { 0284 $metadataString = ' ' . $metadata->__toString() . PHP_EOL; 0285 //$metadataString = str_replace(PHP_EOL, PHP_EOL . ' ', $metadataString); 0286 $string .= $metadataString; 0287 } 0288 } 0289 0290 return $string; 0291 } 0292 0293 /** 0294 * count() - required by the Countable Interface 0295 * 0296 * @return int 0297 */ 0298 public function count() 0299 { 0300 return count($this->_metadatas); 0301 } 0302 0303 /** 0304 * getIterator() - required by the IteratorAggregate interface 0305 * 0306 * @return ArrayIterator 0307 */ 0308 public function getIterator() 0309 { 0310 return new ArrayIterator($this->_metadatas); 0311 } 0312 0313 }