Route.php 11 KB

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