|
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 Dispatcher |
|
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: Abstract.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** Zend_Controller_Dispatcher_Interface */ |
|
24 require_once 'Zend/Controller/Dispatcher/Interface.php'; |
|
25 |
|
26 /** |
|
27 * @category Zend |
|
28 * @package Zend_Controller |
|
29 * @subpackage Dispatcher |
|
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
31 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
32 */ |
|
33 abstract class Zend_Controller_Dispatcher_Abstract implements Zend_Controller_Dispatcher_Interface |
|
34 { |
|
35 /** |
|
36 * Default action |
|
37 * @var string |
|
38 */ |
|
39 protected $_defaultAction = 'index'; |
|
40 |
|
41 /** |
|
42 * Default controller |
|
43 * @var string |
|
44 */ |
|
45 protected $_defaultController = 'index'; |
|
46 |
|
47 /** |
|
48 * Default module |
|
49 * @var string |
|
50 */ |
|
51 protected $_defaultModule = 'default'; |
|
52 |
|
53 /** |
|
54 * Front Controller instance |
|
55 * @var Zend_Controller_Front |
|
56 */ |
|
57 protected $_frontController; |
|
58 |
|
59 /** |
|
60 * Array of invocation parameters to use when instantiating action |
|
61 * controllers |
|
62 * @var array |
|
63 */ |
|
64 protected $_invokeParams = array(); |
|
65 |
|
66 /** |
|
67 * Path delimiter character |
|
68 * @var string |
|
69 */ |
|
70 protected $_pathDelimiter = '_'; |
|
71 |
|
72 /** |
|
73 * Response object to pass to action controllers, if any |
|
74 * @var Zend_Controller_Response_Abstract|null |
|
75 */ |
|
76 protected $_response = null; |
|
77 |
|
78 /** |
|
79 * Word delimiter characters |
|
80 * @var array |
|
81 */ |
|
82 protected $_wordDelimiter = array('-', '.'); |
|
83 |
|
84 /** |
|
85 * Constructor |
|
86 * |
|
87 * @return void |
|
88 */ |
|
89 public function __construct(array $params = array()) |
|
90 { |
|
91 $this->setParams($params); |
|
92 } |
|
93 |
|
94 /** |
|
95 * Formats a string into a controller name. This is used to take a raw |
|
96 * controller name, such as one stored inside a Zend_Controller_Request_Abstract |
|
97 * object, and reformat it to a proper class name that a class extending |
|
98 * Zend_Controller_Action would use. |
|
99 * |
|
100 * @param string $unformatted |
|
101 * @return string |
|
102 */ |
|
103 public function formatControllerName($unformatted) |
|
104 { |
|
105 return ucfirst($this->_formatName($unformatted)) . 'Controller'; |
|
106 } |
|
107 |
|
108 /** |
|
109 * Formats a string into an action name. This is used to take a raw |
|
110 * action name, such as one that would be stored inside a Zend_Controller_Request_Abstract |
|
111 * object, and reformat into a proper method name that would be found |
|
112 * inside a class extending Zend_Controller_Action. |
|
113 * |
|
114 * @param string $unformatted |
|
115 * @return string |
|
116 */ |
|
117 public function formatActionName($unformatted) |
|
118 { |
|
119 $formatted = $this->_formatName($unformatted, true); |
|
120 return strtolower(substr($formatted, 0, 1)) . substr($formatted, 1) . 'Action'; |
|
121 } |
|
122 |
|
123 /** |
|
124 * Verify delimiter |
|
125 * |
|
126 * Verify a delimiter to use in controllers or actions. May be a single |
|
127 * string or an array of strings. |
|
128 * |
|
129 * @param string|array $spec |
|
130 * @return array |
|
131 * @throws Zend_Controller_Dispatcher_Exception with invalid delimiters |
|
132 */ |
|
133 public function _verifyDelimiter($spec) |
|
134 { |
|
135 if (is_string($spec)) { |
|
136 return (array) $spec; |
|
137 } elseif (is_array($spec)) { |
|
138 $allStrings = true; |
|
139 foreach ($spec as $delim) { |
|
140 if (!is_string($delim)) { |
|
141 $allStrings = false; |
|
142 break; |
|
143 } |
|
144 } |
|
145 |
|
146 if (!$allStrings) { |
|
147 require_once 'Zend/Controller/Dispatcher/Exception.php'; |
|
148 throw new Zend_Controller_Dispatcher_Exception('Word delimiter array must contain only strings'); |
|
149 } |
|
150 |
|
151 return $spec; |
|
152 } |
|
153 |
|
154 require_once 'Zend/Controller/Dispatcher/Exception.php'; |
|
155 throw new Zend_Controller_Dispatcher_Exception('Invalid word delimiter'); |
|
156 } |
|
157 |
|
158 /** |
|
159 * Retrieve the word delimiter character(s) used in |
|
160 * controller or action names |
|
161 * |
|
162 * @return array |
|
163 */ |
|
164 public function getWordDelimiter() |
|
165 { |
|
166 return $this->_wordDelimiter; |
|
167 } |
|
168 |
|
169 /** |
|
170 * Set word delimiter |
|
171 * |
|
172 * Set the word delimiter to use in controllers and actions. May be a |
|
173 * single string or an array of strings. |
|
174 * |
|
175 * @param string|array $spec |
|
176 * @return Zend_Controller_Dispatcher_Abstract |
|
177 */ |
|
178 public function setWordDelimiter($spec) |
|
179 { |
|
180 $spec = $this->_verifyDelimiter($spec); |
|
181 $this->_wordDelimiter = $spec; |
|
182 |
|
183 return $this; |
|
184 } |
|
185 |
|
186 /** |
|
187 * Retrieve the path delimiter character(s) used in |
|
188 * controller names |
|
189 * |
|
190 * @return array |
|
191 */ |
|
192 public function getPathDelimiter() |
|
193 { |
|
194 return $this->_pathDelimiter; |
|
195 } |
|
196 |
|
197 /** |
|
198 * Set path delimiter |
|
199 * |
|
200 * Set the path delimiter to use in controllers. May be a single string or |
|
201 * an array of strings. |
|
202 * |
|
203 * @param string $spec |
|
204 * @return Zend_Controller_Dispatcher_Abstract |
|
205 */ |
|
206 public function setPathDelimiter($spec) |
|
207 { |
|
208 if (!is_string($spec)) { |
|
209 require_once 'Zend/Controller/Dispatcher/Exception.php'; |
|
210 throw new Zend_Controller_Dispatcher_Exception('Invalid path delimiter'); |
|
211 } |
|
212 $this->_pathDelimiter = $spec; |
|
213 |
|
214 return $this; |
|
215 } |
|
216 |
|
217 /** |
|
218 * Formats a string from a URI into a PHP-friendly name. |
|
219 * |
|
220 * By default, replaces words separated by the word separator character(s) |
|
221 * with camelCaps. If $isAction is false, it also preserves replaces words |
|
222 * separated by the path separation character with an underscore, making |
|
223 * the following word Title cased. All non-alphanumeric characters are |
|
224 * removed. |
|
225 * |
|
226 * @param string $unformatted |
|
227 * @param boolean $isAction Defaults to false |
|
228 * @return string |
|
229 */ |
|
230 protected function _formatName($unformatted, $isAction = false) |
|
231 { |
|
232 // preserve directories |
|
233 if (!$isAction) { |
|
234 $segments = explode($this->getPathDelimiter(), $unformatted); |
|
235 } else { |
|
236 $segments = (array) $unformatted; |
|
237 } |
|
238 |
|
239 foreach ($segments as $key => $segment) { |
|
240 $segment = str_replace($this->getWordDelimiter(), ' ', strtolower($segment)); |
|
241 $segment = preg_replace('/[^a-z0-9 ]/', '', $segment); |
|
242 $segments[$key] = str_replace(' ', '', ucwords($segment)); |
|
243 } |
|
244 |
|
245 return implode('_', $segments); |
|
246 } |
|
247 |
|
248 /** |
|
249 * Retrieve front controller instance |
|
250 * |
|
251 * @return Zend_Controller_Front |
|
252 */ |
|
253 public function getFrontController() |
|
254 { |
|
255 if (null === $this->_frontController) { |
|
256 require_once 'Zend/Controller/Front.php'; |
|
257 $this->_frontController = Zend_Controller_Front::getInstance(); |
|
258 } |
|
259 |
|
260 return $this->_frontController; |
|
261 } |
|
262 |
|
263 /** |
|
264 * Set front controller instance |
|
265 * |
|
266 * @param Zend_Controller_Front $controller |
|
267 * @return Zend_Controller_Dispatcher_Abstract |
|
268 */ |
|
269 public function setFrontController(Zend_Controller_Front $controller) |
|
270 { |
|
271 $this->_frontController = $controller; |
|
272 return $this; |
|
273 } |
|
274 |
|
275 /** |
|
276 * Add or modify a parameter to use when instantiating an action controller |
|
277 * |
|
278 * @param string $name |
|
279 * @param mixed $value |
|
280 * @return Zend_Controller_Dispatcher_Abstract |
|
281 */ |
|
282 public function setParam($name, $value) |
|
283 { |
|
284 $name = (string) $name; |
|
285 $this->_invokeParams[$name] = $value; |
|
286 return $this; |
|
287 } |
|
288 |
|
289 /** |
|
290 * Set parameters to pass to action controller constructors |
|
291 * |
|
292 * @param array $params |
|
293 * @return Zend_Controller_Dispatcher_Abstract |
|
294 */ |
|
295 public function setParams(array $params) |
|
296 { |
|
297 $this->_invokeParams = array_merge($this->_invokeParams, $params); |
|
298 return $this; |
|
299 } |
|
300 |
|
301 /** |
|
302 * Retrieve a single parameter from the controller parameter stack |
|
303 * |
|
304 * @param string $name |
|
305 * @return mixed |
|
306 */ |
|
307 public function getParam($name) |
|
308 { |
|
309 if(isset($this->_invokeParams[$name])) { |
|
310 return $this->_invokeParams[$name]; |
|
311 } |
|
312 |
|
313 return null; |
|
314 } |
|
315 |
|
316 /** |
|
317 * Retrieve action controller instantiation parameters |
|
318 * |
|
319 * @return array |
|
320 */ |
|
321 public function getParams() |
|
322 { |
|
323 return $this->_invokeParams; |
|
324 } |
|
325 |
|
326 /** |
|
327 * Clear the controller parameter stack |
|
328 * |
|
329 * By default, clears all parameters. If a parameter name is given, clears |
|
330 * only that parameter; if an array of parameter names is provided, clears |
|
331 * each. |
|
332 * |
|
333 * @param null|string|array single key or array of keys for params to clear |
|
334 * @return Zend_Controller_Dispatcher_Abstract |
|
335 */ |
|
336 public function clearParams($name = null) |
|
337 { |
|
338 if (null === $name) { |
|
339 $this->_invokeParams = array(); |
|
340 } elseif (is_string($name) && isset($this->_invokeParams[$name])) { |
|
341 unset($this->_invokeParams[$name]); |
|
342 } elseif (is_array($name)) { |
|
343 foreach ($name as $key) { |
|
344 if (is_string($key) && isset($this->_invokeParams[$key])) { |
|
345 unset($this->_invokeParams[$key]); |
|
346 } |
|
347 } |
|
348 } |
|
349 |
|
350 return $this; |
|
351 } |
|
352 |
|
353 /** |
|
354 * Set response object to pass to action controllers |
|
355 * |
|
356 * @param Zend_Controller_Response_Abstract|null $response |
|
357 * @return Zend_Controller_Dispatcher_Abstract |
|
358 */ |
|
359 public function setResponse(Zend_Controller_Response_Abstract $response = null) |
|
360 { |
|
361 $this->_response = $response; |
|
362 return $this; |
|
363 } |
|
364 |
|
365 /** |
|
366 * Return the registered response object |
|
367 * |
|
368 * @return Zend_Controller_Response_Abstract|null |
|
369 */ |
|
370 public function getResponse() |
|
371 { |
|
372 return $this->_response; |
|
373 } |
|
374 |
|
375 /** |
|
376 * Set the default controller (minus any formatting) |
|
377 * |
|
378 * @param string $controller |
|
379 * @return Zend_Controller_Dispatcher_Abstract |
|
380 */ |
|
381 public function setDefaultControllerName($controller) |
|
382 { |
|
383 $this->_defaultController = (string) $controller; |
|
384 return $this; |
|
385 } |
|
386 |
|
387 /** |
|
388 * Retrieve the default controller name (minus formatting) |
|
389 * |
|
390 * @return string |
|
391 */ |
|
392 public function getDefaultControllerName() |
|
393 { |
|
394 return $this->_defaultController; |
|
395 } |
|
396 |
|
397 /** |
|
398 * Set the default action (minus any formatting) |
|
399 * |
|
400 * @param string $action |
|
401 * @return Zend_Controller_Dispatcher_Abstract |
|
402 */ |
|
403 public function setDefaultAction($action) |
|
404 { |
|
405 $this->_defaultAction = (string) $action; |
|
406 return $this; |
|
407 } |
|
408 |
|
409 /** |
|
410 * Retrieve the default action name (minus formatting) |
|
411 * |
|
412 * @return string |
|
413 */ |
|
414 public function getDefaultAction() |
|
415 { |
|
416 return $this->_defaultAction; |
|
417 } |
|
418 |
|
419 /** |
|
420 * Set the default module |
|
421 * |
|
422 * @param string $module |
|
423 * @return Zend_Controller_Dispatcher_Abstract |
|
424 */ |
|
425 public function setDefaultModule($module) |
|
426 { |
|
427 $this->_defaultModule = (string) $module; |
|
428 return $this; |
|
429 } |
|
430 |
|
431 /** |
|
432 * Retrieve the default module |
|
433 * |
|
434 * @return string |
|
435 */ |
|
436 public function getDefaultModule() |
|
437 { |
|
438 return $this->_defaultModule; |
|
439 } |
|
440 } |