Abstract.php 12 KB

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