Route.php 11 KB

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