2
0

Abstract.php 12 KB

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