File indexing completed on 2024-12-22 05:36:33
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Controller 0017 * @subpackage Dispatcher 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** Zend_Controller_Dispatcher_Interface */ 0024 // require_once 'Zend/Controller/Dispatcher/Interface.php'; 0025 0026 /** 0027 * @category Zend 0028 * @package Zend_Controller 0029 * @subpackage Dispatcher 0030 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0031 * @license http://framework.zend.com/license/new-bsd New BSD License 0032 */ 0033 abstract class Zend_Controller_Dispatcher_Abstract implements Zend_Controller_Dispatcher_Interface 0034 { 0035 /** 0036 * Default action 0037 * @var string 0038 */ 0039 protected $_defaultAction = 'index'; 0040 0041 /** 0042 * Default controller 0043 * @var string 0044 */ 0045 protected $_defaultController = 'index'; 0046 0047 /** 0048 * Default module 0049 * @var string 0050 */ 0051 protected $_defaultModule = 'default'; 0052 0053 /** 0054 * Front Controller instance 0055 * @var Zend_Controller_Front 0056 */ 0057 protected $_frontController; 0058 0059 /** 0060 * Array of invocation parameters to use when instantiating action 0061 * controllers 0062 * @var array 0063 */ 0064 protected $_invokeParams = array(); 0065 0066 /** 0067 * Path delimiter character 0068 * @var string 0069 */ 0070 protected $_pathDelimiter = '_'; 0071 0072 /** 0073 * Response object to pass to action controllers, if any 0074 * @var Zend_Controller_Response_Abstract|null 0075 */ 0076 protected $_response = null; 0077 0078 /** 0079 * Word delimiter characters 0080 * @var array 0081 */ 0082 protected $_wordDelimiter = array('-', '.'); 0083 0084 /** 0085 * Constructor 0086 * 0087 * @return void 0088 */ 0089 public function __construct(array $params = array()) 0090 { 0091 $this->setParams($params); 0092 } 0093 0094 /** 0095 * Formats a string into a controller name. This is used to take a raw 0096 * controller name, such as one stored inside a Zend_Controller_Request_Abstract 0097 * object, and reformat it to a proper class name that a class extending 0098 * Zend_Controller_Action would use. 0099 * 0100 * @param string $unformatted 0101 * @return string 0102 */ 0103 public function formatControllerName($unformatted) 0104 { 0105 return ucfirst($this->_formatName($unformatted)) . 'Controller'; 0106 } 0107 0108 /** 0109 * Formats a string into an action name. This is used to take a raw 0110 * action name, such as one that would be stored inside a Zend_Controller_Request_Abstract 0111 * object, and reformat into a proper method name that would be found 0112 * inside a class extending Zend_Controller_Action. 0113 * 0114 * @param string $unformatted 0115 * @return string 0116 */ 0117 public function formatActionName($unformatted) 0118 { 0119 $formatted = $this->_formatName($unformatted, true); 0120 return strtolower(substr($formatted, 0, 1)) . substr($formatted, 1) . 'Action'; 0121 } 0122 0123 /** 0124 * Verify delimiter 0125 * 0126 * Verify a delimiter to use in controllers or actions. May be a single 0127 * string or an array of strings. 0128 * 0129 * @param string|array $spec 0130 * @return array 0131 * @throws Zend_Controller_Dispatcher_Exception with invalid delimiters 0132 */ 0133 public function _verifyDelimiter($spec) 0134 { 0135 if (is_string($spec)) { 0136 return (array) $spec; 0137 } elseif (is_array($spec)) { 0138 $allStrings = true; 0139 foreach ($spec as $delim) { 0140 if (!is_string($delim)) { 0141 $allStrings = false; 0142 break; 0143 } 0144 } 0145 0146 if (!$allStrings) { 0147 // require_once 'Zend/Controller/Dispatcher/Exception.php'; 0148 throw new Zend_Controller_Dispatcher_Exception('Word delimiter array must contain only strings'); 0149 } 0150 0151 return $spec; 0152 } 0153 0154 // require_once 'Zend/Controller/Dispatcher/Exception.php'; 0155 throw new Zend_Controller_Dispatcher_Exception('Invalid word delimiter'); 0156 } 0157 0158 /** 0159 * Retrieve the word delimiter character(s) used in 0160 * controller or action names 0161 * 0162 * @return array 0163 */ 0164 public function getWordDelimiter() 0165 { 0166 return $this->_wordDelimiter; 0167 } 0168 0169 /** 0170 * Set word delimiter 0171 * 0172 * Set the word delimiter to use in controllers and actions. May be a 0173 * single string or an array of strings. 0174 * 0175 * @param string|array $spec 0176 * @return Zend_Controller_Dispatcher_Abstract 0177 */ 0178 public function setWordDelimiter($spec) 0179 { 0180 $spec = $this->_verifyDelimiter($spec); 0181 $this->_wordDelimiter = $spec; 0182 0183 return $this; 0184 } 0185 0186 /** 0187 * Retrieve the path delimiter character(s) used in 0188 * controller names 0189 * 0190 * @return array 0191 */ 0192 public function getPathDelimiter() 0193 { 0194 return $this->_pathDelimiter; 0195 } 0196 0197 /** 0198 * Set path delimiter 0199 * 0200 * Set the path delimiter to use in controllers. May be a single string or 0201 * an array of strings. 0202 * 0203 * @param string $spec 0204 * @return Zend_Controller_Dispatcher_Abstract 0205 */ 0206 public function setPathDelimiter($spec) 0207 { 0208 if (!is_string($spec)) { 0209 // require_once 'Zend/Controller/Dispatcher/Exception.php'; 0210 throw new Zend_Controller_Dispatcher_Exception('Invalid path delimiter'); 0211 } 0212 $this->_pathDelimiter = $spec; 0213 0214 return $this; 0215 } 0216 0217 /** 0218 * Formats a string from a URI into a PHP-friendly name. 0219 * 0220 * By default, replaces words separated by the word separator character(s) 0221 * with camelCaps. If $isAction is false, it also preserves replaces words 0222 * separated by the path separation character with an underscore, making 0223 * the following word Title cased. All non-alphanumeric characters are 0224 * removed. 0225 * 0226 * @param string $unformatted 0227 * @param boolean $isAction Defaults to false 0228 * @return string 0229 */ 0230 protected function _formatName($unformatted, $isAction = false) 0231 { 0232 // preserve directories 0233 if (!$isAction) { 0234 $segments = explode($this->getPathDelimiter(), $unformatted); 0235 } else { 0236 $segments = (array) $unformatted; 0237 } 0238 0239 foreach ($segments as $key => $segment) { 0240 $segment = str_replace($this->getWordDelimiter(), ' ', strtolower($segment)); 0241 $segment = preg_replace('/[^a-z0-9 ]/', '', $segment); 0242 $segments[$key] = str_replace(' ', '', ucwords($segment)); 0243 } 0244 0245 return implode('_', $segments); 0246 } 0247 0248 /** 0249 * Retrieve front controller instance 0250 * 0251 * @return Zend_Controller_Front 0252 */ 0253 public function getFrontController() 0254 { 0255 if (null === $this->_frontController) { 0256 // require_once 'Zend/Controller/Front.php'; 0257 $this->_frontController = Zend_Controller_Front::getInstance(); 0258 } 0259 0260 return $this->_frontController; 0261 } 0262 0263 /** 0264 * Set front controller instance 0265 * 0266 * @param Zend_Controller_Front $controller 0267 * @return Zend_Controller_Dispatcher_Abstract 0268 */ 0269 public function setFrontController(Zend_Controller_Front $controller) 0270 { 0271 $this->_frontController = $controller; 0272 return $this; 0273 } 0274 0275 /** 0276 * Add or modify a parameter to use when instantiating an action controller 0277 * 0278 * @param string $name 0279 * @param mixed $value 0280 * @return Zend_Controller_Dispatcher_Abstract 0281 */ 0282 public function setParam($name, $value) 0283 { 0284 $name = (string) $name; 0285 $this->_invokeParams[$name] = $value; 0286 return $this; 0287 } 0288 0289 /** 0290 * Set parameters to pass to action controller constructors 0291 * 0292 * @param array $params 0293 * @return Zend_Controller_Dispatcher_Abstract 0294 */ 0295 public function setParams(array $params) 0296 { 0297 $this->_invokeParams = array_merge($this->_invokeParams, $params); 0298 return $this; 0299 } 0300 0301 /** 0302 * Retrieve a single parameter from the controller parameter stack 0303 * 0304 * @param string $name 0305 * @return mixed 0306 */ 0307 public function getParam($name) 0308 { 0309 if(isset($this->_invokeParams[$name])) { 0310 return $this->_invokeParams[$name]; 0311 } 0312 0313 return null; 0314 } 0315 0316 /** 0317 * Retrieve action controller instantiation parameters 0318 * 0319 * @return array 0320 */ 0321 public function getParams() 0322 { 0323 return $this->_invokeParams; 0324 } 0325 0326 /** 0327 * Clear the controller parameter stack 0328 * 0329 * By default, clears all parameters. If a parameter name is given, clears 0330 * only that parameter; if an array of parameter names is provided, clears 0331 * each. 0332 * 0333 * @param null|string|array single key or array of keys for params to clear 0334 * @return Zend_Controller_Dispatcher_Abstract 0335 */ 0336 public function clearParams($name = null) 0337 { 0338 if (null === $name) { 0339 $this->_invokeParams = array(); 0340 } elseif (is_string($name) && isset($this->_invokeParams[$name])) { 0341 unset($this->_invokeParams[$name]); 0342 } elseif (is_array($name)) { 0343 foreach ($name as $key) { 0344 if (is_string($key) && isset($this->_invokeParams[$key])) { 0345 unset($this->_invokeParams[$key]); 0346 } 0347 } 0348 } 0349 0350 return $this; 0351 } 0352 0353 /** 0354 * Set response object to pass to action controllers 0355 * 0356 * @param Zend_Controller_Response_Abstract|null $response 0357 * @return Zend_Controller_Dispatcher_Abstract 0358 */ 0359 public function setResponse(Zend_Controller_Response_Abstract $response = null) 0360 { 0361 $this->_response = $response; 0362 return $this; 0363 } 0364 0365 /** 0366 * Return the registered response object 0367 * 0368 * @return Zend_Controller_Response_Abstract|null 0369 */ 0370 public function getResponse() 0371 { 0372 return $this->_response; 0373 } 0374 0375 /** 0376 * Set the default controller (minus any formatting) 0377 * 0378 * @param string $controller 0379 * @return Zend_Controller_Dispatcher_Abstract 0380 */ 0381 public function setDefaultControllerName($controller) 0382 { 0383 $this->_defaultController = (string) $controller; 0384 return $this; 0385 } 0386 0387 /** 0388 * Retrieve the default controller name (minus formatting) 0389 * 0390 * @return string 0391 */ 0392 public function getDefaultControllerName() 0393 { 0394 return $this->_defaultController; 0395 } 0396 0397 /** 0398 * Set the default action (minus any formatting) 0399 * 0400 * @param string $action 0401 * @return Zend_Controller_Dispatcher_Abstract 0402 */ 0403 public function setDefaultAction($action) 0404 { 0405 $this->_defaultAction = (string) $action; 0406 return $this; 0407 } 0408 0409 /** 0410 * Retrieve the default action name (minus formatting) 0411 * 0412 * @return string 0413 */ 0414 public function getDefaultAction() 0415 { 0416 return $this->_defaultAction; 0417 } 0418 0419 /** 0420 * Set the default module 0421 * 0422 * @param string $module 0423 * @return Zend_Controller_Dispatcher_Abstract 0424 */ 0425 public function setDefaultModule($module) 0426 { 0427 $this->_defaultModule = (string) $module; 0428 return $this; 0429 } 0430 0431 /** 0432 * Retrieve the default module 0433 * 0434 * @return string 0435 */ 0436 public function getDefaultModule() 0437 { 0438 return $this->_defaultModule; 0439 } 0440 }