PageRenderTime 28ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Controller/Action/HelperPriorityStack.php

https://github.com/sidealice/zf2
PHP | 280 lines | 126 code | 35 blank | 119 comment | 20 complexity | d000ee9fc5982cbbfa16127d704ec317 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_Controller
  17. * @subpackage Zend_Controller_Action
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Controller\Action;
  25. /**
  26. * @uses \Zend\Controller\Action\Exception
  27. * @category Zend
  28. * @package Zend_Controller
  29. * @subpackage Zend_Controller_Action
  30. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class HelperPriorityStack implements \IteratorAggregate, \ArrayAccess, \Countable
  34. {
  35. protected $_helpersByPriority = array();
  36. protected $_helpersByNameRef = array();
  37. protected $_nextDefaultPriority = 1;
  38. /**
  39. * Magic property overloading for returning helper by name
  40. *
  41. * @param string $helperName The helper name
  42. * @return \Zend\Controller\Action\Helper\AbstractHelper
  43. */
  44. public function __get($helperName)
  45. {
  46. if (!array_key_exists($helperName, $this->_helpersByNameRef)) {
  47. return false;
  48. }
  49. return $this->_helpersByNameRef[$helperName];
  50. }
  51. /**
  52. * Magic property overloading for returning if helper is set by name
  53. *
  54. * @param string $helperName The helper name
  55. * @return \Zend\Controller\Action\Helper\AbstractHelper
  56. */
  57. public function __isset($helperName)
  58. {
  59. return array_key_exists($helperName, $this->_helpersByNameRef);
  60. }
  61. /**
  62. * Magic property overloading for unsetting if helper is exists by name
  63. *
  64. * @param string $helperName The helper name
  65. * @return \Zend\Controller\Action\Helper\AbstractHelper
  66. */
  67. public function __unset($helperName)
  68. {
  69. return $this->offsetUnset($helperName);
  70. }
  71. /**
  72. * push helper onto the stack
  73. *
  74. * @param \Zend\Controller\Action\Helper\AbstractHelper $helper
  75. * @return \Zend\Controller\Action\HelperBroker\PriorityStack
  76. */
  77. public function push(Helper\AbstractHelper $helper)
  78. {
  79. $this->offsetSet($this->getNextFreeHigherPriority(), $helper);
  80. return $this;
  81. }
  82. /**
  83. * Return something iterable
  84. *
  85. * @return array
  86. */
  87. public function getIterator()
  88. {
  89. return new \ArrayObject($this->_helpersByPriority);
  90. }
  91. /**
  92. * offsetExists()
  93. *
  94. * @param int|string $priorityOrHelperName
  95. * @return \Zend\Controller\Action\HelperBroker\PriorityStack
  96. */
  97. public function offsetExists($priorityOrHelperName)
  98. {
  99. if (is_string($priorityOrHelperName)) {
  100. return array_key_exists($priorityOrHelperName, $this->_helpersByNameRef);
  101. } else {
  102. return array_key_exists($priorityOrHelperName, $this->_helpersByPriority);
  103. }
  104. }
  105. /**
  106. * offsetGet()
  107. *
  108. * @param int|string $priorityOrHelperName
  109. * @return \Zend\Controller\Action\HelperBroker\PriorityStack
  110. */
  111. public function offsetGet($priorityOrHelperName)
  112. {
  113. if (!$this->offsetExists($priorityOrHelperName)) {
  114. throw new Action\Exception('A helper with priority ' . $priorityOrHelperName . ' does not exist.');
  115. }
  116. if (is_string($priorityOrHelperName)) {
  117. return $this->_helpersByNameRef[$priorityOrHelperName];
  118. } else {
  119. return $this->_helpersByPriority[$priorityOrHelperName];
  120. }
  121. }
  122. /**
  123. * offsetSet()
  124. *
  125. * @param int $priority
  126. * @param \Zend\Controller\Action\Helper\AbstractHelper $helper
  127. * @return \Zend\Controller\Action\HelperBroker\PriorityStack
  128. */
  129. public function offsetSet($priority, $helper)
  130. {
  131. $priority = (int) $priority;
  132. if (!$helper instanceof Helper\AbstractHelper) {
  133. throw new Exception('$helper must extend Zend\Controller\Action\Helper\AbstractHelper');
  134. }
  135. if (array_key_exists($helper->getName(), $this->_helpersByNameRef)) {
  136. // remove any object with the same name
  137. $this->offsetUnset($helper->getName());
  138. }
  139. if (array_key_exists($priority, $this->_helpersByPriority)) {
  140. $priority = $this->getNextFreeHigherPriority($priority); // ensures LIFO
  141. }
  142. $this->_helpersByPriority[$priority] = $helper;
  143. $this->_helpersByNameRef[$helper->getName()] = $helper;
  144. if ($priority == ($nextFreeDefault = $this->getNextFreeHigherPriority($this->_nextDefaultPriority))) {
  145. $this->_nextDefaultPriority = $nextFreeDefault;
  146. }
  147. krsort($this->_helpersByPriority); // always make sure priority and LIFO are both enforced
  148. return $this;
  149. }
  150. /**
  151. * offsetUnset()
  152. *
  153. * @param int|string $priorityOrHelperName Priority integer or the helper name
  154. * @return \Zend\Controller\Action\HelperBroker\PriorityStack
  155. */
  156. public function offsetUnset($priorityOrHelperName)
  157. {
  158. if (!$this->offsetExists($priorityOrHelperName)) {
  159. throw new Exception('A helper with priority or name ' . $priorityOrHelperName . ' does not exist.');
  160. }
  161. if (is_string($priorityOrHelperName)) {
  162. $helperName = $priorityOrHelperName;
  163. $helper = $this->_helpersByNameRef[$helperName];
  164. $priority = array_search($helper, $this->_helpersByPriority, true);
  165. } else {
  166. $priority = $priorityOrHelperName;
  167. $helperName = $this->_helpersByPriority[$priorityOrHelperName]->getName();
  168. }
  169. unset($this->_helpersByNameRef[$helperName]);
  170. unset($this->_helpersByPriority[$priority]);
  171. return $this;
  172. }
  173. /**
  174. * return the count of helpers
  175. *
  176. * @return int
  177. */
  178. public function count()
  179. {
  180. return count($this->_helpersByPriority);
  181. }
  182. /**
  183. * Find the next free higher priority. If an index is given, it will
  184. * find the next free highest priority after it.
  185. *
  186. * @param int $indexPriority OPTIONAL
  187. * @return int
  188. */
  189. public function getNextFreeHigherPriority($indexPriority = null)
  190. {
  191. if ($indexPriority == null) {
  192. $indexPriority = $this->_nextDefaultPriority;
  193. }
  194. $priorities = array_keys($this->_helpersByPriority);
  195. while (in_array($indexPriority, $priorities)) {
  196. $indexPriority++;
  197. }
  198. return $indexPriority;
  199. }
  200. /**
  201. * Find the next free lower priority. If an index is given, it will
  202. * find the next free lower priority before it.
  203. *
  204. * @param int $indexPriority
  205. * @return int
  206. */
  207. public function getNextFreeLowerPriority($indexPriority = null)
  208. {
  209. if ($indexPriority == null) {
  210. $indexPriority = $this->_nextDefaultPriority;
  211. }
  212. $priorities = array_keys($this->_helpersByPriority);
  213. while (in_array($indexPriority, $priorities)) {
  214. $indexPriority--;
  215. }
  216. return $indexPriority;
  217. }
  218. /**
  219. * return the highest priority
  220. *
  221. * @return int
  222. */
  223. public function getHighestPriority()
  224. {
  225. return max(array_keys($this->_helpersByPriority));
  226. }
  227. /**
  228. * return the lowest priority
  229. *
  230. * @return int
  231. */
  232. public function getLowestPriority()
  233. {
  234. return min(array_keys($this->_helpersByPriority));
  235. }
  236. /**
  237. * return the helpers referenced by name
  238. *
  239. * @return array
  240. */
  241. public function getHelpersByName()
  242. {
  243. return $this->_helpersByNameRef;
  244. }
  245. }