| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- <?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-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @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-2015 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;
- }
- }
- }
|