-
open/home/heiferftp/www/server/library/Zend/Controller/Dispatcher/Standard.php
Zend_Controller_Dispatcher_Standard->getControllerClass(Zend_Controller_Request_Http)
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-2008 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 */
21
22 /** Zend_Loader */
23 require_once 'Zend/Loader.php';
24
25 /** Zend_Controller_Dispatcher_Abstract */
26 require_once 'Zend/Controller/Dispatcher/Abstract.php';
27
28 /** Zend_Controller_Request_Abstract */
29 require_once 'Zend/Controller/Request/Abstract.php';
30
31 /** Zend_Controller_Response_Abstract */
32 require_once 'Zend/Controller/Response/Abstract.php';
33
34 /** Zend_Controller_Action */
35 require_once 'Zend/Controller/Action.php';
36
37 /**
38 * @category Zend
39 * @package Zend_Controller
40 * @subpackage Dispatcher
41 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
42 * @license http://framework.zend.com/license/new-bsd New BSD License
43 */
44 class Zend_Controller_Dispatcher_Standard extends Zend_Controller_Dispatcher_Abstract
45 {
46 /**
47 * Current dispatchable directory
48 * @var string
49 */
50 protected $_curDirectory;
51
52 /**
53 * Current module (formatted)
54 * @var string
55 */
56 protected $_curModule;
57
58 /**
59 * Controller directory(ies)
60 * @var array
61 */
62 protected $_controllerDirectory = array();
63
64 /**
65 * Constructor: Set current module to default value
66 *
67 * @param array $params
68 * @return void
69 */
70 public function __construct(array $params = array())
71 {
72 parent::__construct($params);
73 $this->_curModule = $this->getDefaultModule();
74 }
75
76 /**
77 * Add a single path to the controller directory stack
78 *
79 * @param string $path
80 * @param string $module
81 * @return Zend_Controller_Dispatcher_Standard
82 */
83 public function addControllerDirectory($path, $module = null)
84 {
85 if (null === $module) {
86 $module = $this->_defaultModule;
87 }
88
89 $module = (string) $module;
90 $path = rtrim((string) $path, '/\\');
91
92 $this->_controllerDirectory[$module] = $path;
93 return $this;
94 }
95
96 /**
97 * Set controller directory
98 *
99 * @param array|string $directory
100 * @return Zend_Controller_Dispatcher_Standard
101 */
102 public function setControllerDirectory($directory, $module = null)
103 {
104 $this->_controllerDirectory = array();
105
106 if (is_string($directory)) {
107 $this->addControllerDirectory($directory, $module);
108 } elseif (is_array($directory)) {
109 foreach ((array) $directory as $module => $path) {
110 $this->addControllerDirectory($path, $module);
111 }
112 } else {
113 throw new Zend_Controller_Exception('Controller directory spec must be either a string or an array');
114 }
115
116 return $this;
117 }
118
119 /**
120 * Return the currently set directories for Zend_Controller_Action class
121 * lookup
122 *
123 * If a module is specified, returns just that directory.
124 *
125 * @param string $module Module name
126 * @return array|string Returns array of all directories by default, single
127 * module directory if module argument provided
128 */
129 public function getControllerDirectory($module = null)
130 {
131 if (null === $module) {
132 return $this->_controllerDirectory;
133 }
134
135 $module = (string) $module;
136 if (array_key_exists($module, $this->_controllerDirectory)) {
137 return $this->_controllerDirectory[$module];
138 }
139
140 return null;
141 }
142
143 /**
144 * Remove a controller directory by module name
145 *
146 * @param string $module
147 * @return bool
148 */
149 public function removeControllerDirectory($module)
150 {
151 $module = (string) $module;
152 if (array_key_exists($module, $this->_controllerDirectory)) {
153 unset($this->_controllerDirectory[$module]);
154 return true;
155 }
156 return false;
157 }
158
159 /**
160 * Format the module name.
161 *
162 * @param string $unformatted
163 * @return string
164 */
165 public function formatModuleName($unformatted)
166 {
167 if (($this->_defaultModule == $unformatted) && !$this->getParam('prefixDefaultModule')) {
168 return $unformatted;
169 }
170
171 return ucfirst($this->_formatName($unformatted));
172 }
173
174 /**
175 * Format action class name
176 *
177 * @param string $moduleName Name of the current module
178 * @param string $className Name of the action class
179 * @return string Formatted class name
180 */
181 public function formatClassName($moduleName, $className)
182 {
183 return $this->formatModuleName($moduleName) . '_' . $className;
184 }
185
186 /**
187 * Convert a class name to a filename
188 *
189 * @param string $class
190 * @return string
191 */
192 public function classToFilename($class)
193 {
194 return str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
195 }
196
197 /**
198 * Returns TRUE if the Zend_Controller_Request_Abstract object can be
199 * dispatched to a controller.
200 *
201 * Use this method wisely. By default, the dispatcher will fall back to the
202 * default controller (either in the module specified or the global default)
203 * if a given controller does not exist. This method returning false does
204 * not necessarily indicate the dispatcher will not still dispatch the call.
205 *
206 * @param Zend_Controller_Request_Abstract $action
207 * @return boolean
208 */
209 public function isDispatchable(Zend_Controller_Request_Abstract $request)
210 {
211 $className = $this->getControllerClass($request);
212 if (!$className) {
213 return false;
214 }
215
216 if (class_exists($className, false)) {
217 return true;
218 }
219
220 $fileSpec = $this->classToFilename($className);
221 $dispatchDir = $this->getDispatchDirectory();
222 $test = $dispatchDir . DIRECTORY_SEPARATOR . $fileSpec;
223 return Zend_Loader::isReadable($test);
224 }
225
226 /**
227 * Dispatch to a controller/action
228 *
229 * By default, if a controller is not dispatchable, dispatch() will throw
230 * an exception. If you wish to use the default controller instead, set the
231 * param 'useDefaultControllerAlways' via {@link setParam()}.
232 *
233 * @param Zend_Controller_Request_Abstract $request
234 * @param Zend_Controller_Response_Abstract $response
235 * @return boolean
236 * @throws Zend_Controller_Dispatcher_Exception
237 */
238 public function dispatch(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response)
239 {
240 $this->setResponse($response);
241
242 /**
243 * Get controller class
244 */
245 if (!$this->isDispatchable($request)) {
246 $controller = $request->getControllerName();
247 if (!$this->getParam('useDefaultControllerAlways') && !empty($controller)) {
248 require_once 'Zend/Controller/Dispatcher/Exception.php';
249 throw new Zend_Controller_Dispatcher_Exception('Invalid controller specified (' . $request->getControllerName() . ')');
250 }
251
252 $className = $this->getDefaultControllerClass($request);
253 } else {
254 $className = $this->getControllerClass($request);
255 if (!$className) {
256 $className = $this->getDefaultControllerClass($request);
257 }
258 }
259
260 /**
261 * Load the controller class file
262 */
263 $className = $this->loadClass($className);
264
265 /**
266 * Instantiate controller with request, response, and invocation
267 * arguments; throw exception if it's not an action controller
268 */
269 $controller = new $className($request, $this->getResponse(), $this->getParams());
270 if (!$controller instanceof Zend_Controller_Action) {
271 require_once 'Zend/Controller/Dispatcher/Exception.php';
272 throw new Zend_Controller_Dispatcher_Exception("Controller '$className' is not an instance of Zend_Controller_Action");
273 }
274
275 /**
276 * Retrieve the action name
277 */
278 $action = $this->getActionMethod($request);
279
280 /**
281 * Dispatch the method call
282 */
283 $request->setDispatched(true);
284
285 // by default, buffer output
286 $disableOb = $this->getParam('disableOutputBuffering');
287 $obLevel = ob_get_level();
288 if (empty($disableOb)) {
289 ob_start();
290 }
291
292 try {
293 $controller->dispatch($action);
294 } catch (Exception $e) {
295 // Clean output buffer on error
296 $curObLevel = ob_get_level();
297 if ($curObLevel > $obLevel) {
298 do {
299 ob_get_clean();
300 $curObLevel = ob_get_level();
301 } while ($curObLevel > $obLevel);
302 }
303
304 throw $e;
305 }
306
307 if (empty($disableOb)) {
308 $content = ob_get_clean();
309 $response->appendBody($content);
310 }
311
312 // Destroy the page controller instance and reflection objects
313 $controller = null;
314 }
315
316 /**
317 * Load a controller class
318 *
319 * Attempts to load the controller class file from
320 * {@link getControllerDirectory()}. If the controller belongs to a
321 * module, looks for the module prefix to the controller class.
322 *
323 * @param string $className
324 * @return string Class name loaded
325 * @throws Zend_Controller_Dispatcher_Exception if class not loaded
326 */
327 public function loadClass($className)
328 {
329 $finalClass = $className;
330 if (($this->_defaultModule != $this->_curModule)
331 || $this->getParam('prefixDefaultModule'))
332 {
333 $finalClass = $this->formatClassName($this->_curModule, $className);
334 }
335 if (class_exists($finalClass, false)) {
336 return $finalClass;
337 }
338
339 $dispatchDir = $this->getDispatchDirectory();
340 $loadFile = $dispatchDir . DIRECTORY_SEPARATOR . $this->classToFilename($className);
341 $dir = dirname($loadFile);
342 $file = basename($loadFile);
343
344 try {
345 Zend_Loader::loadFile($file, $dir, true);
346 } catch (Zend_Exception $e) {
347 require_once 'Zend/Controller/Dispatcher/Exception.php';
348 throw new Zend_Controller_Dispatcher_Exception('Cannot load controller class "' . $className . '" from file "' . $file . '" in directory "' . $dir . '"');
349 }
350
351 if (!class_exists($finalClass, false)) {
352 require_once 'Zend/Controller/Dispatcher/Exception.php';
353 throw new Zend_Controller_Dispatcher_Exception('Invalid controller class ("' . $finalClass . '")');
354 }
355
356 return $finalClass;
357 }
358
359 /**
360 * Get controller class name
361 *
362 * Try request first; if not found, try pulling from request parameter;
363 * if still not found, fallback to default
364 *
365 * @param Zend_Controller_Request_Abstract $request
366 * @return string|false Returns class name on success
367 */
368 public function getControllerClass(Zend_Controller_Request_Abstract $request)
369 {
370 $controllerName = $request->getControllerName();
371 if (empty($controllerName)) {
372 if (!$this->getParam('useDefaultControllerAlways')) {
373 return false;
374 }
375 $controllerName = $this->getDefaultControllerName();
376 $request->setControllerName($controllerName);
377 }
378
379 $className = $this->formatControllerName($controllerName);
380
381 $controllerDirs = $this->getControllerDirectory();
382 $module = $request->getModuleName();
383 if ($this->isValidModule($module)) {
384 $this->_curModule = $module;
385 $this->_curDirectory = $controllerDirs[$module];
386 } elseif ($this->isValidModule($this->_defaultModule)) {
387 $request->setModuleName($this->_defaultModule);
388 $this->_curModule = $this->_defaultModule;
389 $this->_curDirectory = $controllerDirs[$this->_defaultModule];
390 } else {
391 require_once 'Zend/Controller/Exception.php';
392 throw new Zend_Controller_Exception('No default module defined for this application');
393 }
394
395 return $className;
396 }
397
398 /**
399 * Determine if a given module is valid
400 *
401 * @param string $module
402 * @return bool
403 */
404 public function isValidModule($module)
405 {
406 if (!is_string($module)) {
407 return false;
408 }
409
410 $module = strtolower($module);
411 $controllerDir = $this->getControllerDirectory();
412 foreach (array_keys($controllerDir) as $moduleName) {
413 if ($module == strtolower($moduleName)) {
414 return true;
415 }
416 }
417
418 return false;
419 }
420
421 /**
422 * Retrieve default controller class
423 *
424 * Determines whether the default controller to use lies within the
425 * requested module, or if the global default should be used.
426 *
427 * By default, will only use the module default unless that controller does
428 * not exist; if this is the case, it falls back to the default controller
429 * in the default module.
430 *
431 * @param Zend_Controller_Request_Abstract $request
432 * @return string
433 */
434 public function getDefaultControllerClass(Zend_Controller_Request_Abstract $request)
435 {
436 $controller = $this->getDefaultControllerName();
437 $default = $this->formatControllerName($controller);
438 $request->setControllerName($controller)
439 ->setActionName(null);
440
441 $module = $request->getModuleName();
442 $controllerDirs = $this->getControllerDirectory();
443 $this->_curModule = $this->_defaultModule;
444 $this->_curDirectory = $controllerDirs[$this->_defaultModule];
445 if ($this->isValidModule($module)) {
446 $found = false;
447 if (class_exists($default, false)) {
448 $found = true;
449 } else {
450 $moduleDir = $controllerDirs[$module];
451 $fileSpec = $moduleDir . DIRECTORY_SEPARATOR . $this->classToFilename($default);
452 if (Zend_Loader::isReadable($fileSpec)) {
453 $found = true;
454 $this->_curDirectory = $moduleDir;
455 }
456 }
457 if ($found) {
458 $request->setModuleName($module);
459 $this->_curModule = $this->formatModuleName($module);
460 }
461 } else {
462 $request->setModuleName($this->_defaultModule);
463 }
464
465 return $default;
466 }
467
468 /**
469 * Return the value of the currently selected dispatch directory (as set by
470 * {@link getController()})
471 *
472 * @return string
473 */
474 public function getDispatchDirectory()
475 {
476 return $this->_curDirectory;
477 }
478
479 /**
480 * Determine the action name
481 *
482 * First attempt to retrieve from request; then from request params
483 * using action key; default to default action
484 *
485 * Returns formatted action name
486 *
487 * @param Zend_Controller_Request_Abstract $request
488 * @return string
489 */
490 public function getActionMethod(Zend_Controller_Request_Abstract $request)
491 {
492 $action = $request->getActionName();
493 if (empty($action)) {
494 $action = $this->getDefaultAction();
495 $request->setActionName($action);
496 }
497
498 return $this->formatActionName($action);
499 }
500 }
501
-
open/home/heiferftp/www/server/library/Zend/Controller/Dispatcher/Standard.php
Zend_Controller_Dispatcher_Standard->isDispatchable(Zend_Controller_Request_Http)
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-2008 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 */
21
22 /** Zend_Loader */
23 require_once 'Zend/Loader.php';
24
25 /** Zend_Controller_Dispatcher_Abstract */
26 require_once 'Zend/Controller/Dispatcher/Abstract.php';
27
28 /** Zend_Controller_Request_Abstract */
29 require_once 'Zend/Controller/Request/Abstract.php';
30
31 /** Zend_Controller_Response_Abstract */
32 require_once 'Zend/Controller/Response/Abstract.php';
33
34 /** Zend_Controller_Action */
35 require_once 'Zend/Controller/Action.php';
36
37 /**
38 * @category Zend
39 * @package Zend_Controller
40 * @subpackage Dispatcher
41 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
42 * @license http://framework.zend.com/license/new-bsd New BSD License
43 */
44 class Zend_Controller_Dispatcher_Standard extends Zend_Controller_Dispatcher_Abstract
45 {
46 /**
47 * Current dispatchable directory
48 * @var string
49 */
50 protected $_curDirectory;
51
52 /**
53 * Current module (formatted)
54 * @var string
55 */
56 protected $_curModule;
57
58 /**
59 * Controller directory(ies)
60 * @var array
61 */
62 protected $_controllerDirectory = array();
63
64 /**
65 * Constructor: Set current module to default value
66 *
67 * @param array $params
68 * @return void
69 */
70 public function __construct(array $params = array())
71 {
72 parent::__construct($params);
73 $this->_curModule = $this->getDefaultModule();
74 }
75
76 /**
77 * Add a single path to the controller directory stack
78 *
79 * @param string $path
80 * @param string $module
81 * @return Zend_Controller_Dispatcher_Standard
82 */
83 public function addControllerDirectory($path, $module = null)
84 {
85 if (null === $module) {
86 $module = $this->_defaultModule;
87 }
88
89 $module = (string) $module;
90 $path = rtrim((string) $path, '/\\');
91
92 $this->_controllerDirectory[$module] = $path;
93 return $this;
94 }
95
96 /**
97 * Set controller directory
98 *
99 * @param array|string $directory
100 * @return Zend_Controller_Dispatcher_Standard
101 */
102 public function setControllerDirectory($directory, $module = null)
103 {
104 $this->_controllerDirectory = array();
105
106 if (is_string($directory)) {
107 $this->addControllerDirectory($directory, $module);
108 } elseif (is_array($directory)) {
109 foreach ((array) $directory as $module => $path) {
110 $this->addControllerDirectory($path, $module);
111 }
112 } else {
113 throw new Zend_Controller_Exception('Controller directory spec must be either a string or an array');
114 }
115
116 return $this;
117 }
118
119 /**
120 * Return the currently set directories for Zend_Controller_Action class
121 * lookup
122 *
123 * If a module is specified, returns just that directory.
124 *
125 * @param string $module Module name
126 * @return array|string Returns array of all directories by default, single
127 * module directory if module argument provided
128 */
129 public function getControllerDirectory($module = null)
130 {
131 if (null === $module) {
132 return $this->_controllerDirectory;
133 }
134
135 $module = (string) $module;
136 if (array_key_exists($module, $this->_controllerDirectory)) {
137 return $this->_controllerDirectory[$module];
138 }
139
140 return null;
141 }
142
143 /**
144 * Remove a controller directory by module name
145 *
146 * @param string $module
147 * @return bool
148 */
149 public function removeControllerDirectory($module)
150 {
151 $module = (string) $module;
152 if (array_key_exists($module, $this->_controllerDirectory)) {
153 unset($this->_controllerDirectory[$module]);
154 return true;
155 }
156 return false;
157 }
158
159 /**
160 * Format the module name.
161 *
162 * @param string $unformatted
163 * @return string
164 */
165 public function formatModuleName($unformatted)
166 {
167 if (($this->_defaultModule == $unformatted) && !$this->getParam('prefixDefaultModule')) {
168 return $unformatted;
169 }
170
171 return ucfirst($this->_formatName($unformatted));
172 }
173
174 /**
175 * Format action class name
176 *
177 * @param string $moduleName Name of the current module
178 * @param string $className Name of the action class
179 * @return string Formatted class name
180 */
181 public function formatClassName($moduleName, $className)
182 {
183 return $this->formatModuleName($moduleName) . '_' . $className;
184 }
185
186 /**
187 * Convert a class name to a filename
188 *
189 * @param string $class
190 * @return string
191 */
192 public function classToFilename($class)
193 {
194 return str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
195 }
196
197 /**
198 * Returns TRUE if the Zend_Controller_Request_Abstract object can be
199 * dispatched to a controller.
200 *
201 * Use this method wisely. By default, the dispatcher will fall back to the
202 * default controller (either in the module specified or the global default)
203 * if a given controller does not exist. This method returning false does
204 * not necessarily indicate the dispatcher will not still dispatch the call.
205 *
206 * @param Zend_Controller_Request_Abstract $action
207 * @return boolean
208 */
209 public function isDispatchable(Zend_Controller_Request_Abstract $request)
210 {
211 $className = $this->getControllerClass($request);
212 if (!$className) {
213 return false;
214 }
215
216 if (class_exists($className, false)) {
217 return true;
218 }
219
220 $fileSpec = $this->classToFilename($className);
221 $dispatchDir = $this->getDispatchDirectory();
222 $test = $dispatchDir . DIRECTORY_SEPARATOR . $fileSpec;
223 return Zend_Loader::isReadable($test);
224 }
225
226 /**
227 * Dispatch to a controller/action
228 *
229 * By default, if a controller is not dispatchable, dispatch() will throw
230 * an exception. If you wish to use the default controller instead, set the
231 * param 'useDefaultControllerAlways' via {@link setParam()}.
232 *
233 * @param Zend_Controller_Request_Abstract $request
234 * @param Zend_Controller_Response_Abstract $response
235 * @return boolean
236 * @throws Zend_Controller_Dispatcher_Exception
237 */
238 public function dispatch(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response)
239 {
240 $this->setResponse($response);
241
242 /**
243 * Get controller class
244 */
245 if (!$this->isDispatchable($request)) {
246 $controller = $request->getControllerName();
247 if (!$this->getParam('useDefaultControllerAlways') && !empty($controller)) {
248 require_once 'Zend/Controller/Dispatcher/Exception.php';
249 throw new Zend_Controller_Dispatcher_Exception('Invalid controller specified (' . $request->getControllerName() . ')');
250 }
251
252 $className = $this->getDefaultControllerClass($request);
253 } else {
254 $className = $this->getControllerClass($request);
255 if (!$className) {
256 $className = $this->getDefaultControllerClass($request);
257 }
258 }
259
260 /**
261 * Load the controller class file
262 */
263 $className = $this->loadClass($className);
264
265 /**
266 * Instantiate controller with request, response, and invocation
267 * arguments; throw exception if it's not an action controller
268 */
269 $controller = new $className($request, $this->getResponse(), $this->getParams());
270 if (!$controller instanceof Zend_Controller_Action) {
271 require_once 'Zend/Controller/Dispatcher/Exception.php';
272 throw new Zend_Controller_Dispatcher_Exception("Controller '$className' is not an instance of Zend_Controller_Action");
273 }
274
275 /**
276 * Retrieve the action name
277 */
278 $action = $this->getActionMethod($request);
279
280 /**
281 * Dispatch the method call
282 */
283 $request->setDispatched(true);
284
285 // by default, buffer output
286 $disableOb = $this->getParam('disableOutputBuffering');
287 $obLevel = ob_get_level();
288 if (empty($disableOb)) {
289 ob_start();
290 }
291
292 try {
293 $controller->dispatch($action);
294 } catch (Exception $e) {
295 // Clean output buffer on error
296 $curObLevel = ob_get_level();
297 if ($curObLevel > $obLevel) {
298 do {
299 ob_get_clean();
300 $curObLevel = ob_get_level();
301 } while ($curObLevel > $obLevel);
302 }
303
304 throw $e;
305 }
306
307 if (empty($disableOb)) {
308 $content = ob_get_clean();
309 $response->appendBody($content);
310 }
311
312 // Destroy the page controller instance and reflection objects
313 $controller = null;
314 }
315
316 /**
317 * Load a controller class
318 *
319 * Attempts to load the controller class file from
320 * {@link getControllerDirectory()}. If the controller belongs to a
321 * module, looks for the module prefix to the controller class.
322 *
323 * @param string $className
324 * @return string Class name loaded
325 * @throws Zend_Controller_Dispatcher_Exception if class not loaded
326 */
327 public function loadClass($className)
328 {
329 $finalClass = $className;
330 if (($this->_defaultModule != $this->_curModule)
331 || $this->getParam('prefixDefaultModule'))
332 {
333 $finalClass = $this->formatClassName($this->_curModule, $className);
334 }
335 if (class_exists($finalClass, false)) {
336 return $finalClass;
337 }
338
339 $dispatchDir = $this->getDispatchDirectory();
340 $loadFile = $dispatchDir . DIRECTORY_SEPARATOR . $this->classToFilename($className);
341 $dir = dirname($loadFile);
342 $file = basename($loadFile);
343
344 try {
345 Zend_Loader::loadFile($file, $dir, true);
346 } catch (Zend_Exception $e) {
347 require_once 'Zend/Controller/Dispatcher/Exception.php';
348 throw new Zend_Controller_Dispatcher_Exception('Cannot load controller class "' . $className . '" from file "' . $file . '" in directory "' . $dir . '"');
349 }
350
351 if (!class_exists($finalClass, false)) {
352 require_once 'Zend/Controller/Dispatcher/Exception.php';
353 throw new Zend_Controller_Dispatcher_Exception('Invalid controller class ("' . $finalClass . '")');
354 }
355
356 return $finalClass;
357 }
358
359 /**
360 * Get controller class name
361 *
362 * Try request first; if not found, try pulling from request parameter;
363 * if still not found, fallback to default
364 *
365 * @param Zend_Controller_Request_Abstract $request
366 * @return string|false Returns class name on success
367 */
368 public function getControllerClass(Zend_Controller_Request_Abstract $request)
369 {
370 $controllerName = $request->getControllerName();
371 if (empty($controllerName)) {
372 if (!$this->getParam('useDefaultControllerAlways')) {
373 return false;
374 }
375 $controllerName = $this->getDefaultControllerName();
376 $request->setControllerName($controllerName);
377 }
378
379 $className = $this->formatControllerName($controllerName);
380
381 $controllerDirs = $this->getControllerDirectory();
382 $module = $request->getModuleName();
383 if ($this->isValidModule($module)) {
384 $this->_curModule = $module;
385 $this->_curDirectory = $controllerDirs[$module];
386 } elseif ($this->isValidModule($this->_defaultModule)) {
387 $request->setModuleName($this->_defaultModule);
388 $this->_curModule = $this->_defaultModule;
389 $this->_curDirectory = $controllerDirs[$this->_defaultModule];
390 } else {
391 require_once 'Zend/Controller/Exception.php';
392 throw new Zend_Controller_Exception('No default module defined for this application');
393 }
394
395 return $className;
396 }
397
398 /**
399 * Determine if a given module is valid
400 *
401 * @param string $module
402 * @return bool
403 */
404 public function isValidModule($module)
405 {
406 if (!is_string($module)) {
407 return false;
408 }
409
410 $module = strtolower($module);
411 $controllerDir = $this->getControllerDirectory();
412 foreach (array_keys($controllerDir) as $moduleName) {
413 if ($module == strtolower($moduleName)) {
414 return true;
415 }
416 }
417
418 return false;
419 }
420
421 /**
422 * Retrieve default controller class
423 *
424 * Determines whether the default controller to use lies within the
425 * requested module, or if the global default should be used.
426 *
427 * By default, will only use the module default unless that controller does
428 * not exist; if this is the case, it falls back to the default controller
429 * in the default module.
430 *
431 * @param Zend_Controller_Request_Abstract $request
432 * @return string
433 */
434 public function getDefaultControllerClass(Zend_Controller_Request_Abstract $request)
435 {
436 $controller = $this->getDefaultControllerName();
437 $default = $this->formatControllerName($controller);
438 $request->setControllerName($controller)
439 ->setActionName(null);
440
441 $module = $request->getModuleName();
442 $controllerDirs = $this->getControllerDirectory();
443 $this->_curModule = $this->_defaultModule;
444 $this->_curDirectory = $controllerDirs[$this->_defaultModule];
445 if ($this->isValidModule($module)) {
446 $found = false;
447 if (class_exists($default, false)) {
448 $found = true;
449 } else {
450 $moduleDir = $controllerDirs[$module];
451 $fileSpec = $moduleDir . DIRECTORY_SEPARATOR . $this->classToFilename($default);
452 if (Zend_Loader::isReadable($fileSpec)) {
453 $found = true;
454 $this->_curDirectory = $moduleDir;
455 }
456 }
457 if ($found) {
458 $request->setModuleName($module);
459 $this->_curModule = $this->formatModuleName($module);
460 }
461 } else {
462 $request->setModuleName($this->_defaultModule);
463 }
464
465 return $default;
466 }
467
468 /**
469 * Return the value of the currently selected dispatch directory (as set by
470 * {@link getController()})
471 *
472 * @return string
473 */
474 public function getDispatchDirectory()
475 {
476 return $this->_curDirectory;
477 }
478
479 /**
480 * Determine the action name
481 *
482 * First attempt to retrieve from request; then from request params
483 * using action key; default to default action
484 *
485 * Returns formatted action name
486 *
487 * @param Zend_Controller_Request_Abstract $request
488 * @return string
489 */
490 public function getActionMethod(Zend_Controller_Request_Abstract $request)
491 {
492 $action = $request->getActionName();
493 if (empty($action)) {
494 $action = $this->getDefaultAction();
495 $request->setActionName($action);
496 }
497
498 return $this->formatActionName($action);
499 }
500 }
501
-
open/home/heiferftp/www/server/library/Zend/Controller/Front.php
Zend_Controller_Dispatcher_Standard->dispatch(Zend_Controller_Request_Http, Zend_Controller_Response_Http)
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 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 */
20
21
22 /** Zend_Loader */
23 require_once 'Zend/Loader.php';
24
25 /** Zend_Controller_Action_HelperBroker */
26 require_once 'Zend/Controller/Action/HelperBroker.php';
27
28 /** Zend_Controller_Exception */
29 require_once 'Zend/Controller/Exception.php';
30
31 /** Zend_Controller_Plugin_Broker */
32 require_once 'Zend/Controller/Plugin/Broker.php';
33
34 /** Zend_Controller_Request_Abstract */
35 require_once 'Zend/Controller/Request/Abstract.php';
36
37 /** Zend_Controller_Router_Interface */
38 require_once 'Zend/Controller/Router/Interface.php';
39
40 /** Zend_Controller_Dispatcher_Interface */
41 require_once 'Zend/Controller/Dispatcher/Interface.php';
42
43 /** Zend_Controller_Response_Abstract */
44 require_once 'Zend/Controller/Response/Abstract.php';
45
46 /**
47 * @category Zend
48 * @package Zend_Controller
49 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
50 * @license http://framework.zend.com/license/new-bsd New BSD License
51 */
52 class Zend_Controller_Front
53 {
54 /**
55 * Base URL
56 * @var string
57 */
58 protected $_baseUrl = null;
59
60 /**
61 * Directory|ies where controllers are stored
62 *
63 * @var string|array
64 */
65 protected $_controllerDir = null;
66
67 /**
68 * Instance of Zend_Controller_Dispatcher_Interface
69 * @var Zend_Controller_Dispatcher_Interface
70 */
71 protected $_dispatcher = null;
72
73 /**
74 * Singleton instance
75 *
76 * Marked only as protected to allow extension of the class. To extend,
77 * simply override {@link getInstance()}.
78 *
79 * @var Zend_Controller_Front
80 */
81 protected static $_instance = null;
82
83 /**
84 * Array of invocation parameters to use when instantiating action
85 * controllers
86 * @var array
87 */
88 protected $_invokeParams = array();
89
90 /**
91 * Subdirectory within a module containing controllers; defaults to 'controllers'
92 * @var string
93 */
94 protected $_moduleControllerDirectoryName = 'controllers';
95
96 /**
97 * Instance of Zend_Controller_Plugin_Broker
98 * @var Zend_Controller_Plugin_Broker
99 */
100 protected $_plugins = null;
101
102 /**
103 * Instance of Zend_Controller_Request_Abstract
104 * @var Zend_Controller_Request_Abstract
105 */
106 protected $_request = null;
107
108 /**
109 * Instance of Zend_Controller_Response_Abstract
110 * @var Zend_Controller_Response_Abstract
111 */
112 protected $_response = null;
113
114 /**
115 * Whether or not to return the response prior to rendering output while in
116 * {@link dispatch()}; default is to send headers and render output.
117 * @var boolean
118 */
119 protected $_returnResponse = false;
120
121 /**
122 * Instance of Zend_Controller_Router_Interface
123 * @var Zend_Controller_Router_Interface
124 */
125 protected $_router = null;
126
127 /**
128 * Whether or not exceptions encountered in {@link dispatch()} should be
129 * thrown or trapped in the response object
130 * @var boolean
131 */
132 protected $_throwExceptions = false;
133
134 /**
135 * Constructor
136 *
137 * Instantiate using {@link getInstance()}; front controller is a singleton
138 * object.
139 *
140 * Instantiates the plugin broker.
141 *
142 * @return void
143 */
144 protected function __construct()
145 {
146 $this->_plugins = new Zend_Controller_Plugin_Broker();
147 }
148
149 /**
150 * Enforce singleton; disallow cloning
151 *
152 * @return void
153 */
154 private function __clone()
155 {
156 }
157
158 /**
159 * Singleton instance
160 *
161 * @return Zend_Controller_Front
162 */
163 public static function getInstance()
164 {
165 if (null === self::$_instance) {
166 self::$_instance = new self();
167 }
168
169 return self::$_instance;
170 }
171
172 /**
173 * Resets all object properties of the singleton instance
174 *
175 * Primarily used for testing; could be used to chain front controllers.
176 *
177 * Also resets action helper broker, clearing all registered helpers.
178 *
179 * @return void
180 */
181 public function resetInstance()
182 {
183 $reflection = new ReflectionObject($this);
184 foreach ($reflection->getProperties() as $property) {
185 $name = $property->getName();
186 switch ($name) {
187 case '_instance':
188 break;
189 case '_controllerDir':
190 case '_invokeParams':
191 $this->{$name} = array();
192 break;
193 case '_plugins':
194 $this->{$name} = new Zend_Controller_Plugin_Broker();
195 break;
196 case '_throwExceptions':
197 case '_returnResponse':
198 $this->{$name} = false;
199 break;
200 case '_moduleControllerDirectoryName':
201 $this->{$name} = 'controllers';
202 break;
203 default:
204 $this->{$name} = null;
205 break;
206 }
207 }
208 Zend_Controller_Action_HelperBroker::resetHelpers();
209 }
210
211 /**
212 * Convenience feature, calls setControllerDirectory()->setRouter()->dispatch()
213 *
214 * In PHP 5.1.x, a call to a static method never populates $this -- so run()
215 * may actually be called after setting up your front controller.
216 *
217 * @param string|array $controllerDirectory Path to Zend_Controller_Action
218 * controller classes or array of such paths
219 * @return void
220 * @throws Zend_Controller_Exception if called from an object instance
221 */
222 public static function run($controllerDirectory)
223 {
224 self::getInstance()
225 ->setControllerDirectory($controllerDirectory)
226 ->dispatch();
227 }
228
229 /**
230 * Add a controller directory to the controller directory stack
231 *
232 * If $args is presented and is a string, uses it for the array key mapping
233 * to the directory specified.
234 *
235 * @param string $directory
236 * @param string $module Optional argument; module with which to associate directory. If none provided, assumes 'default'
237 * @return Zend_Controller_Front
238 * @throws Zend_Controller_Exception if directory not found or readable
239 */
240 public function addControllerDirectory($directory, $module = null)
241 {
242 $this->getDispatcher()->addControllerDirectory($directory, $module);
243 return $this;
244 }
245
246 /**
247 * Set controller directory
248 *
249 * Stores controller directory(ies) in dispatcher. May be an array of
250 * directories or a string containing a single directory.
251 *
252 * @param string|array $directory Path to Zend_Controller_Action controller
253 * classes or array of such paths
254 * @param string $module Optional module name to use with string $directory
255 * @return Zend_Controller_Front
256 */
257 public function setControllerDirectory($directory, $module = null)
258 {
259 $this->getDispatcher()->setControllerDirectory($directory, $module);
260 return $this;
261 }
262
263 /**
264 * Retrieve controller directory
265 *
266 * Retrieves:
267 * - Array of all controller directories if no $name passed
268 * - String path if $name passed and exists as a key in controller directory array
269 * - null if $name passed but does not exist in controller directory keys
270 *
271 * @param string $name Default null
272 * @return array|string|null
273 */
274 public function getControllerDirectory($name = null)
275 {
276 return $this->getDispatcher()->getControllerDirectory($name);
277 }
278
279 /**
280 * Remove a controller directory by module name
281 *
282 * @param string $module
283 * @return bool
284 */
285 public function removeControllerDirectory($module)
286 {
287 return $this->getDispatcher()->removeControllerDirectory($module);
288 }
289
290 /**
291 * Specify a directory as containing modules
292 *
293 * Iterates through the directory, adding any subdirectories as modules;
294 * the subdirectory within each module named after {@link $_moduleControllerDirectoryName}
295 * will be used as the controller directory path.
296 *
297 * @param string $path
298 * @return Zend_Controller_Front
299 */
300 public function addModuleDirectory($path)
301 {
302 try{
303 $dir = new DirectoryIterator($path);
304 }catch(Exception $e){
305 throw new Zend_Controller_Exception("Directory $path not readable");
306 }
307 foreach ($dir as $file) {
308 if ($file->isDot() || !$file->isDir()) {
309 continue;
310 }
311
312 $module = $file->getFilename();
313
314 // Don't use SCCS directories as modules
315 if (preg_match('/^[^a-z]/i', $module) || ('CVS' == $module)) {
316 continue;
317 }
318
319 $moduleDir = $file->getPathname() . DIRECTORY_SEPARATOR . $this->getModuleControllerDirectoryName();
320 $this->addControllerDirectory($moduleDir, $module);
321 }
322
323 return $this;
324 }
325
326 /**
327 * Return the path to a module directory (but not the controllers directory within)
328 *
329 * @param string $module
330 * @return string|null
331 */
332 public function getModuleDirectory($module = null)
333 {
334 if (null === $module) {
335 $request = $this->getRequest();
336 if (null !== $request) {
337 $module = $this->getRequest()->getModuleName();
338 }
339 if (empty($module)) {
340 $module = $this->getDispatcher()->getDefaultModule();
341 }
342 }
343
344 $controllerDir = $this->getControllerDirectory($module);
345
346 if ((null === $controllerDir) || !is_string($controllerDir)) {
347 return null;
348 }
349
350 return dirname($controllerDir);
351 }
352
353 /**
354 * Set the directory name within a module containing controllers
355 *
356 * @param string $name
357 * @return Zend_Controller_Front
358 */
359 public function setModuleControllerDirectoryName($name = 'controllers')
360 {
361 $this->_moduleControllerDirectoryName = (string) $name;
362
363 return $this;
364 }
365
366 /**
367 * Return the directory name within a module containing controllers
368 *
369 * @return string
370 */
371 public function getModuleControllerDirectoryName()
372 {
373 return $this->_moduleControllerDirectoryName;
374 }
375
376 /**
377 * Set the default controller (unformatted string)
378 *
379 * @param string $controller
380 * @return Zend_Controller_Front
381 */
382 public function setDefaultControllerName($controller)
383 {
384 $dispatcher = $this->getDispatcher();
385 $dispatcher->setDefaultControllerName($controller);
386 return $this;
387 }
388
389 /**
390 * Retrieve the default controller (unformatted string)
391 *
392 * @return string
393 */
394 public function getDefaultControllerName()
395 {
396 return $this->getDispatcher()->getDefaultControllerName();
397 }
398
399 /**
400 * Set the default action (unformatted string)
401 *
402 * @param string $action
403 * @return Zend_Controller_Front
404 */
405 public function setDefaultAction($action)
406 {
407 $dispatcher = $this->getDispatcher();
408 $dispatcher->setDefaultAction($action);
409 return $this;
410 }
411
412 /**
413 * Retrieve the default action (unformatted string)
414 *
415 * @return string
416 */
417 public function getDefaultAction()
418 {
419 return $this->getDispatcher()->getDefaultAction();
420 }
421
422 /**
423 * Set the default module name
424 *
425 * @param string $module
426 * @return Zend_Controller_Front
427 */
428 public function setDefaultModule($module)
429 {
430 $dispatcher = $this->getDispatcher();
431 $dispatcher->setDefaultModule($module);
432 return $this;
433 }
434
435 /**
436 * Retrieve the default module
437 *
438 * @return string
439 */
440 public function getDefaultModule()
441 {
442 return $this->getDispatcher()->getDefaultModule();
443 }
444
445 /**
446 * Set request class/object
447 *
448 * Set the request object. The request holds the request environment.
449 *
450 * If a class name is provided, it will instantiate it
451 *
452 * @param string|Zend_Controller_Request_Abstract $request
453 * @throws Zend_Controller_Exception if invalid request class
454 * @return Zend_Controller_Front
455 */
456 public function setRequest($request)
457 {
458 if (is_string($request)) {
459 Zend_Loader::loadClass($request);
460 $request = new $request();
461 }
462 if (!$request instanceof Zend_Controller_Request_Abstract) {
463 throw new Zend_Controller_Exception('Invalid request class');
464 }
465
466 $this->_request = $request;
467
468 return $this;
469 }
470
471 /**
472 * Return the request object.
473 *
474 * @return null|Zend_Controller_Request_Abstract
475 */
476 public function getRequest()
477 {
478 return $this->_request;
479 }
480
481 /**
482 * Set router class/object
483 *
484 * Set the router object. The router is responsible for mapping
485 * the request to a controller and action.
486 *
487 * If a class name is provided, instantiates router with any parameters
488 * registered via {@link setParam()} or {@link setParams()}.
489 *
490 * @param string|Zend_Controller_Router_Interface $router
491 * @throws Zend_Controller_Exception if invalid router class
492 * @return Zend_Controller_Front
493 */
494 public function setRouter($router)
495 {
496 if (is_string($router)) {
497 Zend_Loader::loadClass($router);
498 $router = new $router();
499 }
500
501 if (!$router instanceof Zend_Controller_Router_Interface) {
502 throw new Zend_Controller_Exception('Invalid router class');
503 }
504
505 $router->setFrontController($this);
506 $this->_router = $router;
507
508 return $this;
509 }
510
511 /**
512 * Return the router object.
513 *
514 * Instantiates a Zend_Controller_Router_Rewrite object if no router currently set.
515 *
516 * @return Zend_Controller_Router_Interface
517 */
518 public function getRouter()
519 {
520 if (null == $this->_router) {
521 require_once 'Zend/Controller/Router/Rewrite.php';
522 $this->setRouter(new Zend_Controller_Router_Rewrite());
523 }
524
525 return $this->_router;
526 }
527
528 /**
529 * Set the base URL used for requests
530 *
531 * Use to set the base URL segment of the REQUEST_URI to use when
532 * determining PATH_INFO, etc. Examples:
533 * - /admin
534 * - /myapp
535 * - /subdir/index.php
536 *
537 * Note that the URL should not include the full URI. Do not use:
538 * - http://example.com/admin
539 * - http://example.com/myapp
540 * - http://example.com/subdir/index.php
541 *
542 * If a null value is passed, this can be used as well for autodiscovery (default).
543 *
544 * @param string $base
545 * @return Zend_Controller_Front
546 * @throws Zend_Controller_Exception for non-string $base
547 */
548 public function setBaseUrl($base = null)
549 {
550 if (!is_string($base) && (null !== $base)) {
551 throw new Zend_Controller_Exception('Rewrite base must be a string');
552 }
553
554 $this->_baseUrl = $base;
555
556 if ((null !== ($request = $this->getRequest())) && (method_exists($request, 'setBaseUrl'))) {
557 $request->setBaseUrl($base);
558 }
559
560 return $this;
561 }
562
563 /**
564 * Retrieve the currently set base URL
565 *
566 * @return string
567 */
568 public function getBaseUrl()
569 {
570 $request = $this->getRequest();
571 if ((null !== $request) && method_exists($request, 'getBaseUrl')) {
572 return $request->getBaseUrl();
573 }
574
575 return $this->_baseUrl;
576 }
577
578 /**
579 * Set the dispatcher object. The dispatcher is responsible for
580 * taking a Zend_Controller_Dispatcher_Token object, instantiating the controller, and
581 * call the action method of the controller.
582 *
583 * @param Zend_Controller_Dispatcher_Interface $dispatcher
584 * @return Zend_Controller_Front
585 */
586 public function setDispatcher(Zend_Controller_Dispatcher_Interface $dispatcher)
587 {
588 $this->_dispatcher = $dispatcher;
589 return $this;
590 }
591
592 /**
593 * Return the dispatcher object.
594 *
595 * @return Zend_Controller_Dispatcher_Interface
596 */
597 public function getDispatcher()
598 {
599 /**
600 * Instantiate the default dispatcher if one was not set.
601 */
602 if (!$this->_dispatcher instanceof Zend_Controller_Dispatcher_Interface) {
603 require_once 'Zend/Controller/Dispatcher/Standard.php';
604 $this->_dispatcher = new Zend_Controller_Dispatcher_Standard();
605 }
606 return $this->_dispatcher;
607 }
608
609 /**
610 * Set response class/object
611 *
612 * Set the response object. The response is a container for action
613 * responses and headers. Usage is optional.
614 *
615 * If a class name is provided, instantiates a response object.
616 *
617 * @param string|Zend_Controller_Response_Abstract $response
618 * @throws Zend_Controller_Exception if invalid response class
619 * @return Zend_Controller_Front
620 */
621 public function setResponse($response)
622 {
623 if (is_string($response)) {
624 Zend_Loader::loadClass($response);
625 $response = new $response();
626 }
627 if (!$response instanceof Zend_Controller_Response_Abstract) {
628 throw new Zend_Controller_Exception('Invalid response class');
629 }
630
631 $this->_response = $response;
632
633 return $this;
634 }
635
636 /**
637 * Return the response object.
638 *
639 * @return null|Zend_Controller_Response_Abstract
640 */
641 public function getResponse()
642 {
643 return $this->_response;
644 }
645
646 /**
647 * Add or modify a parameter to use when instantiating an action controller
648 *
649 * @param string $name
650 * @param mixed $value
651 * @return Zend_Controller_Front
652 */
653 public function setParam($name, $value)
654 {
655 $name = (string) $name;
656 $this->_invokeParams[$name] = $value;
657 return $this;
658 }
659
660 /**
661 * Set parameters to pass to action controller constructors
662 *
663 * @param array $params
664 * @return Zend_Controller_Front
665 */
666 public function setParams(array $params)
667 {
668 $this->_invokeParams = array_merge($this->_invokeParams, $params);
669 return $this;
670 }
671
672 /**
673 * Retrieve a single parameter from the controller parameter stack
674 *
675 * @param string $name
676 * @return mixed
677 */
678 public function getParam($name)
679 {
680 if(isset($this->_invokeParams[$name])) {
681 return $this->_invokeParams[$name];
682 }
683
684 return null;
685 }
686
687 /**
688 * Retrieve action controller instantiation parameters
689 *
690 * @return array
691 */
692 public function getParams()
693 {
694 return $this->_invokeParams;
695 }
696
697 /**
698 * Clear the controller parameter stack
699 *
700 * By default, clears all parameters. If a parameter name is given, clears
701 * only that parameter; if an array of parameter names is provided, clears
702 * each.
703 *
704 * @param null|string|array single key or array of keys for params to clear
705 * @return Zend_Controller_Front
706 */
707 public function clearParams($name = null)
708 {
709 if (null === $name) {
710 $this->_invokeParams = array();
711 } elseif (is_string($name) && isset($this->_invokeParams[$name])) {
712 unset($this->_invokeParams[$name]);
713 } elseif (is_array($name)) {
714 foreach ($name as $key) {
715 if (is_string($key) && isset($this->_invokeParams[$key])) {
716 unset($this->_invokeParams[$key]);
717 }
718 }
719 }
720
721 return $this;
722 }
723
724 /**
725 * Register a plugin.
726 *
727 * @param Zend_Controller_Plugin_Abstract $plugin
728 * @param int $stackIndex Optional; stack index for plugin
729 * @return Zend_Controller_Front
730 */
731 public function registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)
732 {
733 $this->_plugins->registerPlugin($plugin, $stackIndex);
734 return $this;
735 }
736
737 /**
738 * Unregister a plugin.
739 *
740 * @param string|Zend_Controller_Plugin_Abstract $plugin Plugin class or object to unregister
741 * @return Zend_Controller_Front
742 */
743 public function unregisterPlugin($plugin)
744 {
745 $this->_plugins->unregisterPlugin($plugin);
746 return $this;
747 }
748
749 /**
750 * Is a particular plugin registered?
751 *
752 * @param string $class
753 * @return bool
754 */
755 public function hasPlugin($class)
756 {
757 return $this->_plugins->hasPlugin($class);
758 }
759
760 /**
761 * Retrieve a plugin or plugins by class
762 *
763 * @param string $class
764 * @return false|Zend_Controller_Plugin_Abstract|array
765 */
766 public function getPlugin($class)
767 {
768 return $this->_plugins->getPlugin($class);
769 }
770
771 /**
772 * Retrieve all plugins
773 *
774 * @return array
775 */
776 public function getPlugins()
777 {
778 return $this->_plugins->getPlugins();
779 }
780
781 /**
782 * Set the throwExceptions flag and retrieve current status
783 *
784 * Set whether exceptions encounted in the dispatch loop should be thrown
785 * or caught and trapped in the response object.
786 *
787 * Default behaviour is to trap them in the response object; call this
788 * method to have them thrown.
789 *
790 * Passing no value will return the current value of the flag; passing a
791 * boolean true or false value will set the flag and return the current
792 * object instance.
793 *
794 * @param boolean $flag Defaults to null (return flag state)
795 * @return boolean|Zend_Controller_Front Used as a setter, returns object; as a getter, returns boolean
796 */
797 public function throwExceptions($flag = null)
798 {
799 if ($flag !== null) {
800 $this->_throwExceptions = (bool) $flag;
801 return $this;
802 }
803
804 return $this->_throwExceptions;
805 }
806
807 /**
808 * Set whether {@link dispatch()} should return the response without first
809 * rendering output. By default, output is rendered and dispatch() returns
810 * nothing.
811 *
812 * @param boolean $flag
813 * @return boolean|Zend_Controller_Front Used as a setter, returns object; as a getter, returns boolean
814 */
815 public function returnResponse($flag = null)
816 {
817 if (true === $flag) {
818 $this->_returnResponse = true;
819 return $this;
820 } elseif (false === $flag) {
821 $this->_returnResponse = false;
822 return $this;
823 }
824
825 return $this->_returnResponse;
826 }
827
828 /**
829 * Dispatch an HTTP request to a controller/action.
830 *
831 * @param Zend_Controller_Request_Abstract|null $request
832 * @param Zend_Controller_Response_Abstract|null $response
833 * @return void|Zend_Controller_Response_Abstract Returns response object if returnResponse() is true
834 */
835 public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
836 {
837 if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('Zend_Controller_Plugin_ErrorHandler')) {
838 // Register with stack index of 100
839 require_once 'Zend/Controller/Plugin/ErrorHandler.php';
840 $this->_plugins->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(), 100);
841 }
842
843 if (!$this->getParam('noViewRenderer') && !Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
844 require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
845 Zend_Controller_Action_HelperBroker::getStack()->offsetSet(-80, new Zend_Controller_Action_Helper_ViewRenderer());
846 }
847
848 /**
849 * Instantiate default request object (HTTP version) if none provided
850 */
851 if (null !== $request) {
852 $this->setRequest($request);
853 } elseif ((null === $request) && (null === ($request = $this->getRequest()))) {
854 require_once 'Zend/Controller/Request/Http.php';
855 $request = new Zend_Controller_Request_Http();
856 $this->setRequest($request);
857 }
858
859 /**
860 * Set base URL of request object, if available
861 */
862 if (is_callable(array($this->_request, 'setBaseUrl'))) {
863 if (null !== $this->_baseUrl) {
864 $this->_request->setBaseUrl($this->_baseUrl);
865 }
866 }
867
868 /**
869 * Instantiate default response object (HTTP version) if none provided
870 */
871 if (null !== $response) {
872 $this->setResponse($response);
873 } elseif ((null === $this->_response) && (null === ($this->_response = $this->getResponse()))) {
874 require_once 'Zend/Controller/Response/Http.php';
875 $response = new Zend_Controller_Response_Http();
876 $this->setResponse($response);
877 }
878
879 /**
880 * Register request and response objects with plugin broker
881 */
882 $this->_plugins
883 ->setRequest($this->_request)
884 ->setResponse($this->_response);
885
886 /**
887 * Initialize router
888 */
889 $router = $this->getRouter();
890 $router->setParams($this->getParams());
891
892 /**
893 * Initialize dispatcher
894 */
895 $dispatcher = $this->getDispatcher();
896 $dispatcher->setParams($this->getParams())
897 ->setResponse($this->_response);
898
899 // Begin dispatch
900 try {
901 /**
902 * Route request to controller/action, if a router is provided
903 */
904
905 /**
906 * Notify plugins of router startup
907 */
908 $this->_plugins->routeStartup($this->_request);
909
910 $router->route($this->_request);
911
912 /**
913 * Notify plugins of router completion
914 */
915 $this->_plugins->routeShutdown($this->_request);
916
917 /**
918 * Notify plugins of dispatch loop startup
919 */
920 $this->_plugins->dispatchLoopStartup($this->_request);
921
922 /**
923 * Attempt to dispatch the controller/action. If the $this->_request
924 * indicates that it needs to be dispatched, move to the next
925 * action in the request.
926 */
927 do {
928 $this->_request->setDispatched(true);
929
930 /**
931 * Notify plugins of dispatch startup
932 */
933 $this->_plugins->preDispatch($this->_request);
934
935 /**
936 * Skip requested action if preDispatch() has reset it
937 */
938 if (!$this->_request->isDispatched()) {
939 continue;
940 }
941
942 /**
943 * Dispatch request
944 */
945 try {
946 $dispatcher->dispatch($this->_request, $this->_response);
947 } catch (Exception $e) {
948 if ($this->throwExceptions()) {
949 throw $e;
950 }
951 $this->_response->setException($e);
952 }
953
954 /**
955 * Notify plugins of dispatch completion
956 */
957 $this->_plugins->postDispatch($this->_request);
958 } while (!$this->_request->isDispatched());
959 } catch (Exception $e) {
960 if ($this->throwExceptions()) {
961 throw $e;
962 }
963
964 $this->_response->setException($e);
965 }
966
967 /**
968 * Notify plugins of dispatch loop completion
969 */
970 try {
971 $this->_plugins->dispatchLoopShutdown();
972 } catch (Exception $e) {
973 if ($this->throwExceptions()) {
974 throw $e;
975 }
976
977 $this->_response->setException($e);
978 }
979
980 if ($this->returnResponse()) {
981 return $this->_response;
982 }
983
984 $this->_response->sendResponse();
985 }
986 }
987
-
open/home/heiferftp/www/server/application/bootstrap.php
Zend_Controller_Front->dispatch()
1
<?php
2
3 /******************************************************************************************************************
4 * Copyright (c) 2008, Orsa Studio (www.orsa-studio.com). All Rights Reserved.
5 * Author Philip Andrew. This is commercial software, you may not use/distribute without permission of the author
6 ******************************************************************************************************************/
7
8 // @todo add error controller http://framework.zend.com/docs/quickstart/create-an-error-controller-and-view
9
10 include_once('bootstrapcommon.php');
11
12 // This is the for the login php script, see function _loginInclude
13 ini_set('allow_call_time_pass_reference',true);
14 define('ROOT_DIR', dirname(dirname(__FILE__)));
15
16 // @todo adopt the Zym App approach http://code.google.com/p/zym/source/browse/trunk/incubator/library/Zym/App/Resource/Doctrine.php?spec=svn719&r=719
17
18 class