Container.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Tool
  17. * @subpackage Framework
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Tool_Project_Profile_Resource_SearchConstraints
  24. */
  25. require_once 'Zend/Tool/Project/Profile/Resource/SearchConstraints.php';
  26. /**
  27. * This class is an iterator that will iterate only over enabled resources
  28. *
  29. * @category Zend
  30. * @package Zend_Tool
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Tool_Project_Profile_Resource_Container implements RecursiveIterator, Countable
  35. {
  36. /**
  37. * @var array
  38. */
  39. protected $_subResources = array();
  40. /**
  41. * @var int
  42. */
  43. protected $_position = 0;
  44. /**
  45. * @var bool
  46. */
  47. protected $_appendable = true;
  48. /**
  49. * Finder method to be able to find resources by context name
  50. * and attributes. Example usage:
  51. *
  52. * <code>
  53. *
  54. * </code>
  55. *
  56. * @param Zend_Tool_Project_Profile_Resource_SearchConstraints|string|array $searchParameters
  57. * @return Zend_Tool_Project_Profile_Resource
  58. */
  59. public function search($searchConstraints)
  60. {
  61. if (!$searchConstraints instanceof Zend_Tool_Project_Profile_Resource_SearchConstraints) {
  62. $searchConstraints = new Zend_Tool_Project_Profile_Resource_SearchConstraints($searchConstraints);
  63. }
  64. $this->rewind();
  65. $riIterator = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST);
  66. $foundResource = false;
  67. $currentConstraint = $searchConstraints->getConstraint();
  68. $foundDepth = 0;
  69. while ($currentResource = $riIterator->current()) {
  70. // if current depth is less than found depth, end
  71. if ($riIterator->getDepth() < $foundDepth) {
  72. break;
  73. }
  74. if (strtolower($currentResource->getName()) == strtolower($currentConstraint->name)) {
  75. $paramsMatch = true;
  76. // @todo check to ensure params match (perhaps)
  77. if (count($currentConstraint->params) > 0) {
  78. $currentResourceAttributes = $currentResource->getAttributes();
  79. foreach ($currentConstraint->params as $paramName => $paramValue) {
  80. if (!isset($currentResourceAttributes[$paramName]) || $currentResourceAttributes[$paramName] != $paramValue) {
  81. $paramsMatch = false;
  82. break;
  83. }
  84. }
  85. }
  86. if ($paramsMatch) {
  87. $foundDepth = $riIterator->getDepth();
  88. if (($currentConstraint = $searchConstraints->getConstraint()) == null) {
  89. $foundResource = $currentResource;
  90. break;
  91. }
  92. }
  93. }
  94. $riIterator->next();
  95. }
  96. return $foundResource;
  97. }
  98. /**
  99. * createResourceAt()
  100. *
  101. * @param array|Zend_Tool_Project_Profile_Resource_SearchConstraints $appendResourceOrSearchConstraints
  102. * @param string $context
  103. * @param array $attributes
  104. * @return Zend_Tool_Project_Profile_Resource
  105. */
  106. public function createResourceAt($appendResourceOrSearchConstraints, $context, Array $attributes = array())
  107. {
  108. if (!$appendResourceOrSearchConstraints instanceof Zend_Tool_Project_Profile_Resource_Container) {
  109. if (($parentResource = $this->search($appendResourceOrSearchConstraints)) == false) {
  110. require_once 'Zend/Tool/Project/Profile/Exception.php';
  111. throw new Zend_Tool_Project_Profile_Exception('No node was found to append to.');
  112. }
  113. } else {
  114. $parentResource = $appendResourceOrSearchConstraints;
  115. }
  116. return $parentResource->createResource($context, $attributes);
  117. }
  118. /**
  119. * createResource()
  120. *
  121. * Method to create a resource with a given context with specific attributes
  122. *
  123. * @param string $context
  124. * @param array $attributes
  125. * @return Zend_Tool_Project_Profile_Resource
  126. */
  127. public function createResource($context, Array $attributes = array())
  128. {
  129. if (is_string($context)) {
  130. $contextRegistry = Zend_Tool_Project_Context_Repository::getInstance();
  131. if ($contextRegistry->hasContext($context)) {
  132. $context = $contextRegistry->getContext($context);
  133. } else {
  134. require_once 'Zend/Tool/Project/Profile/Exception.php';
  135. throw new Zend_Tool_Project_Profile_Exception('Context by name ' . $context . ' was not found in the context registry.');
  136. }
  137. } elseif (!$context instanceof Zend_Tool_Project_Context_Interface) {
  138. require_once 'Zend/Tool/Project/Profile/Exception.php';
  139. throw new Zend_Tool_Project_Profile_Exception('Context must be of type string or Zend_Tool_Project_Context_Interface.');
  140. }
  141. $newResource = new Zend_Tool_Project_Profile_Resource($context);
  142. if ($attributes) {
  143. $newResource->setAttributes($attributes);
  144. }
  145. /**
  146. * Interesting logic here:
  147. *
  148. * First set the parentResource (this will also be done inside append). This will allow
  149. * the initialization routine to change the appendability of the parent resource. This
  150. * is important to allow specific resources to be appendable by very specific sub-resources.
  151. */
  152. $newResource->setParentResource($this);
  153. $newResource->initializeContext();
  154. $this->append($newResource);
  155. return $newResource;
  156. }
  157. /**
  158. * setAttributes()
  159. *
  160. * persist the attributes if the resource will accept them
  161. *
  162. * @param array $attributes
  163. * @return Zend_Tool_Project_Profile_Resource_Container
  164. */
  165. public function setAttributes(Array $attributes)
  166. {
  167. foreach ($attributes as $attrName => $attrValue) {
  168. $setMethod = 'set' . $attrName;
  169. if (method_exists($this, $setMethod)) {
  170. $this->{$setMethod}($attrValue);
  171. } else {
  172. $this->setAttribute($attrName, $attrValue);
  173. }
  174. }
  175. return $this;
  176. }
  177. /**
  178. * getAttributes()
  179. *
  180. * @return array
  181. */
  182. public function getAttributes()
  183. {
  184. return $this->_attributes;
  185. }
  186. /**
  187. * setAttribute()
  188. *
  189. * @param string $name
  190. * @param mixed $value
  191. * @return Zend_Tool_Project_Profile_Resource_Container
  192. */
  193. public function setAttribute($name, $value)
  194. {
  195. $this->_attributes[$name] = $value;
  196. return $this;
  197. }
  198. /**
  199. * getAttribute()
  200. *
  201. * @param string $name
  202. * @return Zend_Tool_Project_Profile_Resource_Container
  203. */
  204. public function getAttribute($name)
  205. {
  206. return (array_key_exists($name, $this->_attributes)) ? $this->_attributes[$name] : null;
  207. }
  208. /**
  209. * setAppendable()
  210. *
  211. * @param bool $appendable
  212. * @return Zend_Tool_Project_Profile_Resource_Container
  213. */
  214. public function setAppendable($appendable)
  215. {
  216. $this->_appendable = (bool) $appendable;
  217. return $this;
  218. }
  219. /**
  220. * isAppendable()
  221. *
  222. * @return bool
  223. */
  224. public function isAppendable()
  225. {
  226. return $this->_appendable;
  227. }
  228. /**
  229. * setParentResource()
  230. *
  231. * @param Zend_Tool_Project_Profile_Resource_Container $parentResource
  232. * @return Zend_Tool_Project_Profile_Resource_Container
  233. */
  234. public function setParentResource(Zend_Tool_Project_Profile_Resource_Container $parentResource)
  235. {
  236. $this->_parentResource = $parentResource;
  237. return $this;
  238. }
  239. /**
  240. * getParentResource()
  241. *
  242. * @return Zend_Tool_Project_Profile_Resource_Container
  243. */
  244. public function getParentResource()
  245. {
  246. return $this->_parentResource;
  247. }
  248. /**
  249. * append()
  250. *
  251. * @param Zend_Tool_Project_Profile_Resource_Container $resource
  252. * @return Zend_Tool_Project_Profile_Resource_Container
  253. */
  254. public function append(Zend_Tool_Project_Profile_Resource_Container $resource)
  255. {
  256. if (!$this->isAppendable()) {
  257. throw new Exception('Resource by name ' . (string) $this . ' is not appendable');
  258. }
  259. array_push($this->_subResources, $resource);
  260. $resource->setParentResource($this);
  261. return $this;
  262. }
  263. /**
  264. * current() - required by RecursiveIterator
  265. *
  266. * @return Zend_Tool_Project_Profile_Resource
  267. */
  268. public function current()
  269. {
  270. return current($this->_subResources);
  271. }
  272. /**
  273. * key() - required by RecursiveIterator
  274. *
  275. * @return int
  276. */
  277. public function key()
  278. {
  279. return key($this->_subResources);
  280. }
  281. /**
  282. * next() - required by RecursiveIterator
  283. *
  284. * @return bool
  285. */
  286. public function next()
  287. {
  288. return next($this->_subResources);
  289. }
  290. /**
  291. * rewind() - required by RecursiveIterator
  292. *
  293. * @return bool
  294. */
  295. public function rewind()
  296. {
  297. return reset($this->_subResources);
  298. }
  299. /**
  300. * valid() - - required by RecursiveIterator
  301. *
  302. * @return bool
  303. */
  304. public function valid()
  305. {
  306. return (bool) $this->current();
  307. }
  308. /**
  309. * hasChildren()
  310. *
  311. * @return bool
  312. */
  313. public function hasChildren()
  314. {
  315. return (count($this->_subResources > 0)) ? true : false;
  316. }
  317. /**
  318. * getChildren()
  319. *
  320. * @return array
  321. */
  322. public function getChildren()
  323. {
  324. return $this->current();
  325. }
  326. /**
  327. * count()
  328. *
  329. * @return int
  330. */
  331. public function count()
  332. {
  333. return count($this->_subResources);
  334. }
  335. /**
  336. * __clone()
  337. *
  338. */
  339. public function __clone()
  340. {
  341. $this->rewind();
  342. foreach ($this->_subResources as $index => $resource) {
  343. $this->_subResources[$index] = clone $resource;
  344. }
  345. }
  346. }