Route.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_Rest
  16. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. * @version $Id$
  19. */
  20. /**
  21. * @see Zend_Controller_Router_Route_Interface
  22. */
  23. require_once 'Zend/Controller/Router/Route/Interface.php';
  24. /**
  25. * @see Zend_Controller_Router_Route_Module
  26. */
  27. require_once 'Zend/Controller/Router/Route/Module.php';
  28. /**
  29. * @see Zend_Controller_Dispatcher_Interface
  30. */
  31. require_once 'Zend/Controller/Dispatcher/Interface.php';
  32. /**
  33. * @see Zend_Controller_Request_Abstract
  34. */
  35. require_once 'Zend/Controller/Request/Abstract.php';
  36. /**
  37. * Rest Route
  38. *
  39. * Request-aware route for RESTful modular routing
  40. *
  41. * @package Zend_Rest
  42. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. */
  45. class Zend_Rest_Route extends Zend_Controller_Router_Route_Module
  46. {
  47. /**
  48. * Specific Modules to receive RESTful routes
  49. * @var array
  50. */
  51. protected $_restfulModules = null;
  52. /**
  53. * Specific Modules=>Controllers to receive RESTful routes
  54. * @var array
  55. */
  56. protected $_restfulControllers = null;
  57. /**
  58. * Constructor
  59. *
  60. * @param array $defaults Defaults for map variables with keys as variable names
  61. * @param Zend_Controller_Front $front Front Controller object
  62. */
  63. public function __construct(Zend_Controller_Front $front,
  64. array $defaults = array(),
  65. array $responders = array())
  66. {
  67. $this->_defaults = $defaults;
  68. if($responders)
  69. $this->_parseResponders($responders);
  70. if (isset($front)) {
  71. $this->_request = $front->getRequest();
  72. $this->_dispatcher = $front->getDispatcher();
  73. }
  74. }
  75. /**
  76. * Matches a user submitted request. Assigns and returns an array of variables
  77. * on a successful match.
  78. *
  79. * If a request object is registered, it uses its setModuleName(),
  80. * setControllerName(), and setActionName() accessors to set those values.
  81. * Always returns the values as an array.
  82. *
  83. * @param Zend_Controller_Request_Http $request Request used to match against this routing ruleset
  84. * @return array An array of assigned values or a false on a mismatch
  85. */
  86. public function match($request)
  87. {
  88. $this->_setRequestKeys();
  89. $path = $request->getPathInfo();
  90. $values = array();
  91. $params = array();
  92. $path = trim($path, self::URI_DELIMITER);
  93. if ($path != '') {
  94. $path = explode(self::URI_DELIMITER, $path);
  95. // Determine Module
  96. $moduleName = $this->_defaults[$this->_moduleKey];
  97. if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
  98. $moduleName = $path[0];
  99. if ($this->_checkRestfulModule($moduleName)) {
  100. $values[$this->_moduleKey] = array_shift($path);
  101. $this->_moduleValid = true;
  102. }
  103. }
  104. // Determine Controller
  105. $controllerName = $this->_defaults[$this->_controllerKey];
  106. if (count($path) && !empty($path[0])) {
  107. if ($this->_checkRestfulController($moduleName, $path[0])) {
  108. $controllerName = $path[0];
  109. $values[$this->_controllerKey] = array_shift($path);
  110. $values[$this->_actionKey] = 'get';
  111. } else {
  112. // If Controller in URI is not found to be a RESTful
  113. // Controller, return false to fall back to other routes
  114. return false;
  115. }
  116. }
  117. //Store path count for method mapping
  118. $pathElementCount = count($path);
  119. // Check for leading "special get" URI's
  120. $specialGetTarget = false;
  121. if ($pathElementCount && array_search($path[0], array('index', 'new')) > -1) {
  122. $specialGetTarget = array_shift($path);
  123. } elseif ($pathElementCount == 1) {
  124. $params['id'] = array_shift($path);
  125. } elseif ($pathElementCount == 0 || $pathElementCount > 1) {
  126. $specialGetTarget = 'list';
  127. }
  128. // Digest URI params
  129. if ($numSegs = count($path)) {
  130. for ($i = 0; $i < $numSegs; $i = $i + 2) {
  131. $key = urldecode($path[$i]);
  132. $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
  133. $params[$key] = $val;
  134. }
  135. }
  136. // Check for trailing "special get" URI
  137. if (array_key_exists('edit', $params))
  138. $specialGetTarget = 'edit';
  139. // Determine Action
  140. $requestMethod = strtolower($request->getMethod());
  141. if ($requestMethod != 'get') {
  142. if ($request->getParam('_method')) {
  143. $values[$this->_actionKey] = strtolower($request->getParam('_method'));
  144. } elseif ( $this->_request->getHeader('X-HTTP-Method-Override') ) {
  145. $values[$this->_actionKey] = strtolower($this->_request->getHeader('X-HTTP-Method-Override'));
  146. } else {
  147. $values[$this->_actionKey] = $requestMethod;
  148. }
  149. //Map PUT and POST to actual create/update actions
  150. //based on parameter count (posting to resource or collection)
  151. switch( $values[$this->_actionKey] ){
  152. case 'post':
  153. if ($pathElementCount > 0) {
  154. $values[$this->_actionKey] = 'put';
  155. } else {
  156. $values[$this->_actionKey] = 'post';
  157. }
  158. break;
  159. case 'put':
  160. $values[$this->_actionKey] = 'put';
  161. break;
  162. }
  163. } elseif ($specialGetTarget) {
  164. $values[$this->_actionKey] = $specialGetTarget;
  165. }
  166. }
  167. $this->_values = $values + $params;
  168. return $this->_values + $this->_defaults;
  169. }
  170. /**
  171. * Assembles user submitted parameters forming a URL path defined by this route
  172. *
  173. * @param array $data An array of variable and value pairs used as parameters
  174. * @param bool $reset Weither to reset the current params
  175. * @return string Route path with user submitted parameters
  176. */
  177. public function assemble($data = array(), $reset = false, $encode = true)
  178. {
  179. if (!$this->_keysSet) {
  180. $this->_setRequestKeys();
  181. }
  182. $params = (!$reset) ? $this->_values : array();
  183. foreach ($data as $key => $value) {
  184. if ($value !== null) {
  185. $params[$key] = $value;
  186. } elseif (isset($params[$key])) {
  187. unset($params[$key]);
  188. }
  189. }
  190. $params += $this->_defaults;
  191. $url = '';
  192. if ($this->_moduleValid || array_key_exists($this->_moduleKey, $data)) {
  193. if ($params[$this->_moduleKey] != $this->_defaults[$this->_moduleKey]) {
  194. $module = $params[$this->_moduleKey];
  195. }
  196. }
  197. unset($params[$this->_moduleKey]);
  198. $controller = $params[$this->_controllerKey];
  199. unset($params[$this->_controllerKey]);
  200. unset($params[$this->_actionKey]);
  201. if (isset($params['index']) && $params['index']) {
  202. unset($params['index']);
  203. $url .= '/index';
  204. foreach ($params as $key => $value) {
  205. $url .= '/' . $key;
  206. $url .= '/' . $value;
  207. }
  208. } else {
  209. if (isset($params['id']))
  210. $url .= '/' . $params['id'];
  211. }
  212. if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
  213. $url = '/' . $controller . $url;
  214. }
  215. if (isset($module)) {
  216. $url = '/' . $module . $url;
  217. }
  218. return ltrim($url, self::URI_DELIMITER);
  219. }
  220. /**
  221. * Tells Rewrite Router which version this Route is
  222. *
  223. * @return int Route "version"
  224. */
  225. public function getVersion()
  226. {
  227. return 2;
  228. }
  229. /**
  230. * Parses the responders array sent to constructor to know
  231. * which modules and/or controllers are RESTful
  232. *
  233. * @param array $responders
  234. */
  235. private function _parseResponders($responders)
  236. {
  237. $modulesOnly = true;
  238. foreach ($responders as $responder) {
  239. if(is_array($responder))
  240. $modulesOnly = false;
  241. }
  242. if ($modulesOnly) {
  243. $this->_restfulModules = $responders;
  244. } else {
  245. $this->_restfulControllers = $responders;
  246. }
  247. }
  248. /**
  249. * Determine if a specified module supports RESTful routing
  250. *
  251. * @param string $moduleName
  252. * @return bool
  253. */
  254. private function _checkRestfulModule($moduleName)
  255. {
  256. if ($this->_allRestful())
  257. return true;
  258. if ($this->_fullRestfulModule($moduleName))
  259. return true;
  260. if ($this->_restfulControllers && array_key_exists($moduleName, $this->_restfulControllers))
  261. return true;
  262. return false;
  263. }
  264. /**
  265. * Determine if a specified module + controller combination supports
  266. * RESTful routing
  267. *
  268. * @param string $moduleName
  269. * @param string $controllerName
  270. * @return bool
  271. */
  272. private function _checkRestfulController($moduleName, $controllerName)
  273. {
  274. if ($this->_allRestful())
  275. return true;
  276. if ($this->_fullRestfulModule($moduleName))
  277. return true;
  278. if ($this->_checkRestfulModule($moduleName)
  279. && $this->_restfulControllers
  280. && array_search($controllerName, $this->_restfulControllers[$moduleName]) !== false)
  281. return true;
  282. return false;
  283. }
  284. /**
  285. * Determines if RESTful routing applies to the entire app
  286. *
  287. * @return bool
  288. */
  289. private function _allRestful()
  290. {
  291. return (!$this->_restfulModules && !$this->_restfulControllers);
  292. }
  293. /**
  294. * Determines if RESTful routing applies to an entire module
  295. *
  296. * @param string $moduleName
  297. * @return bool
  298. */
  299. private function _fullRestfulModule($moduleName)
  300. {
  301. return ($this->_restfulModules && array_search($moduleName, $this->_restfulModules) !== false);
  302. }
  303. }