PageRenderTime 94ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/flow3-1.0.0alpha7/Packages/Framework/FLOW3/Classes/MVC/Web/Routing/Router.php

https://github.com/ochoto/framework-benchs
PHP | 269 lines | 109 code | 22 blank | 138 comment | 10 complexity | bd97826f6523c18a69c0f8fa0dc19441 MD5 | raw file
  1. <?php
  2. declare(ENCODING = 'utf-8');
  3. namespace F3\FLOW3\MVC\Web\Routing;
  4. /* *
  5. * This script belongs to the FLOW3 framework. *
  6. * *
  7. * It is free software; you can redistribute it and/or modify it under *
  8. * the terms of the GNU Lesser General Public License as published by the *
  9. * Free Software Foundation, either version 3 of the License, or (at your *
  10. * option) any later version. *
  11. * *
  12. * This script is distributed in the hope that it will be useful, but *
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
  14. * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
  15. * General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU Lesser General Public *
  18. * License along with the script. *
  19. * If not, see http://www.gnu.org/licenses/lgpl.html *
  20. * *
  21. * The TYPO3 project - inspiring people to share! *
  22. * */
  23. /**
  24. * The default web router
  25. *
  26. * @version $Id: Router.php 3643 2010-01-15 14:38:07Z robert $
  27. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
  28. * @api
  29. */
  30. class Router implements \F3\FLOW3\MVC\Web\Routing\RouterInterface {
  31. /**
  32. * @var \F3\FLOW3\Object\ObjectManagerInterface
  33. */
  34. protected $objectManager;
  35. /**
  36. * @var \F3\FLOW3\Object\ObjectFactoryInterface
  37. */
  38. protected $objectFactory;
  39. /**
  40. * @var \F3\FLOW3\Utility\Environment
  41. */
  42. protected $environment;
  43. /**
  44. * @var \F3\FLOW3\Log\SystemLoggerInterface
  45. */
  46. protected $systemLogger;
  47. /**
  48. * Array containing the configuration for all routes.
  49. * @var array
  50. */
  51. protected $routesConfiguration = array();
  52. /**
  53. * Array of routes to match against
  54. * @var array
  55. */
  56. protected $routes = array();
  57. /**
  58. * TRUE if route object have been created, otherwise FALSE
  59. * @var boolean
  60. */
  61. protected $routesCreated = FALSE;
  62. /**
  63. * The current request. Will be set in route()
  64. * @var \F3\FLOW3\MVC\Web\Request
  65. */
  66. protected $request;
  67. /**
  68. * Injects the object manager
  69. *
  70. * @param \F3\FLOW3\Object\ObjectManagerInterface $objectManager
  71. * @return void
  72. * @author Karsten Dambekalns <karsten@typo3.org>
  73. */
  74. public function injectObjectManager(\F3\FLOW3\Object\ObjectManagerInterface $objectManager) {
  75. $this->objectManager = $objectManager;
  76. }
  77. /**
  78. * Injects the object factory
  79. *
  80. * @param \F3\FLOW3\Object\ObjectFactoryInterface $objectFactory
  81. * @return void
  82. * @author Karsten Dambekalns <karsten@typo3.org>
  83. */
  84. public function injectObjectFactory(\F3\FLOW3\Object\ObjectFactoryInterface $objectFactory) {
  85. $this->objectFactory = $objectFactory;
  86. }
  87. /**
  88. * Injects the environment
  89. *
  90. * @param \F3\FLOW3\Utility\Environment $environment
  91. * @return void
  92. * @author Karsten Dambekalns <karsten@typo3.org>
  93. */
  94. public function injectEnvironment(\F3\FLOW3\Utility\Environment $environment) {
  95. $this->environment = $environment;
  96. }
  97. /**
  98. * Injects the system logger
  99. *
  100. * @param \F3\FLOW3\Log\SystemLoggerInterface $systemLogger
  101. * @return void
  102. * @author Robert Lemke <robert@typo3.org>
  103. */
  104. public function injectSystemLogger(\F3\FLOW3\Log\SystemLoggerInterface $systemLogger) {
  105. $this->systemLogger = $systemLogger;
  106. }
  107. /**
  108. * Sets the routes configuration.
  109. *
  110. * @param array $routesConfiguration The routes configuration
  111. * @return void
  112. * @author Bastian Waidelich <bastian@typo3.org>
  113. */
  114. public function setRoutesConfiguration(array $routesConfiguration) {
  115. $this->routesConfiguration = $routesConfiguration;
  116. }
  117. /**
  118. * Routes the specified web request by setting the controller name, action and possible
  119. * parameters. If the request could not be routed, it will be left untouched.
  120. *
  121. * @param \F3\FLOW3\MVC\Web\Request $request The web request to be analyzed. Will be modified by the router.
  122. * @return void
  123. * @author Robert Lemke <robert@typo3.org>
  124. * @author Bastian Waidelich <bastian@typo3.org>
  125. * @author Karsten Dambekalns <karsten@typo3.org>
  126. */
  127. public function route(\F3\FLOW3\MVC\Web\Request $request) {
  128. $this->request = $request;
  129. $requestPath = $this->request->getRequestPath();
  130. $matchResults = $this->findMatchResults($requestPath);
  131. if ($matchResults !== NULL) {
  132. $this->setControllerKeysAndFormat($matchResults);
  133. foreach ($matchResults as $argumentName => $argumentValue) {
  134. if ($argumentName[0] !== '@') {
  135. $this->request->setArgument($argumentName, $argumentValue);
  136. }
  137. }
  138. }
  139. $this->setControllerKeysAndFormat($this->request->getArguments());
  140. }
  141. /**
  142. * Sets package key, subpackage key, controller name, action name and format
  143. * of the current request.
  144. *
  145. * @param array $arguments
  146. * @return void
  147. * @author Bastian Waidelich <bastian@typo3.org>
  148. * @author Karsten Dambekalns <karsten@typo3.org>
  149. * @see \F3\FLOW3\MVC\Web\Request
  150. * @api
  151. */
  152. protected function setControllerKeysAndFormat(array $arguments) {
  153. foreach($arguments as $argumentName => $argumentValue) {
  154. switch ($argumentName) {
  155. case '@package' :
  156. $this->request->setControllerPackageKey($argumentValue);
  157. break;
  158. case '@subpackage' :
  159. $this->request->setControllerSubpackageKey($argumentValue);
  160. break;
  161. case '@controller' :
  162. $this->request->setControllerName($argumentValue);
  163. break;
  164. case '@action' :
  165. $this->request->setControllerActionName(lcfirst($argumentValue));
  166. break;
  167. case '@format' :
  168. $this->request->setFormat(strtolower($argumentValue));
  169. break;
  170. }
  171. }
  172. }
  173. /**
  174. * Iterates through all configured routes and calls matches() on them.
  175. * Returns the matchResults of the matching route or NULL if no matching
  176. * route could be found.
  177. * Note: calls of this message are cached by RouterCachingAspect
  178. *
  179. * @param string $requestPath The request path
  180. * @return array results of the matching route
  181. * @see route()
  182. * @author Bastian Waidelich <bastian@typo3.org>
  183. */
  184. protected function findMatchResults($requestPath) {
  185. $this->createRoutesFromConfiguration();
  186. foreach ($this->routes as $route) {
  187. if ($route->matches($requestPath)) {
  188. $matchResults = $route->getMatchResults();
  189. $this->systemLogger->log('Router route(): Route "' . $route->getName() . '" matched the request path "' . $requestPath . '".', LOG_DEBUG);
  190. return $matchResults;
  191. }
  192. }
  193. $this->systemLogger->log('Router route(): No route matched the request path "' . $requestPath . '".', LOG_NOTICE);
  194. return NULL;
  195. }
  196. /**
  197. * Builds the corresponding uri (excluding protocol and host) by iterating
  198. * through all configured routes and calling their respective resolves()
  199. * method. If no matching route is found, an empty string is returned.
  200. * Note: calls of this message are cached by RouterCachingAspect
  201. *
  202. * @param array $routeValues Key/value pairs to be resolved. E.g. array('@package' => 'MyPackage', '@controller' => 'MyController');
  203. * @return string
  204. * @author Bastian Waidelich <bastian@typo3.org>
  205. */
  206. public function resolve(array $routeValues) {
  207. $this->createRoutesFromConfiguration();
  208. foreach ($this->routes as $route) {
  209. if ($route->resolves($routeValues)) {
  210. return $route->getMatchingUri();
  211. }
  212. }
  213. $this->systemLogger->log('Router resolve(): No route matched ' . str_replace(chr(10), '', var_export($routeValues, TRUE)) . '.', LOG_NOTICE);
  214. return '';
  215. }
  216. /**
  217. * Creates F3\FLOW3\MVC\Web\Routing\Route objects from the injected routes
  218. * configuration.
  219. *
  220. * @return void
  221. * @author Bastian Waidelich <bastian@typo3.org>
  222. */
  223. protected function createRoutesFromConfiguration() {
  224. if ($this->routesCreated === FALSE) {
  225. $this->routes = array();
  226. foreach ($this->routesConfiguration as $routeConfiguration) {
  227. $route = $this->objectFactory->create('F3\FLOW3\MVC\Web\Routing\Route');
  228. if (isset($routeConfiguration['name'])) {
  229. $route->setName($routeConfiguration['name']);
  230. }
  231. $route->setUriPattern($routeConfiguration['uriPattern']);
  232. if (isset($routeConfiguration['defaults'])) {
  233. $route->setDefaults($routeConfiguration['defaults']);
  234. }
  235. if (isset($routeConfiguration['routeParts'])) {
  236. $route->setRoutePartsConfiguration($routeConfiguration['routeParts']);
  237. }
  238. if (isset($routeConfiguration['toLowerCase'])) {
  239. $route->setLowerCase($routeConfiguration['toLowerCase']);
  240. }
  241. $this->routes[] = $route;
  242. }
  243. $this->routesCreated = TRUE;
  244. }
  245. }
  246. }
  247. ?>