Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

RequestEventListener.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\EventListener;
8
9 use eZ\Publish\Core\MVC\ConfigResolverInterface;
10 use eZ\Publish\Core\MVC\Symfony\SiteAccess;
11 use eZ\Publish\Core\MVC\Symfony\SiteAccess\URILexer;
12 use Psr\Log\LoggerInterface;
13 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14 use Symfony\Component\HttpFoundation\RedirectResponse;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17 use Symfony\Component\HttpKernel\HttpKernelInterface;
18 use Symfony\Component\HttpKernel\KernelEvents;
19 use Symfony\Component\Routing\RouterInterface;
20
21 class RequestEventListener implements EventSubscriberInterface
22 {
24 private $logger;
25
27 private $configResolver;
28
30 private $defaultSiteAccess;
31
33 private $router;
34
35 public function __construct(ConfigResolverInterface $configResolver, RouterInterface $router, $defaultSiteAccess, LoggerInterface $logger = null)
36 {
37 $this->configResolver = $configResolver;
38 $this->defaultSiteAccess = $defaultSiteAccess;
39 $this->router = $router;
40 $this->logger = $logger;
41 }
42
43 public static function getSubscribedEvents()
44 {
45 return [
46 KernelEvents::REQUEST => [
47 ['onKernelRequestForward', 10],
48 ['onKernelRequestRedirect', 0],
49 ],
50 ];
51 }
52
56 public function onKernelRequestForward(GetResponseEvent $event)
57 {
58 if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) {
59 $request = $event->getRequest();
60 if ($request->attributes->get('needsForward') && $request->attributes->has('semanticPathinfo')) {
61 $semanticPathinfo = $request->attributes->get('semanticPathinfo');
62 $request->attributes->remove('needsForward');
63 $forwardRequest = Request::create(
64 $semanticPathinfo,
65 $request->getMethod(),
66 $request->getMethod() === 'POST' ? $request->request->all() : $request->query->all(),
67 $request->cookies->all(),
68 $request->files->all(),
69 $request->server->all(),
70 $request->getContent()
71 );
72 $forwardRequest->attributes->add($request->attributes->all());
73 if ($request->headers->has('X-User-Hash')) {
74 $forwardRequest->headers->set('X-User-Hash', $request->headers->get('X-User-Hash'));
75 }
76 // Not forcing HttpKernelInterface::SUB_REQUEST on purpose since we're very early here
77 // and we need to bootstrap essential stuff like sessions.
78 $event->setResponse($event->getKernel()->handle($forwardRequest));
79 $event->stopPropagation();
80
81 if (isset($this->logger)) {
82 $this->logger->info(
83 "URLAlias made request to be forwarded to $semanticPathinfo",
84 ['pathinfo' => $request->getPathInfo()]
85 );
86 }
87 }
88 }
89 }
90
101 public function onKernelRequestRedirect(GetResponseEvent $event)
102 {
103 if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
104 $request = $event->getRequest();
105 if ($request->attributes->get('needsRedirect') && $request->attributes->has('semanticPathinfo')) {
106 $siteaccess = $request->attributes->get('siteaccess');
107 $semanticPathinfo = $request->attributes->get('semanticPathinfo');
108 $queryString = $request->getQueryString();
109 if (
110 $request->attributes->get('prependSiteaccessOnRedirect', true)
111 && $siteaccess instanceof SiteAccess
112 && $siteaccess->matcher instanceof URILexer
113 ) {
114 $semanticPathinfo = $siteaccess->matcher->analyseLink($semanticPathinfo);
115 }
116
117 $headers = [];
118 if ($request->attributes->has('locationId')) {
119 $headers['X-Location-Id'] = $request->attributes->get('locationId');
120 }
121 $event->setResponse(
122 new RedirectResponse(
123 $semanticPathinfo . ($queryString ? "?$queryString" : ''),
124 301,
125 $headers
126 )
127 );
128 $event->stopPropagation();
129
130 if (isset($this->logger)) {
131 $this->logger->info(
132 "URLAlias made request to be redirected to $semanticPathinfo",
133 ['pathinfo' => $request->getPathInfo()]
134 );
135 }
136 }
137 }
138 }
139 }
eZ\Bundle\EzPublishCoreBundle\EventListener\RequestEventListener
Definition: RequestEventListener.php:22
eZ\Bundle\EzPublishCoreBundle\EventListener\RequestEventListener\getSubscribedEvents
static getSubscribedEvents()
Definition: RequestEventListener.php:43
eZ\Bundle\EzPublishCoreBundle\EventListener\RequestEventListener\$configResolver
$configResolver
Definition: RequestEventListener.php:27
eZ\Bundle\EzPublishCoreBundle\EventListener\RequestEventListener\onKernelRequestForward
onKernelRequestForward(GetResponseEvent $event)
Definition: RequestEventListener.php:56
eZ\Bundle\EzPublishCoreBundle\EventListener\RequestEventListener\$logger
$logger
Definition: RequestEventListener.php:24
eZ\Bundle\EzPublishCoreBundle\EventListener\RequestEventListener\$router
$router
Definition: RequestEventListener.php:33
eZ\Bundle\EzPublishCoreBundle\EventListener\RequestEventListener\$defaultSiteAccess
$defaultSiteAccess
Definition: RequestEventListener.php:30
eZ\Bundle\EzPublishCoreBundle\EventListener\RequestEventListener\__construct
__construct(ConfigResolverInterface $configResolver, RouterInterface $router, $defaultSiteAccess, LoggerInterface $logger=null)
Definition: RequestEventListener.php:35
eZ\Bundle\EzPublishCoreBundle\EventListener\RequestEventListener\onKernelRequestRedirect
onKernelRequestRedirect(GetResponseEvent $event)
Checks if the request needs to be redirected and return a RedirectResponse in such case.
Definition: RequestEventListener.php:101
eZ\Publish\Core\MVC\Symfony\SiteAccess
Base struct for a siteaccess representation.
Definition: SiteAccess.php:16
eZ\Publish\Core\MVC\ConfigResolverInterface
Interface for config resolvers.
Definition: ConfigResolverInterface.php:19
eZ\Publish\Core\MVC\Symfony\SiteAccess\URILexer
Interface for SiteAccess matchers that need to alter the URI after matching.
Definition: URILexer.php:14
eZ\Bundle\EzPublishCoreBundle\EventListener
Definition: BackgroundIndexingTerminateListener.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.