PageRenderTime 63ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/Tool/Project/Profile/Resource/Container.php

https://bitbucket.org/netglue/zf-1.12-release
PHP | 421 lines | 185 code | 49 blank | 187 comment | 25 complexity | 67c4a6260cf4e20b8d7461a9f98bf795 MD5 | raw file
  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-2012 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-2012 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. * @var array
  50. */
  51. protected $_attributes = array();
  52. /**
  53. * Finder method to be able to find resources by context name
  54. * and attributes. Example usage:
  55. *
  56. * <code>
  57. *
  58. * </code>
  59. *
  60. * @param Zend_Tool_Project_Profile_Resource_SearchConstraints|string|array $searchParameters
  61. * @return Zend_Tool_Project_Profile_Resource
  62. */
  63. public function search($matchSearchConstraints, $nonMatchSearchConstraints = null)
  64. {
  65. if (!$matchSearchConstraints instanceof Zend_Tool_Project_Profile_Resource_SearchConstraints) {
  66. $matchSearchConstraints = new Zend_Tool_Project_Profile_Resource_SearchConstraints($matchSearchConstraints);
  67. }
  68. $this->rewind();
  69. /**
  70. * @todo This should be re-written with better support for a filter iterator, its the way to go
  71. */
  72. if ($nonMatchSearchConstraints) {
  73. $filterIterator = new Zend_Tool_Project_Profile_Iterator_ContextFilter($this, array('denyNames' => $nonMatchSearchConstraints));
  74. $riIterator = new RecursiveIteratorIterator($filterIterator, RecursiveIteratorIterator::SELF_FIRST);
  75. } else {
  76. $riIterator = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST);
  77. }
  78. $foundResource = false;
  79. $currentConstraint = $matchSearchConstraints->getConstraint();
  80. $foundDepth = 0;
  81. foreach ($riIterator as $currentResource) {
  82. // if current depth is less than found depth, end
  83. if ($riIterator->getDepth() < $foundDepth) {
  84. break;
  85. }
  86. if (strtolower($currentResource->getName()) == strtolower($currentConstraint->name)) {
  87. $paramsMatch = true;
  88. // @todo check to ensure params match (perhaps)
  89. if (count($currentConstraint->params) > 0) {
  90. $currentResourceAttributes = $currentResource->getAttributes();
  91. if (!is_array($currentConstraint->params)) {
  92. require_once 'Zend/Tool/Project/Profile/Exception.php';
  93. throw new Zend_Tool_Project_Profile_Exception('Search parameter specifics must be in the form of an array for key "'
  94. . $currentConstraint->name .'"');
  95. }
  96. foreach ($currentConstraint->params as $paramName => $paramValue) {
  97. if (!isset($currentResourceAttributes[$paramName]) || $currentResourceAttributes[$paramName] != $paramValue) {
  98. $paramsMatch = false;
  99. break;
  100. }
  101. }
  102. }
  103. if ($paramsMatch) {
  104. $foundDepth = $riIterator->getDepth();
  105. if (($currentConstraint = $matchSearchConstraints->getConstraint()) == null) {
  106. $foundResource = $currentResource;
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. return $foundResource;
  113. }
  114. /**
  115. * createResourceAt()
  116. *
  117. * @param array|Zend_Tool_Project_Profile_Resource_SearchConstraints $appendResourceOrSearchConstraints
  118. * @param string $context
  119. * @param array $attributes
  120. * @return Zend_Tool_Project_Profile_Resource
  121. */
  122. public function createResourceAt($appendResourceOrSearchConstraints, $context, Array $attributes = array())
  123. {
  124. if (!$appendResourceOrSearchConstraints instanceof Zend_Tool_Project_Profile_Resource_Container) {
  125. if (($parentResource = $this->search($appendResourceOrSearchConstraints)) == false) {
  126. require_once 'Zend/Tool/Project/Profile/Exception.php';
  127. throw new Zend_Tool_Project_Profile_Exception('No node was found to append to.');
  128. }
  129. } else {
  130. $parentResource = $appendResourceOrSearchConstraints;
  131. }
  132. return $parentResource->createResource($context, $attributes);
  133. }
  134. /**
  135. * createResource()
  136. *
  137. * Method to create a resource with a given context with specific attributes
  138. *
  139. * @param string $context
  140. * @param array $attributes
  141. * @return Zend_Tool_Project_Profile_Resource
  142. */
  143. public function createResource($context, Array $attributes = array())
  144. {
  145. if (is_string($context)) {
  146. $contextRegistry = Zend_Tool_Project_Context_Repository::getInstance();
  147. if ($contextRegistry->hasContext($context)) {
  148. $context = $contextRegistry->getContext($context);
  149. } else {
  150. require_once 'Zend/Tool/Project/Profile/Exception.php';
  151. throw new Zend_Tool_Project_Profile_Exception('Context by name ' . $context . ' was not found in the context registry.');
  152. }
  153. } elseif (!$context instanceof Zend_Tool_Project_Context_Interface) {
  154. require_once 'Zend/Tool/Project/Profile/Exception.php';
  155. throw new Zend_Tool_Project_Profile_Exception('Context must be of type string or Zend_Tool_Project_Context_Interface.');
  156. }
  157. $newResource = new Zend_Tool_Project_Profile_Resource($context);
  158. if ($attributes) {
  159. $newResource->setAttributes($attributes);
  160. }
  161. /**
  162. * Interesting logic here:
  163. *
  164. * First set the parentResource (this will also be done inside append). This will allow
  165. * the initialization routine to change the appendability of the parent resource. This
  166. * is important to allow specific resources to be appendable by very specific sub-resources.
  167. */
  168. $newResource->setParentResource($this);
  169. $newResource->initializeContext();
  170. $this->append($newResource);
  171. return $newResource;
  172. }
  173. /**
  174. * setAttributes()
  175. *
  176. * persist the attributes if the resource will accept them
  177. *
  178. * @param array $attributes
  179. * @return Zend_Tool_Project_Profile_Resource_Container
  180. */
  181. public function setAttributes(Array $attributes)
  182. {
  183. foreach ($attributes as $attrName => $attrValue) {
  184. $setMethod = 'set' . $attrName;
  185. if (method_exists($this, $setMethod)) {
  186. $this->{$setMethod}($attrValue);
  187. } else {
  188. $this->setAttribute($attrName, $attrValue);
  189. }
  190. }
  191. return $this;
  192. }
  193. /**
  194. * getAttributes()
  195. *
  196. * @return array
  197. */
  198. public function getAttributes()
  199. {
  200. return $this->_attributes;
  201. }
  202. /**
  203. * setAttribute()
  204. *
  205. * @param string $name
  206. * @param mixed $value
  207. * @return Zend_Tool_Project_Profile_Resource_Container
  208. */
  209. public function setAttribute($name, $value)
  210. {
  211. $this->_attributes[$name] = $value;
  212. return $this;
  213. }
  214. /**
  215. * getAttribute()
  216. *
  217. * @param string $name
  218. * @return Zend_Tool_Project_Profile_Resource_Container
  219. */
  220. public function getAttribute($name)
  221. {
  222. return (array_key_exists($name, $this->_attributes)) ? $this->_attributes[$name] : null;
  223. }
  224. /**
  225. * hasAttribute()
  226. *
  227. * @param string $name
  228. * @return bool
  229. */
  230. public function hasAttribute($name)
  231. {
  232. return array_key_exists($name, $this->_attributes);
  233. }
  234. /**
  235. * setAppendable()
  236. *
  237. * @param bool $appendable
  238. * @return Zend_Tool_Project_Profile_Resource_Container
  239. */
  240. public function setAppendable($appendable)
  241. {
  242. $this->_appendable = (bool) $appendable;
  243. return $this;
  244. }
  245. /**
  246. * isAppendable()
  247. *
  248. * @return bool
  249. */
  250. public function isAppendable()
  251. {
  252. return $this->_appendable;
  253. }
  254. /**
  255. * setParentResource()
  256. *
  257. * @param Zend_Tool_Project_Profile_Resource_Container $parentResource
  258. * @return Zend_Tool_Project_Profile_Resource_Container
  259. */
  260. public function setParentResource(Zend_Tool_Project_Profile_Resource_Container $parentResource)
  261. {
  262. $this->_parentResource = $parentResource;
  263. return $this;
  264. }
  265. /**
  266. * getParentResource()
  267. *
  268. * @return Zend_Tool_Project_Profile_Resource_Container
  269. */
  270. public function getParentResource()
  271. {
  272. return $this->_parentResource;
  273. }
  274. /**
  275. * append()
  276. *
  277. * @param Zend_Tool_Project_Profile_Resource_Container $resource
  278. * @return Zend_Tool_Project_Profile_Resource_Container
  279. */
  280. public function append(Zend_Tool_Project_Profile_Resource_Container $resource)
  281. {
  282. if (!$this->isAppendable()) {
  283. throw new Exception('Resource by name ' . (string) $this . ' is not appendable');
  284. }
  285. array_push($this->_subResources, $resource);
  286. $resource->setParentResource($this);
  287. return $this;
  288. }
  289. /**
  290. * current() - required by RecursiveIterator
  291. *
  292. * @return Zend_Tool_Project_Profile_Resource
  293. */
  294. public function current()
  295. {
  296. return current($this->_subResources);
  297. }
  298. /**
  299. * key() - required by RecursiveIterator
  300. *
  301. * @return int
  302. */
  303. public function key()
  304. {
  305. return key($this->_subResources);
  306. }
  307. /**
  308. * next() - required by RecursiveIterator
  309. *
  310. * @return bool
  311. */
  312. public function next()
  313. {
  314. return next($this->_subResources);
  315. }
  316. /**
  317. * rewind() - required by RecursiveIterator
  318. *
  319. * @return bool
  320. */
  321. public function rewind()
  322. {
  323. return reset($this->_subResources);
  324. }
  325. /**
  326. * valid() - - required by RecursiveIterator
  327. *
  328. * @return bool
  329. */
  330. public function valid()
  331. {
  332. return (bool) $this->current();
  333. }
  334. /**
  335. * hasChildren()
  336. *
  337. * @return bool
  338. */
  339. public function hasChildren()
  340. {
  341. return (count($this->_subResources > 0)) ? true : false;
  342. }
  343. /**
  344. * getChildren()
  345. *
  346. * @return array
  347. */
  348. public function getChildren()
  349. {
  350. return $this->current();
  351. }
  352. /**
  353. * count()
  354. *
  355. * @return int
  356. */
  357. public function count()
  358. {
  359. return count($this->_subResources);
  360. }
  361. /**
  362. * __clone()
  363. *
  364. */
  365. public function __clone()
  366. {
  367. $this->rewind();
  368. foreach ($this->_subResources as $index => $resource) {
  369. $this->_subResources[$index] = clone $resource;
  370. }
  371. }
  372. }