Module.php 8.7 KB

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