Module.php 8.5 KB

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