/modules/ZendFramework1Mvc/library/Zend/Controller/Dispatcher/AbstractDispatcher.php

https://github.com/leerbag/zf2 · PHP · 463 lines · 176 code · 46 blank · 241 comment · 12 complexity · 9c0bd2db9ef4a8330bffddaf9f68f999 MD5 · raw file

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