PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/http-kernel/EventListener/RouterListener.php

https://gitlab.com/reasonat/test8
PHP | 191 lines | 117 code | 26 blank | 48 comment | 22 complexity | 8c9024c3b8f9804019eab9140935a29c MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\EventListener;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  13. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  19. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  20. use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
  21. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  22. use Symfony\Component\Routing\RequestContext;
  23. use Symfony\Component\Routing\RequestContextAwareInterface;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. use Symfony\Component\HttpFoundation\Request;
  26. /**
  27. * Initializes the context from the request and sets request attributes based on a matching route.
  28. *
  29. * This listener works in 2 modes:
  30. *
  31. * * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
  32. * * 2.4+ mode where you must pass a RequestStack instance in the constructor.
  33. *
  34. * @author Fabien Potencier <fabien@symfony.com>
  35. */
  36. class RouterListener implements EventSubscriberInterface
  37. {
  38. private $matcher;
  39. private $context;
  40. private $logger;
  41. private $request;
  42. private $requestStack;
  43. /**
  44. * Constructor.
  45. *
  46. * RequestStack will become required in 3.0.
  47. *
  48. * @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
  49. * @param RequestStack $requestStack A RequestStack instance
  50. * @param RequestContext|null $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
  51. * @param LoggerInterface|null $logger The logger
  52. *
  53. * @throws \InvalidArgumentException
  54. */
  55. public function __construct($matcher, $requestStack = null, $context = null, $logger = null)
  56. {
  57. if ($requestStack instanceof RequestContext || $context instanceof LoggerInterface || $logger instanceof RequestStack) {
  58. $tmp = $requestStack;
  59. $requestStack = $logger;
  60. $logger = $context;
  61. $context = $tmp;
  62. @trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as second argument as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
  63. } elseif (!$requestStack instanceof RequestStack) {
  64. @trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
  65. }
  66. if (null !== $requestStack && !$requestStack instanceof RequestStack) {
  67. throw new \InvalidArgumentException('RequestStack instance expected.');
  68. }
  69. if (null !== $context && !$context instanceof RequestContext) {
  70. throw new \InvalidArgumentException('RequestContext instance expected.');
  71. }
  72. if (null !== $logger && !$logger instanceof LoggerInterface) {
  73. throw new \InvalidArgumentException('Logger must implement LoggerInterface.');
  74. }
  75. if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
  76. throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
  77. }
  78. if (null === $context && !$matcher instanceof RequestContextAwareInterface) {
  79. throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');
  80. }
  81. $this->matcher = $matcher;
  82. $this->context = $context ?: $matcher->getContext();
  83. $this->requestStack = $requestStack;
  84. $this->logger = $logger;
  85. }
  86. /**
  87. * Sets the current Request.
  88. *
  89. * This method was used to synchronize the Request, but as the HttpKernel
  90. * is doing that automatically now, you should never call it directly.
  91. * It is kept public for BC with the 2.3 version.
  92. *
  93. * @param Request|null $request A Request instance
  94. *
  95. * @deprecated since version 2.4, to be removed in 3.0.
  96. */
  97. public function setRequest(Request $request = null)
  98. {
  99. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.4 and will be made private in 3.0.', E_USER_DEPRECATED);
  100. $this->setCurrentRequest($request);
  101. }
  102. private function setCurrentRequest(Request $request = null)
  103. {
  104. if (null !== $request && $this->request !== $request) {
  105. $this->context->fromRequest($request);
  106. }
  107. $this->request = $request;
  108. }
  109. public function onKernelFinishRequest(FinishRequestEvent $event)
  110. {
  111. if (null === $this->requestStack) {
  112. return; // removed when requestStack is required
  113. }
  114. $this->setCurrentRequest($this->requestStack->getParentRequest());
  115. }
  116. public function onKernelRequest(GetResponseEvent $event)
  117. {
  118. $request = $event->getRequest();
  119. // initialize the context that is also used by the generator (assuming matcher and generator share the same context instance)
  120. // we call setCurrentRequest even if most of the time, it has already been done to keep compatibility
  121. // with frameworks which do not use the Symfony service container
  122. // when we have a RequestStack, no need to do it
  123. if (null !== $this->requestStack) {
  124. $this->setCurrentRequest($request);
  125. }
  126. if ($request->attributes->has('_controller')) {
  127. // routing is already done
  128. return;
  129. }
  130. // add attributes based on the request (routing)
  131. try {
  132. // matching a request is more powerful than matching a URL path + context, so try that first
  133. if ($this->matcher instanceof RequestMatcherInterface) {
  134. $parameters = $this->matcher->matchRequest($request);
  135. } else {
  136. $parameters = $this->matcher->match($request->getPathInfo());
  137. }
  138. if (null !== $this->logger) {
  139. $this->logger->info(sprintf('Matched route "%s".', isset($parameters['_route']) ? $parameters['_route'] : 'n/a'), array(
  140. 'route_parameters' => $parameters,
  141. 'request_uri' => $request->getUri(),
  142. ));
  143. }
  144. $request->attributes->add($parameters);
  145. unset($parameters['_route'], $parameters['_controller']);
  146. $request->attributes->set('_route_params', $parameters);
  147. } catch (ResourceNotFoundException $e) {
  148. $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
  149. if ($referer = $request->headers->get('referer')) {
  150. $message .= sprintf(' (from "%s")', $referer);
  151. }
  152. throw new NotFoundHttpException($message, $e);
  153. } catch (MethodNotAllowedException $e) {
  154. $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), implode(', ', $e->getAllowedMethods()));
  155. throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
  156. }
  157. }
  158. public static function getSubscribedEvents()
  159. {
  160. return array(
  161. KernelEvents::REQUEST => array(array('onKernelRequest', 32)),
  162. KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
  163. );
  164. }
  165. }