PageRenderTime 27ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Rest/Route.php

https://github.com/Spavacz/NPG
PHP | 410 lines | 234 code | 48 blank | 128 comment | 63 complexity | 96d36129c965dbb0ce3788a4924a7e03 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. $pathOrig = $path;
  124. if ($path != '') {
  125. $path = explode(self::URI_DELIMITER, $path);
  126. // Determine Module
  127. $moduleName = $this->_defaults[$this->_moduleKey];
  128. $dispatcher = $this->_front->getDispatcher();
  129. if ($dispatcher && $dispatcher->isValidModule($path[0])) {
  130. $moduleName = $path[0];
  131. if ($this->_checkRestfulModule($moduleName)) {
  132. $values[$this->_moduleKey] = array_shift($path);
  133. $this->_moduleValid = true;
  134. }
  135. }
  136. // Determine Controller
  137. $controllerName = $this->_defaults[$this->_controllerKey];
  138. if (count($path) && !empty($path[0])) {
  139. if ($this->_checkRestfulController($moduleName, $path[0])) {
  140. $controllerName = $path[0];
  141. $values[$this->_controllerKey] = array_shift($path);
  142. $values[$this->_actionKey] = 'get';
  143. } else {
  144. // If Controller in URI is not found to be a RESTful
  145. // Controller, return false to fall back to other routes
  146. return false;
  147. }
  148. } elseif ($this->_checkRestfulController($moduleName, $controllerName)) {
  149. $values[$this->_controllerKey] = $controllerName;
  150. $values[$this->_actionKey] = 'get';
  151. } else {
  152. return false;
  153. }
  154. //Store path count for method mapping
  155. $pathElementCount = count($path);
  156. // Check for "special get" URI's
  157. $specialGetTarget = false;
  158. if ($pathElementCount && array_search($path[0], array('index', 'new')) > -1) {
  159. $specialGetTarget = array_shift($path);
  160. } elseif ($pathElementCount && $path[$pathElementCount-1] == 'edit') {
  161. $specialGetTarget = 'edit';
  162. $params['id'] = $path[$pathElementCount-2];
  163. } elseif ($pathElementCount == 1) {
  164. $params['id'] = urldecode(array_shift($path));
  165. } elseif ($pathElementCount == 0 && !isset($params['id'])) {
  166. $specialGetTarget = 'index';
  167. }
  168. // Digest URI params
  169. if ($numSegs = count($path)) {
  170. for ($i = 0; $i < $numSegs; $i = $i + 2) {
  171. $key = urldecode($path[$i]);
  172. $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
  173. $params[$key] = $val;
  174. }
  175. }
  176. // Determine Action
  177. $requestMethod = strtolower($request->getMethod());
  178. if ($requestMethod != 'get') {
  179. if ($request->getParam('_method')) {
  180. $values[$this->_actionKey] = strtolower($request->getParam('_method'));
  181. } elseif ( $request->getHeader('X-HTTP-Method-Override') ) {
  182. $values[$this->_actionKey] = strtolower($request->getHeader('X-HTTP-Method-Override'));
  183. } else {
  184. $values[$this->_actionKey] = $requestMethod;
  185. }
  186. // Map PUT and POST to actual create/update actions
  187. // based on parameter count (posting to resource or collection)
  188. switch( $values[$this->_actionKey] ){
  189. case 'post':
  190. if ($pathElementCount > 0) {
  191. $values[$this->_actionKey] = 'put';
  192. } else {
  193. $values[$this->_actionKey] = 'post';
  194. }
  195. break;
  196. case 'put':
  197. $values[$this->_actionKey] = 'put';
  198. break;
  199. }
  200. } elseif ($specialGetTarget) {
  201. $values[$this->_actionKey] = $specialGetTarget;
  202. }
  203. }
  204. if ($pathOrig != '' || $this->_allRestful()) {
  205. $this->_values = $values + $params;
  206. $result = $this->_values + $this->_defaults;
  207. if ($partial && $result) {
  208. $this->setMatchedPath($request->getPathInfo());
  209. }
  210. return $result;
  211. }
  212. return false;
  213. }
  214. /**
  215. * Assembles user submitted parameters forming a URL path defined by this route
  216. *
  217. * @param array $data An array of variable and value pairs used as parameters
  218. * @param bool $reset Weither to reset the current params
  219. * @param bool $encode Weither to return urlencoded string
  220. * @return string Route path with user submitted parameters
  221. */
  222. public function assemble($data = array(), $reset = false, $encode = true)
  223. {
  224. if (!$this->_keysSet) {
  225. if (null === $this->_request) {
  226. $this->_request = $this->_front->getRequest();
  227. }
  228. $this->_setRequestKeys();
  229. }
  230. $params = (!$reset) ? $this->_values : array();
  231. foreach ($data as $key => $value) {
  232. if ($value !== null) {
  233. $params[$key] = $value;
  234. } elseif (isset($params[$key])) {
  235. unset($params[$key]);
  236. }
  237. }
  238. $params += $this->_defaults;
  239. $url = '';
  240. if ($this->_moduleValid || array_key_exists($this->_moduleKey, $data)) {
  241. if ($params[$this->_moduleKey] != $this->_defaults[$this->_moduleKey]) {
  242. $module = $params[$this->_moduleKey];
  243. }
  244. }
  245. unset($params[$this->_moduleKey]);
  246. $controller = $params[$this->_controllerKey];
  247. unset($params[$this->_controllerKey]);
  248. unset($params[$this->_actionKey]);
  249. if (isset($params['index']) && $params['index']) {
  250. unset($params['index']);
  251. $url .= '/index';
  252. foreach ($params as $key => $value) {
  253. if ($encode) $value = urlencode($value);
  254. $url .= '/' . $key . '/' . $value;
  255. }
  256. } elseif (isset($params['id'])) {
  257. $url .= '/' . $params['id'];
  258. }
  259. if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
  260. $url = '/' . $controller . $url;
  261. }
  262. if (isset($module)) {
  263. $url = '/' . $module . $url;
  264. }
  265. return ltrim($url, self::URI_DELIMITER);
  266. }
  267. /**
  268. * Tells Rewrite Router which version this Route is
  269. *
  270. * @return int Route "version"
  271. */
  272. public function getVersion()
  273. {
  274. return 2;
  275. }
  276. /**
  277. * Parses the responders array sent to constructor to know
  278. * which modules and/or controllers are RESTful
  279. *
  280. * @param array $responders
  281. */
  282. protected function _parseResponders($responders)
  283. {
  284. $modulesOnly = true;
  285. foreach ($responders as $responder) {
  286. if(is_array($responder)) {
  287. $modulesOnly = false;
  288. break;
  289. }
  290. }
  291. if ($modulesOnly) {
  292. $this->_restfulModules = $responders;
  293. } else {
  294. $this->_restfulControllers = $responders;
  295. }
  296. }
  297. /**
  298. * Determine if a specified module supports RESTful routing
  299. *
  300. * @param string $moduleName
  301. * @return bool
  302. */
  303. protected function _checkRestfulModule($moduleName)
  304. {
  305. if ($this->_allRestful()) {
  306. return true;
  307. }
  308. if ($this->_fullRestfulModule($moduleName)) {
  309. return true;
  310. }
  311. if ($this->_restfulControllers && array_key_exists($moduleName, $this->_restfulControllers)) {
  312. return true;
  313. }
  314. return false;
  315. }
  316. /**
  317. * Determine if a specified module + controller combination supports
  318. * RESTful routing
  319. *
  320. * @param string $moduleName
  321. * @param string $controllerName
  322. * @return bool
  323. */
  324. protected function _checkRestfulController($moduleName, $controllerName)
  325. {
  326. if ($this->_allRestful()) {
  327. return true;
  328. }
  329. if ($this->_fullRestfulModule($moduleName)) {
  330. return true;
  331. }
  332. if ($this->_checkRestfulModule($moduleName)
  333. && $this->_restfulControllers
  334. && (false !== array_search($controllerName, $this->_restfulControllers[$moduleName]))
  335. ) {
  336. return true;
  337. }
  338. return false;
  339. }
  340. /**
  341. * Determines if RESTful routing applies to the entire app
  342. *
  343. * @return bool
  344. */
  345. protected function _allRestful()
  346. {
  347. return (!$this->_restfulModules && !$this->_restfulControllers);
  348. }
  349. /**
  350. * Determines if RESTful routing applies to an entire module
  351. *
  352. * @param string $moduleName
  353. * @return bool
  354. */
  355. protected function _fullRestfulModule($moduleName)
  356. {
  357. return (
  358. $this->_restfulModules
  359. && (false !==array_search($moduleName, $this->_restfulModules))
  360. );
  361. }
  362. }