PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Rest/Route.php

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