Module.php 9.3 KB

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