/library/Zend/Controller/Router/Route/Module.php

https://github.com/adaykin/zf2 · PHP · 297 lines · 154 code · 46 blank · 97 comment · 35 complexity · a01472afa759ee317542b235cded0566 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 Router
  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\Router\Route;
  25. use Zend\Controller\Request;
  26. use Zend\Controller\Dispatcher;
  27. use Zend\Config;
  28. /**
  29. * Module Route
  30. *
  31. * Default route for module functionality
  32. *
  33. * @uses \Zend\Controller\Front
  34. * @uses \Zend\Controller\Router\Route\AbstractRoute
  35. * @package Zend_Controller
  36. * @subpackage Router
  37. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @see http://manuals.rubyonrails.com/read/chapter/65
  40. */
  41. class Module extends AbstractRoute
  42. {
  43. /**
  44. * URI delimiter
  45. */
  46. const URI_DELIMITER = '/';
  47. /**
  48. * Default values for the route (ie. module, controller, action, params)
  49. * @var array
  50. */
  51. protected $_defaults;
  52. protected $_values = array();
  53. protected $_moduleValid = false;
  54. protected $_keysSet = false;
  55. /**#@+
  56. * Array keys to use for module, controller, and action. Should be taken out of request.
  57. * @var string
  58. */
  59. protected $_moduleKey = 'module';
  60. protected $_controllerKey = 'controller';
  61. protected $_actionKey = 'action';
  62. /**#@-*/
  63. /**
  64. * @var \Zend\Controller\Dispatcher
  65. */
  66. protected $_dispatcher;
  67. /**
  68. * @var \Zend\Controller\Request\AbstractRequest
  69. */
  70. protected $_request;
  71. public function getVersion() {
  72. return 1;
  73. }
  74. /**
  75. * Instantiates route based on passed Zend_Config structure
  76. */
  77. public static function getInstance(Config\Config $config)
  78. {
  79. $frontController = \Zend\Controller\Front::getInstance();
  80. $defs = ($config->defaults instanceof Config\Config) ? $config->defaults->toArray() : array();
  81. $dispatcher = $frontController->getDispatcher();
  82. $request = $frontController->getRequest();
  83. return new self($defs, $dispatcher, $request);
  84. }
  85. /**
  86. * Constructor
  87. *
  88. * @param array $defaults Defaults for map variables with keys as variable names
  89. * @param \Zend\Controller\Dispatcher $dispatcher Dispatcher object
  90. * @param \Zend\Controller\Request\AbstractRequest $request Request object
  91. */
  92. public function __construct(array $defaults = array(),
  93. \Zend\Controller\Dispatcher $dispatcher = null,
  94. \Zend\Controller\Request\AbstractRequest $request = null)
  95. {
  96. $this->_defaults = $defaults;
  97. if (isset($request)) {
  98. $this->_request = $request;
  99. }
  100. if (isset($dispatcher)) {
  101. $this->_dispatcher = $dispatcher;
  102. }
  103. }
  104. /**
  105. * Set request keys based on values in request object
  106. *
  107. * @return void
  108. */
  109. protected function _setRequestKeys()
  110. {
  111. if (null !== $this->_request) {
  112. $this->_moduleKey = $this->_request->getModuleKey();
  113. $this->_controllerKey = $this->_request->getControllerKey();
  114. $this->_actionKey = $this->_request->getActionKey();
  115. }
  116. if (null !== $this->_dispatcher) {
  117. $this->_defaults += array(
  118. $this->_controllerKey => $this->_dispatcher->getDefaultControllerName(),
  119. $this->_actionKey => $this->_dispatcher->getDefaultAction(),
  120. $this->_moduleKey => $this->_dispatcher->getDefaultModule()
  121. );
  122. }
  123. $this->_keysSet = true;
  124. }
  125. /**
  126. * Matches a user submitted path. Assigns and returns an array of variables
  127. * on a successful match.
  128. *
  129. * If a request object is registered, it uses its setModuleName(),
  130. * setControllerName(), and setActionName() accessors to set those values.
  131. * Always returns the values as an array.
  132. *
  133. * @param string $path Path used to match against this routing map
  134. * @return array An array of assigned values or a false on a mismatch
  135. */
  136. public function match($path, $partial = false)
  137. {
  138. $this->_setRequestKeys();
  139. $values = array();
  140. $params = array();
  141. if (!$partial) {
  142. $path = trim($path, self::URI_DELIMITER);
  143. } else {
  144. $matchedPath = $path;
  145. }
  146. if ($path != '') {
  147. $path = explode(self::URI_DELIMITER, $path);
  148. if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
  149. $values[$this->_moduleKey] = array_shift($path);
  150. $this->_moduleValid = true;
  151. }
  152. if (count($path) && !empty($path[0])) {
  153. $values[$this->_controllerKey] = array_shift($path);
  154. }
  155. if (count($path) && !empty($path[0])) {
  156. $values[$this->_actionKey] = array_shift($path);
  157. }
  158. if ($numSegs = count($path)) {
  159. for ($i = 0; $i < $numSegs; $i = $i + 2) {
  160. $key = urldecode($path[$i]);
  161. $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
  162. $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val);
  163. }
  164. }
  165. }
  166. if ($partial) {
  167. $this->setMatchedPath($matchedPath);
  168. }
  169. $this->_values = $values + $params;
  170. return $this->_values + $this->_defaults;
  171. }
  172. /**
  173. * Assembles user submitted parameters forming a URL path defined by this route
  174. *
  175. * @param array $data An array of variable and value pairs used as parameters
  176. * @param bool $reset Weither to reset the current params
  177. * @return string Route path with user submitted parameters
  178. */
  179. public function assemble($data = array(), $reset = false, $encode = true, $partial = false)
  180. {
  181. if (!$this->_keysSet) {
  182. $this->_setRequestKeys();
  183. }
  184. $params = (!$reset) ? $this->_values : array();
  185. foreach ($data as $key => $value) {
  186. if ($value !== null) {
  187. $params[$key] = $value;
  188. } elseif (isset($params[$key])) {
  189. unset($params[$key]);
  190. }
  191. }
  192. $params += $this->_defaults;
  193. $url = '';
  194. if ($this->_moduleValid || array_key_exists($this->_moduleKey, $data)) {
  195. if ($params[$this->_moduleKey] != $this->_defaults[$this->_moduleKey]) {
  196. $module = $params[$this->_moduleKey];
  197. }
  198. }
  199. unset($params[$this->_moduleKey]);
  200. $controller = $params[$this->_controllerKey];
  201. unset($params[$this->_controllerKey]);
  202. $action = $params[$this->_actionKey];
  203. unset($params[$this->_actionKey]);
  204. foreach ($params as $key => $value) {
  205. $key = ($encode) ? urlencode($key) : $key;
  206. if (is_array($value)) {
  207. foreach ($value as $arrayValue) {
  208. $arrayValue = ($encode) ? urlencode($arrayValue) : $arrayValue;
  209. $url .= '/' . $key;
  210. $url .= '/' . $arrayValue;
  211. }
  212. } else {
  213. if ($encode) $value = urlencode($value);
  214. $url .= '/' . $key;
  215. $url .= '/' . $value;
  216. }
  217. }
  218. if (!empty($url) || $action !== $this->_defaults[$this->_actionKey]) {
  219. if ($encode) $action = urlencode($action);
  220. $url = '/' . $action . $url;
  221. }
  222. if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
  223. if ($encode) $controller = urlencode($controller);
  224. $url = '/' . $controller . $url;
  225. }
  226. if (isset($module)) {
  227. if ($encode) $module = urlencode($module);
  228. $url = '/' . $module . $url;
  229. }
  230. return ltrim($url, self::URI_DELIMITER);
  231. }
  232. /**
  233. * Return a single parameter of route's defaults
  234. *
  235. * @param string $name Array key of the parameter
  236. * @return string Previously set default
  237. */
  238. public function getDefault($name) {
  239. if (isset($this->_defaults[$name])) {
  240. return $this->_defaults[$name];
  241. }
  242. }
  243. /**
  244. * Return an array of defaults
  245. *
  246. * @return array Route defaults
  247. */
  248. public function getDefaults() {
  249. return $this->_defaults;
  250. }
  251. }