/framework/vendor/zend/Zend/Rest/Route.php

http://zoop.googlecode.com/ · PHP · 401 lines · 229 code · 44 blank · 128 comment · 60 complexity · e67ab8811433f7b8c592f963cbc4aa05 MD5 · raw file

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