PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

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