vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Routing/RequestListener.php line 50

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\WebsiteBundle\Routing;
  11. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Routing\RequestContextAwareInterface;
  16. /**
  17. * Create your own RequestListener with lower priority if you need to change the behaviour of this one.
  18. *
  19. * @internal
  20. *
  21. * @final
  22. */
  23. class RequestListener implements EventSubscriberInterface
  24. {
  25. /**
  26. * @var RequestContextAwareInterface
  27. */
  28. private $router;
  29. /**
  30. * @var RequestAnalyzerInterface
  31. */
  32. private $requestAnalyzer;
  33. public function __construct(RequestContextAwareInterface $router, RequestAnalyzerInterface $requestAnalyzer)
  34. {
  35. $this->router = $router;
  36. $this->requestAnalyzer = $requestAnalyzer;
  37. }
  38. public static function getSubscribedEvents(): array
  39. {
  40. return [KernelEvents::REQUEST => ['onRequest', 31]]; // directly after the RouterListener where the portal information is set
  41. }
  42. public function onRequest(RequestEvent $event): void
  43. {
  44. $context = $this->router->getContext();
  45. $portalInformation = $this->requestAnalyzer->getPortalInformation();
  46. $request = $event->getRequest();
  47. if ($request->attributes->get('internalRequest', false)) {
  48. return;
  49. }
  50. if ($portalInformation) {
  51. if (!$context->hasParameter('prefix')) {
  52. $context->setParameter('prefix', $portalInformation->getPrefix());
  53. }
  54. if (!$context->hasParameter('host')) {
  55. $context->setParameter('host', $portalInformation->getHost());
  56. }
  57. }
  58. }
  59. }