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