Route.php 12 KB

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