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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
<?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: Container.php 20967 2010-02-07 18:17:49Z ralph $
*/
/**
* @see Zend_Tool_Project_Profile_Resource_SearchConstraints
*/
require_once 'Zend/Tool/Project/Profile/Resource/SearchConstraints.php';
/**
* This class is an iterator that will iterate only over enabled resources
*
* @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_Project_Profile_Resource_Container implements RecursiveIterator, Countable
{
/**
* @var array
*/
protected $_subResources = array();
/**
* @var int
*/
protected $_position = 0;
/**
* @var bool
*/
protected $_appendable = true;
/**
* @var array
*/
protected $_attributes = array();
/**
* Finder method to be able to find resources by context name
* and attributes. Example usage:
*
* <code>
*
* </code>
*
* @param Zend_Tool_Project_Profile_Resource_SearchConstraints|string|array $searchParameters
* @return Zend_Tool_Project_Profile_Resource
*/
public function search($matchSearchConstraints, $nonMatchSearchConstraints = null)
{
if (!$matchSearchConstraints instanceof Zend_Tool_Project_Profile_Resource_SearchConstraints) {
$matchSearchConstraints = new Zend_Tool_Project_Profile_Resource_SearchConstraints($matchSearchConstraints);
}
$this->rewind();
/**
* @todo This should be re-written with better support for a filter iterator, its the way to go
*/
if ($nonMatchSearchConstraints) {
$filterIterator = new Zend_Tool_Project_Profile_Iterator_ContextFilter($this, array('denyNames' => $nonMatchSearchConstraints));
$riIterator = new RecursiveIteratorIterator($filterIterator, RecursiveIteratorIterator::SELF_FIRST);
} else {
$riIterator = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST);
}
$foundResource = false;
$currentConstraint = $matchSearchConstraints->getConstraint();
$foundDepth = 0;
foreach ($riIterator as $currentResource) {
// if current depth is less than found depth, end
if ($riIterator->getDepth() < $foundDepth) {
break;
}
if (strtolower($currentResource->getName()) == strtolower($currentConstraint->name)) {
$paramsMatch = true;
// @todo check to ensure params match (perhaps)
if (count($currentConstraint->params) > 0) {
$currentResourceAttributes = $currentResource->getAttributes();
if (!is_array($currentConstraint->params)) {
require_once 'Zend/Tool/Project/Profile/Exception.php';
throw new Zend_Tool_Project_Profile_Exception('Search parameter specifics must be in the form of an array for key "'
. $currentConstraint->name .'"');
}
foreach ($currentConstraint->params as $paramName => $paramValue) {
if (!isset($currentResourceAttributes[$paramName]) || $currentResourceAttributes[$paramName] != $paramValue) {
$paramsMatch = false;
break;
}
}
}
if ($paramsMatch) {
$foundDepth = $riIterator->getDepth();
if (($currentConstraint = $matchSearchConstraints->getConstraint()) == null) {
$foundResource = $currentResource;
break;
}
}
}
}
return $foundResource;
}
/**
* createResourceAt()
*
* @param array|Zend_Tool_Project_Profile_Resource_SearchConstraints $appendResourceOrSearchConstraints
* @param string $context
* @param array $attributes
* @return Zend_Tool_Project_Profile_Resource
*/
public function createResourceAt($appendResourceOrSearchConstraints, $context, Array $attributes = array())
{
if (!$appendResourceOrSearchConstraints instanceof Zend_Tool_Project_Profile_Resource_Container) {
if (($parentResource = $this->search($appendResourceOrSearchConstraints)) == false) {
require_once 'Zend/Tool/Project/Profile/Exception.php';
throw new Zend_Tool_Project_Profile_Exception('No node was found to append to.');
}
} else {
$parentResource = $appendResourceOrSearchConstraints;
}
return $parentResource->createResource($context, $attributes);
}
/**
* createResource()
*
* Method to create a resource with a given context with specific attributes
*
* @param string $context
* @param array $attributes
* @return Zend_Tool_Project_Profile_Resource
*/
public function createResource($context, Array $attributes = array())
{
if (is_string($context)) {
$contextRegistry = Zend_Tool_Project_Context_Repository::getInstance();
if ($contextRegistry->hasContext($context)) {
$context = $contextRegistry->getContext($context);
} else {
require_once 'Zend/Tool/Project/Profile/Exception.php';
throw new Zend_Tool_Project_Profile_Exception('Context by name ' . $context . ' was not found in the context registry.');
}
} elseif (!$context instanceof Zend_Tool_Project_Context_Interface) {
require_once 'Zend/Tool/Project/Profile/Exception.php';
throw new Zend_Tool_Project_Profile_Exception('Context must be of type string or Zend_Tool_Project_Context_Interface.');
}
$newResource = new Zend_Tool_Project_Profile_Resource($context);
if ($attributes) {
$newResource->setAttributes($attributes);
}
/**
* Interesting logic here:
*
* First set the parentResource (this will also be done inside append). This will allow
* the initialization routine to change the appendability of the parent resource. This
* is important to allow specific resources to be appendable by very specific sub-resources.
*/
$newResource->setParentResource($this);
$newResource->initializeContext();
$this->append($newResource);
return $newResource;
}
/**
* setAttributes()
*
* persist the attributes if the resource will accept them
*
* @param array $attributes
* @return Zend_Tool_Project_Profile_Resource_Container
*/
public function setAttributes(Array $attributes)
{
foreach ($attributes as $attrName => $attrValue) {
$setMethod = 'set' . $attrName;
if (method_exists($this, $setMethod)) {
$this->{$setMethod}($attrValue);
} else {
$this->setAttribute($attrName, $attrValue);
}
}
return $this;
}
/**
* getAttributes()
*
* @return array
*/
public function getAttributes()
{
return $this->_attributes;
}
/**
* setAttribute()
*
* @param string $name
* @param mixed $value
* @return Zend_Tool_Project_Profile_Resource_Container
*/
public function setAttribute($name, $value)
{
$this->_attributes[$name] = $value;
return $this;
}
/**
* getAttribute()
*
* @param string $name
* @return Zend_Tool_Project_Profile_Resource_Container
*/
public function getAttribute($name)
{
return (array_key_exists($name, $this->_attributes)) ? $this->_attributes[$name] : null;
}
/**
* hasAttribute()
*
* @param string $name
* @return bool
*/
public function hasAttribute($name)
{
return array_key_exists($name, $this->_attributes);
}
/**
* setAppendable()
*
* @param bool $appendable
* @return Zend_Tool_Project_Profile_Resource_Container
*/
public function setAppendable($appendable)
{
$this->_appendable = (bool) $appendable;
return $this;
}
/**
* isAppendable()
*
* @return bool
*/
public function isAppendable()
{
return $this->_appendable;
}
/**
* setParentResource()
*
* @param Zend_Tool_Project_Profile_Resource_Container $parentResource
* @return Zend_Tool_Project_Profile_Resource_Container
*/
public function setParentResource(Zend_Tool_Project_Profile_Resource_Container $parentResource)
{
$this->_parentResource = $parentResource;
return $this;
}
/**
* getParentResource()
*
* @return Zend_Tool_Project_Profile_Resource_Container
*/
public function getParentResource()
{
return $this->_parentResource;
}
/**
* append()
*
* @param Zend_Tool_Project_Profile_Resource_Container $resource
* @return Zend_Tool_Project_Profile_Resource_Container
*/
public function append(Zend_Tool_Project_Profile_Resource_Container $resource)
{
if (!$this->isAppendable()) {
throw new Exception('Resource by name ' . (string) $this . ' is not appendable');
}
array_push($this->_subResources, $resource);
$resource->setParentResource($this);
return $this;
}
/**
* current() - required by RecursiveIterator
*
* @return Zend_Tool_Project_Profile_Resource
*/
public function current()
{
return current($this->_subResources);
}
/**
* key() - required by RecursiveIterator
*
* @return int
*/
public function key()
{
return key($this->_subResources);
}
/**
* next() - required by RecursiveIterator
*
* @return bool
*/
public function next()
{
return next($this->_subResources);
}
/**
* rewind() - required by RecursiveIterator
*
* @return bool
*/
public function rewind()
{
return reset($this->_subResources);
}
/**
* valid() - - required by RecursiveIterator
*
* @return bool
*/
public function valid()
{
return (bool) $this->current();
}
/**
* hasChildren()
*
* @return bool
*/
public function hasChildren()
{
return (count($this->_subResources > 0)) ? true : false;
}
/**
* getChildren()
*
* @return array
*/
public function getChildren()
{
return $this->current();
}
/**
* count()
*
* @return int
*/
public function count()
{
return count($this->_subResources);
}
/**
* __clone()
*
*/
public function __clone()
{
$this->rewind();
foreach ($this->_subResources as $index => $resource) {
$this->_subResources[$index] = clone $resource;
}
}
}
|