1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Tool
* @subpackage Framework
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Repository.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_Tool_Framework_Registry_EnabledInterface
*/
require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php';
/**
* @category Zend
* @package Zend_Tool
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Tool_Framework_Manifest_Repository
implements Zend_Tool_Framework_Registry_EnabledInterface, IteratorAggregate, Countable
{
/**
* @var Zend_Tool_Framework_Provider_Registry_Interface
*/
protected $_registry = null;
/**
* @var array
*/
protected $_manifests = array();
/**
* @var array Array of Zend_Tool_Framework_Metadata_Interface
*/
protected $_metadatas = array();
/**
* setRegistry()
*
* @param Zend_Tool_Framework_Registry_Interface $registry
* @return unknown
*/
public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
{
$this->_registry = $registry;
return $this;
}
/**
* addManifest() - Add a manifest for later processing
*
* @param Zend_Tool_Framework_Manifest_Interface $manifest
* @return Zend_Tool_Framework_Manifest_Repository
*/
public function addManifest(Zend_Tool_Framework_Manifest_Interface $manifest)
{
// we need to get an index number so that manifests with
// higher indexes have priority over others
$index = count($this->_manifests);
if ($manifest instanceof Zend_Tool_Framework_Registry_EnabledInterface) {
$manifest->setRegistry($this->_registry);
}
// if the manifest supplies a getIndex() method, use it
if ($manifest instanceof Zend_Tool_Framework_Manifest_Indexable) {
$index = $manifest->getIndex();
}
// get the required objects from the framework registry
$actionRepository = $this->_registry->getActionRepository();
$providerRepository = $this->_registry->getProviderRepository();
// load providers if interface supports that method
if ($manifest instanceof Zend_Tool_Framework_Manifest_ProviderManifestable) {
$providers = $manifest->getProviders();
if (!is_array($providers)) {
$providers = array($providers);
}
foreach ($providers as $provider) {
// if provider is a string, try and load it as an object
if (is_string($provider)) {
$provider = new $provider();
}
if (!$provider instanceof Zend_Tool_Framework_Provider_Interface) {
require_once 'Zend/Tool/Framework/Manifest/Exception.php';
throw new Zend_Tool_Framework_Manifest_Exception(
'A provider provided by the ' . get_class($manifest)
. ' does not implement Zend_Tool_Framework_Provider_Interface'
);
}
if (!$providerRepository->hasProvider($provider, false)) {
$providerRepository->addProvider($provider);
}
}
}
// load actions if interface supports that method
if ($manifest instanceof Zend_Tool_Framework_Manifest_ActionManifestable) {
$actions = $manifest->getActions();
if (!is_array($actions)) {
$actions = array($actions);
}
foreach ($actions as $action) {
if (is_string($action)) {
$action = new Zend_Tool_Framework_Action_Base($action);
}
$actionRepository->addAction($action);
}
}
// should we detect collisions here? does it even matter?
$this->_manifests[$index] = $manifest;
ksort($this->_manifests);
return $this;
}
/**
* getManifests()
*
* @return Zend_Tool_Framework_Manifest_Interface[]
*/
public function getManifests()
{
return $this->_manifests;
}
/**
* addMetadata() - add a metadata peice by peice
*
* @param Zend_Tool_Framework_Manifest_Metadata $metadata
* @return Zend_Tool_Framework_Manifest_Repository
*/
public function addMetadata(Zend_Tool_Framework_Metadata_Interface $metadata)
{
$this->_metadatas[] = $metadata;
return $this;
}
/**
* process() - Process is expected to be called at the end of client construction time.
* By this time, the loader has run and loaded any found manifests into the repository
* for loading
*
* @return Zend_Tool_Framework_Manifest_Repository
*/
public function process()
{
foreach ($this->_manifests as $manifest) {
if ($manifest instanceof Zend_Tool_Framework_Manifest_MetadataManifestable) {
$metadatas = $manifest->getMetadata();
if (!is_array($metadatas)) {
$metadatas = array($metadatas);
}
foreach ($metadatas as $metadata) {
if (is_array($metadata)) {
if (!class_exists('Zend_Tool_Framework_Metadata_Dynamic')) {
require_once 'Zend/Tool/Framework/Metadata/Dynamic.php';
}
$metadata = new Zend_Tool_Framework_Metadata_Dynamic($metadata);
}
if (!$metadata instanceof Zend_Tool_Framework_Metadata_Interface) {
require_once 'Zend/Tool/Framework/Manifest/Exception.php';
throw new Zend_Tool_Framework_Manifest_Exception(
'A Zend_Tool_Framework_Metadata_Interface object was not found in manifest ' . get_class($manifest)
);
}
$this->addMetadata($metadata);
}
}
}
return $this;
}
/**
* getMetadatas() - This is the main search function for the repository.
*
* example: This will retrieve all metadata that matches the following criteria
* $manifestRepo->getMetadatas(array(
* 'providerName' => 'Version',
* 'actionName' => 'show'
* ));
*
* @param array $searchProperties
* @param bool $includeNonExistentProperties
* @return Zend_Tool_Framework_Manifest_Metadata[]
*/
public function getMetadatas(Array $searchProperties = array(), $includeNonExistentProperties = true)
{
$returnMetadatas = array();
// loop through the metadatas so that we can search each individual one
foreach ($this->_metadatas as $metadata) {
// each value will be retrieved from the metadata, each metadata should
// implement a getter method to retrieve the value
foreach ($searchProperties as $searchPropertyName => $searchPropertyValue) {
if (method_exists($metadata, 'get' . $searchPropertyName)) {
if ($metadata->{'get' . $searchPropertyName}() != $searchPropertyValue) {
// if the metadata supports a specific property but the value does not
// match, move on
continue 2;
}
} elseif (!$includeNonExistentProperties) {
// if the option $includeNonExitentProperties is false, then move on as
// we dont want to include this metadata if non existent
// search properties are not inside the target (current) metadata
continue 2;
}
}
// all searching has been accounted for, if we reach this point, then the metadata
// is good and we can return it
$returnMetadatas[] = $metadata;
}
return $returnMetadatas;
}
/**
* getMetadata() - This will proxy to getMetadatas(), but will only return a single metadata. This method
* should be used in situations where the search criteria is known to only find a single metadata object
*
* @param array $searchProperties
* @param bool $includeNonExistentProperties
* @return Zend_Tool_Framework_Manifest_Metadata
*/
public function getMetadata(Array $searchProperties = array(), $includeNonExistentProperties = true)
{
$metadatas = $this->getMetadatas($searchProperties, $includeNonExistentProperties);
return array_shift($metadatas);
}
/**
* __toString() - cast to string
*
* @return string
*/
public function __toString()
{
$metadatasByType = array();
foreach ($this->_metadatas as $metadata) {
if (!array_key_exists($metadata->getType(), $metadatasByType)) {
$metadatasByType[$metadata->getType()] = array();
}
$metadatasByType[$metadata->getType()][] = $metadata;
}
$string = '';
foreach ($metadatasByType as $type => $metadatas) {
$string .= $type . PHP_EOL;
foreach ($metadatas as $metadata) {
$metadataString = ' ' . $metadata->__toString() . PHP_EOL;
//$metadataString = str_replace(PHP_EOL, PHP_EOL . ' ', $metadataString);
$string .= $metadataString;
}
}
return $string;
}
/**
* count() - required by the Countable Interface
*
* @return int
*/
public function count()
{
return count($this->_metadatas);
}
/**
* getIterator() - required by the IteratorAggregate interface
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_metadatas);
}
}
|