PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Rest/Route.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 413 lines | 240 code | 44 blank | 129 comment | 63 complexity | 4945190146abd4924f7002374852b166 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-2011 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 24013 2011-05-04 21:19:12Z ralph $
  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-2011 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'] = urldecode($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]) ? $path[$i + 1] : null;
  172. $params[$key] = urldecode($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. // set $action if value given is 'new' or 'edit'
  244. if (in_array($params[$this->_actionKey], array('new', 'edit'))) {
  245. $action = $params[$this->_actionKey];
  246. }
  247. unset($params[$this->_actionKey]);
  248. if (isset($params['index']) && $params['index']) {
  249. unset($params['index']);
  250. $url .= '/index';
  251. if (isset($params['id'])) {
  252. $url .= '/'.$params['id'];
  253. unset($params['id']);
  254. }
  255. foreach ($params as $key => $value) {
  256. if ($encode) $value = urlencode($value);
  257. $url .= '/' . $key . '/' . $value;
  258. }
  259. } elseif (! empty($action) && isset($params['id'])) {
  260. $url .= sprintf('/%s/%s', $params['id'], $action);
  261. } elseif (! empty($action)) {
  262. $url .= sprintf('/%s', $action);
  263. } elseif (isset($params['id'])) {
  264. $url .= '/' . $params['id'];
  265. }
  266. if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
  267. $url = '/' . $controller . $url;
  268. }
  269. if (isset($module)) {
  270. $url = '/' . $module . $url;
  271. }
  272. return ltrim($url, self::URI_DELIMITER);
  273. }
  274. /**
  275. * Tells Rewrite Router which version this Route is
  276. *
  277. * @return int Route "version"
  278. */
  279. public function getVersion()
  280. {
  281. return 2;
  282. }
  283. /**
  284. * Parses the responders array sent to constructor to know
  285. * which modules and/or controllers are RESTful
  286. *
  287. * @param array $responders
  288. */
  289. protected function _parseResponders($responders)
  290. {
  291. $modulesOnly = true;
  292. foreach ($responders as $responder) {
  293. if(is_array($responder)) {
  294. $modulesOnly = false;
  295. break;
  296. }
  297. }
  298. if ($modulesOnly) {
  299. $this->_restfulModules = $responders;
  300. } else {
  301. $this->_restfulControllers = $responders;
  302. }
  303. }
  304. /**
  305. * Determine if a specified module supports RESTful routing
  306. *
  307. * @param string $moduleName
  308. * @return bool
  309. */
  310. protected function _checkRestfulModule($moduleName)
  311. {
  312. if ($this->_allRestful()) {
  313. return true;
  314. }
  315. if ($this->_fullRestfulModule($moduleName)) {
  316. return true;
  317. }
  318. if ($this->_restfulControllers && array_key_exists($moduleName, $this->_restfulControllers)) {
  319. return true;
  320. }
  321. return false;
  322. }
  323. /**
  324. * Determine if a specified module + controller combination supports
  325. * RESTful routing
  326. *
  327. * @param string $moduleName
  328. * @param string $controllerName
  329. * @return bool
  330. */
  331. protected function _checkRestfulController($moduleName, $controllerName)
  332. {
  333. if ($this->_allRestful()) {
  334. return true;
  335. }
  336. if ($this->_fullRestfulModule($moduleName)) {
  337. return true;
  338. }
  339. if ($this->_checkRestfulModule($moduleName)
  340. && $this->_restfulControllers
  341. && (false !== array_search($controllerName, $this->_restfulControllers[$moduleName]))
  342. ) {
  343. return true;
  344. }
  345. return false;
  346. }
  347. /**
  348. * Determines if RESTful routing applies to the entire app
  349. *
  350. * @return bool
  351. */
  352. protected function _allRestful()
  353. {
  354. return (!$this->_restfulModules && !$this->_restfulControllers);
  355. }
  356. /**
  357. * Determines if RESTful routing applies to an entire module
  358. *
  359. * @param string $moduleName
  360. * @return bool
  361. */
  362. protected function _fullRestfulModule($moduleName)
  363. {
  364. return (
  365. $this->_restfulModules
  366. && (false !==array_search($moduleName, $this->_restfulModules))
  367. );
  368. }
  369. }