RouterListener.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Bundle\FrameworkBundle\EventListener;
  11. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  14. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  17. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  18. use Symfony\Component\Routing\RouterInterface;
  19. /**
  20. * Initializes request attributes based on a matching route.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class RouterListener
  25. {
  26. private $router;
  27. private $logger;
  28. private $httpPort;
  29. private $httpsPort;
  30. public function __construct(RouterInterface $router, $httpPort = 80, $httpsPort = 443, LoggerInterface $logger = null)
  31. {
  32. $this->router = $router;
  33. $this->httpPort = $httpPort;
  34. $this->httpsPort = $httpsPort;
  35. $this->logger = $logger;
  36. }
  37. public function onEarlyKernelRequest(GetResponseEvent $event)
  38. {
  39. if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
  40. return;
  41. }
  42. $request = $event->getRequest();
  43. $context = $this->router->getContext();
  44. // set the context even if the parsing does not need to be done
  45. // to have correct link generation
  46. $context->setBaseUrl($request->getBaseUrl());
  47. $context->setMethod($request->getMethod());
  48. $context->setHost($request->getHost());
  49. $context->setScheme($request->getScheme());
  50. $context->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort());
  51. $context->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
  52. }
  53. public function onKernelRequest(GetResponseEvent $event)
  54. {
  55. $request = $event->getRequest();
  56. if ($request->attributes->has('_controller')) {
  57. // routing is already done
  58. return;
  59. }
  60. // add attributes based on the path info (routing)
  61. try {
  62. // The path is returned in decoded form from the request, so we need to
  63. // encode it again as the router applies its own decoding. This prevents
  64. // double-decoding.
  65. $parameters = $this->router->match(urlencode($request->getPathInfo()));
  66. if (null !== $this->logger) {
  67. $this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], $this->parametersToString($parameters)));
  68. }
  69. $request->attributes->add($parameters);
  70. } catch (ResourceNotFoundException $e) {
  71. $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
  72. throw new NotFoundHttpException($message, $e);
  73. } catch (MethodNotAllowedException $e) {
  74. $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), strtoupper(implode(', ', $e->getAllowedMethods())));
  75. throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
  76. }
  77. if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
  78. $context = $this->router->getContext();
  79. $session = $request->getSession();
  80. if ($locale = $request->attributes->get('_locale')) {
  81. if ($session) {
  82. $session->setLocale($locale);
  83. }
  84. $context->setParameter('_locale', $locale);
  85. } elseif ($session) {
  86. $context->setParameter('_locale', $session->getLocale());
  87. }
  88. }
  89. }
  90. private function parametersToString(array $parameters)
  91. {
  92. $pieces = array();
  93. foreach ($parameters as $key => $val) {
  94. $pieces[] = sprintf('"%s": "%s"', $key, (is_string($val) ? $val : json_encode($val)));
  95. }
  96. return implode(', ', $pieces);
  97. }
  98. }