Module.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. *
  60. * @var string
  61. */
  62. protected $_moduleKey = 'module';
  63. protected $_controllerKey = 'controller';
  64. protected $_actionKey = 'action';
  65. /**#@-*/
  66. /**
  67. * @var Zend_Controller_Dispatcher_Interface
  68. */
  69. protected $_dispatcher;
  70. /**
  71. * @var Zend_Controller_Request_Abstract
  72. */
  73. protected $_request;
  74. /**
  75. * Get the version of the route
  76. *
  77. * @return int
  78. */
  79. public function getVersion()
  80. {
  81. return 1;
  82. }
  83. /**
  84. * Instantiates route based on passed Zend_Config structure
  85. *
  86. * @param Zend_Config $config
  87. * @return Zend_Controller_Router_Route_Module
  88. */
  89. public static function getInstance(Zend_Config $config)
  90. {
  91. $frontController = Zend_Controller_Front::getInstance();
  92. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  93. $dispatcher = $frontController->getDispatcher();
  94. $request = $frontController->getRequest();
  95. return new self($defs, $dispatcher, $request);
  96. }
  97. /**
  98. * Constructor
  99. *
  100. * @param array $defaults Defaults for map variables with keys as variable names
  101. * @param Zend_Controller_Dispatcher_Interface $dispatcher Dispatcher object
  102. * @param Zend_Controller_Request_Abstract $request Request object
  103. */
  104. public function __construct(
  105. array $defaults = array(),
  106. Zend_Controller_Dispatcher_Interface $dispatcher = null,
  107. Zend_Controller_Request_Abstract $request = null
  108. )
  109. {
  110. $this->_defaults = $defaults;
  111. if (isset($request)) {
  112. $this->_request = $request;
  113. }
  114. if (isset($dispatcher)) {
  115. $this->_dispatcher = $dispatcher;
  116. }
  117. }
  118. /**
  119. * Set request keys based on values in request object
  120. *
  121. * @return void
  122. */
  123. protected function _setRequestKeys()
  124. {
  125. if (null !== $this->_request) {
  126. $this->_moduleKey = $this->_request->getModuleKey();
  127. $this->_controllerKey = $this->_request->getControllerKey();
  128. $this->_actionKey = $this->_request->getActionKey();
  129. }
  130. if (null !== $this->_dispatcher) {
  131. $this->_defaults += array(
  132. $this->_controllerKey => $this->_dispatcher->getDefaultControllerName(),
  133. $this->_actionKey => $this->_dispatcher->getDefaultAction(),
  134. $this->_moduleKey => $this->_dispatcher->getDefaultModule()
  135. );
  136. }
  137. $this->_keysSet = true;
  138. }
  139. /**
  140. * Matches a user submitted path. Assigns and returns an array of variables
  141. * on a successful match.
  142. *
  143. * If a request object is registered, it uses its setModuleName(),
  144. * setControllerName(), and setActionName() accessors to set those values.
  145. * Always returns the values as an array.
  146. *
  147. * @param string $path Path used to match against this routing map
  148. * @param boolean $partial
  149. * @return array An array of assigned values or a false on a mismatch
  150. */
  151. public function match($path, $partial = false)
  152. {
  153. $this->_setRequestKeys();
  154. $values = array();
  155. $params = array();
  156. if (!$partial) {
  157. $path = trim($path, self::URI_DELIMITER);
  158. } else {
  159. $matchedPath = $path;
  160. }
  161. if ($path != '') {
  162. $path = explode(self::URI_DELIMITER, $path);
  163. if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
  164. $values[$this->_moduleKey] = array_shift($path);
  165. $this->_moduleValid = true;
  166. }
  167. if (count($path) && !empty($path[0])) {
  168. $values[$this->_controllerKey] = array_shift($path);
  169. }
  170. if (count($path) && !empty($path[0])) {
  171. $values[$this->_actionKey] = array_shift($path);
  172. }
  173. if ($numSegs = count($path)) {
  174. for ($i = 0; $i < $numSegs; $i = $i + 2) {
  175. $key = urldecode($path[$i]);
  176. $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
  177. $params[$key] = (isset($params[$key]) ? (array_merge((array)$params[$key], array($val))) : $val);
  178. }
  179. }
  180. }
  181. if ($partial) {
  182. $this->setMatchedPath($matchedPath);
  183. }
  184. $this->_values = $values + $params;
  185. return $this->_values + $this->_defaults;
  186. }
  187. /**
  188. * Assembles user submitted parameters forming a URL path defined by this route
  189. *
  190. * @param array $data An array of variable and value pairs used as parameters
  191. * @param boolean $reset Weither to reset the current params
  192. * @param boolean $encode
  193. * @param boolean $partial
  194. * @return string Route path with user submitted parameters
  195. */
  196. public function assemble($data = array(), $reset = false, $encode = true, $partial = false)
  197. {
  198. if (!$this->_keysSet) {
  199. $this->_setRequestKeys();
  200. }
  201. $params = (!$reset) ? $this->_values : array();
  202. foreach ($data as $key => $value) {
  203. if ($value !== null) {
  204. $params[$key] = $value;
  205. } elseif (isset($params[$key])) {
  206. unset($params[$key]);
  207. }
  208. }
  209. $params += $this->_defaults;
  210. $url = '';
  211. if ($this->_moduleValid || array_key_exists($this->_moduleKey, $data)) {
  212. if ($params[$this->_moduleKey] != $this->_defaults[$this->_moduleKey]) {
  213. $module = $params[$this->_moduleKey];
  214. }
  215. }
  216. unset($params[$this->_moduleKey]);
  217. $controller = $params[$this->_controllerKey];
  218. unset($params[$this->_controllerKey]);
  219. $action = $params[$this->_actionKey];
  220. unset($params[$this->_actionKey]);
  221. foreach ($params as $key => $value) {
  222. $key = ($encode) ? urlencode($key) : $key;
  223. if (is_array($value)) {
  224. foreach ($value as $arrayValue) {
  225. $arrayValue = ($encode) ? urlencode($arrayValue) : $arrayValue;
  226. $url .= self::URI_DELIMITER . $key;
  227. $url .= self::URI_DELIMITER . $arrayValue;
  228. }
  229. } else {
  230. if ($encode) {
  231. $value = urlencode($value);
  232. }
  233. $url .= self::URI_DELIMITER . $key;
  234. $url .= self::URI_DELIMITER . $value;
  235. }
  236. }
  237. if (!empty($url) || $action !== $this->_defaults[$this->_actionKey]) {
  238. if ($encode) {
  239. $action = urlencode($action);
  240. }
  241. $url = self::URI_DELIMITER . $action . $url;
  242. }
  243. if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
  244. if ($encode) {
  245. $controller = urlencode($controller);
  246. }
  247. $url = self::URI_DELIMITER . $controller . $url;
  248. }
  249. if (isset($module)) {
  250. if ($encode) {
  251. $module = urlencode($module);
  252. }
  253. $url = self::URI_DELIMITER . $module . $url;
  254. }
  255. return ltrim($url, self::URI_DELIMITER);
  256. }
  257. /**
  258. * Return a single parameter of route's defaults
  259. *
  260. * @param string $name Array key of the parameter
  261. * @return string Previously set default
  262. */
  263. public function getDefault($name)
  264. {
  265. if (isset($this->_defaults[$name])) {
  266. return $this->_defaults[$name];
  267. }
  268. }
  269. /**
  270. * Return an array of defaults
  271. *
  272. * @return array Route defaults
  273. */
  274. public function getDefaults()
  275. {
  276. return $this->_defaults;
  277. }
  278. }