Module.php 8.7 KB

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