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

SiteAccessMatchListener.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\EventListener;
8
9 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
10 use eZ\Publish\Core\MVC\Symfony\Component\Serializer\SerializerTrait;
11 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12 use Symfony\Component\HttpFoundation\Request;
13 use Symfony\Component\HttpFoundation\RequestMatcherInterface;
14 use Symfony\Component\HttpKernel\KernelEvents;
15 use eZ\Publish\Core\MVC\Symfony\SiteAccess;
16 use eZ\Publish\Core\MVC\Symfony\SiteAccess\Router as SiteAccessRouter;
17 use eZ\Publish\Core\MVC\Symfony\Event\PostSiteAccessMatchEvent;
18 use eZ\Publish\Core\MVC\Symfony\MVCEvents;
19 use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest;
20 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
22
27 class SiteAccessMatchListener implements EventSubscriberInterface
28 {
29 use SerializerTrait;
30
32 protected $siteAccessRouter;
33
35 protected $eventDispatcher;
36
42 private $userContextRequestMatcher;
43
44 public function __construct(
45 SiteAccessRouter $siteAccessRouter,
46 EventDispatcherInterface $eventDispatcher,
47 RequestMatcherInterface $userContextRequestMatcher
48 ) {
49 $this->siteAccessRouter = $siteAccessRouter;
50 $this->eventDispatcher = $eventDispatcher;
51 $this->userContextRequestMatcher = $userContextRequestMatcher;
52 }
53
54 public static function getSubscribedEvents()
55 {
56 return [
57 // Should take place just after FragmentListener (priority 48) in order to get rebuilt request attributes in case of subrequest
58 KernelEvents::REQUEST => ['onKernelRequest', 45],
59 ];
60 }
61
65 public function onKernelRequest(GetResponseEvent $event)
66 {
67 $request = $event->getRequest();
68
69 // Don't try to match when it's a user hash request. SiteAccess is irrelevant in this case.
70 if ($this->userContextRequestMatcher->matches($request) && !$request->attributes->has('_ez_original_request')) {
71 return;
72 }
73
74 // We have a serialized siteaccess object from a fragment (sub-request), we need to get it back.
75 if ($request->attributes->has('serialized_siteaccess')) {
76 $serializer = $this->getSerializer();
78 $siteAccess = $serializer->deserialize($request->attributes->get('serialized_siteaccess'), SiteAccess::class, 'json');
79 if ($siteAccess->matcher !== null) {
80 if (in_array(SiteAccess\Matcher::class, class_implements($siteAccess->matcher))) {
81 $siteAccess->matcher = $serializer->deserialize($request->attributes->get('serialized_siteaccess_matcher'), $siteAccess->matcher, 'json');
82 } else {
83 throw new InvalidArgumentException(
84 'matcher',
85 sprintf(
86 'SiteAccess matcher must implement %s or %s',
87 SiteAccess\Matcher::class,
88 SiteAccess\URILexer::class
89 )
90 );
91 }
92 if ($request->attributes->get('serialized_siteaccess_sub_matchers')) {
93 $subMatchers = [];
94 foreach ($request->attributes->get('serialized_siteaccess_sub_matchers') as $matcherClass => $serializedData) {
95 $subMatchers[$matcherClass] = $serializer->deserialize($serializedData, $matcherClass, 'json');
96 }
97 $siteAccess->matcher->setSubMatchers($subMatchers);
98 }
99 }
100
101 $request->attributes->set(
102 'siteaccess',
103 $siteAccess
104 );
105 $request->attributes->remove('serialized_siteaccess');
106 } elseif (!$request->attributes->has('siteaccess')) {
107 // Get SiteAccess from original request if present ("_ez_original_request" attribute), or current request otherwise.
108 // "_ez_original_request" attribute is present in the case of user context hash generation (aka "user hash request").
109 $request->attributes->set(
110 'siteaccess',
111 $this->getSiteAccessFromRequest($request->attributes->get('_ez_original_request', $request))
112 );
113 }
114
115 $siteaccess = $request->attributes->get('siteaccess');
116 if ($siteaccess instanceof SiteAccess) {
117 $siteAccessEvent = new PostSiteAccessMatchEvent($siteaccess, $request, $event->getRequestType());
118 $this->eventDispatcher->dispatch(MVCEvents::SITEACCESS, $siteAccessEvent);
119 }
120 }
121
127 private function getSiteAccessFromRequest(Request $request)
128 {
129 return $this->siteAccessRouter->match(
130 new SimplifiedRequest(
131 [
132 'scheme' => $request->getScheme(),
133 'host' => $request->getHost(),
134 'port' => $request->getPort(),
135 'pathinfo' => $request->getPathInfo(),
136 'queryParams' => $request->query->all(),
137 'languages' => $request->getLanguages(),
138 'headers' => $request->headers->all(),
139 ]
140 )
141 );
142 }
143 }
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\MVC\Symfony\EventListener\SiteAccessMatchListener
kernel.request listener, triggers SiteAccess matching.
Definition: SiteAccessMatchListener.php:28
eZ\Publish\Core\MVC\Symfony\EventListener\SiteAccessMatchListener\getSiteAccessFromRequest
getSiteAccessFromRequest(Request $request)
Definition: SiteAccessMatchListener.php:127
eZ\Publish\Core\MVC\Symfony\EventListener\SiteAccessMatchListener\getSubscribedEvents
static getSubscribedEvents()
Definition: SiteAccessMatchListener.php:54
eZ\Publish\Core\MVC\Symfony\EventListener\SiteAccessMatchListener\$userContextRequestMatcher
$userContextRequestMatcher
Definition: SiteAccessMatchListener.php:42
eZ\Publish\Core\MVC\Symfony\EventListener\SiteAccessMatchListener\__construct
__construct(SiteAccessRouter $siteAccessRouter, EventDispatcherInterface $eventDispatcher, RequestMatcherInterface $userContextRequestMatcher)
Definition: SiteAccessMatchListener.php:44
eZ\Publish\Core\MVC\Symfony\EventListener\SiteAccessMatchListener\$eventDispatcher
$eventDispatcher
Definition: SiteAccessMatchListener.php:35
eZ\Publish\Core\MVC\Symfony\EventListener\SiteAccessMatchListener\$siteAccessRouter
$siteAccessRouter
Definition: SiteAccessMatchListener.php:32
eZ\Publish\Core\MVC\Symfony\Event\PostSiteAccessMatchEvent
This event is triggered after SiteAccess matching process and allows further control on it and the as...
Definition: PostSiteAccessMatchEvent.php:17
eZ\Publish\Core\MVC\Symfony\MVCEvents
Definition: MVCEvents.php:10
eZ\Publish\Core\MVC\Symfony\MVCEvents\SITEACCESS
const SITEACCESS
The SITEACCESS event occurs after the SiteAccess matching has occurred.
Definition: MVCEvents.php:17
eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest
Definition: SimplifiedRequest.php:21
eZ\Publish\Core\MVC\Symfony\SiteAccess\Router
Definition: Publish/Core/MVC/Symfony/SiteAccess/Router.php:17
eZ\Publish\Core\MVC\Symfony\SiteAccess
Base struct for a siteaccess representation.
Definition: SiteAccess.php:16
eZ\Publish\Core\MVC\Symfony\EventListener
Definition: LanguageSwitchListener.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.