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

SiteAccessListener.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\EventListener;
8
9 use eZ\Publish\Core\MVC\Symfony\MVCEvents;
10 use eZ\Publish\Core\MVC\Symfony\Event\PostSiteAccessMatchEvent;
11 use eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator;
12 use eZ\Publish\Core\MVC\Symfony\SiteAccess\URILexer;
13 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
14 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16 use Symfony\Component\Routing\RouterInterface;
17 use Symfony\Component\Security\Http\HttpUtils;
18
22 class SiteAccessListener implements EventSubscriberInterface, ContainerAwareInterface
23 {
24 use ContainerAwareTrait;
25
27 private $defaultRouter;
28
30 private $urlAliasGenerator;
31
33 private $httpUtils;
34
35 public function __construct(RouterInterface $defaultRouter, UrlAliasGenerator $urlAliasGenerator, HttpUtils $httpUtils)
36 {
37 $this->defaultRouter = $defaultRouter;
38 $this->urlAliasGenerator = $urlAliasGenerator;
39 $this->httpUtils = $httpUtils;
40 }
41
42 public static function getSubscribedEvents()
43 {
44 return [
45 MVCEvents::SITEACCESS => ['onSiteAccessMatch', 255],
46 ];
47 }
48
49 public function onSiteAccessMatch(PostSiteAccessMatchEvent $event)
50 {
51 $request = $event->getRequest();
52 $matchedSiteAccess = $event->getSiteAccess();
53
54 $siteAccess = $this->container->get('ezpublish.siteaccess');
55 $siteAccess->name = $matchedSiteAccess->name;
56 $siteAccess->matchingType = $matchedSiteAccess->matchingType;
57 $siteAccess->matcher = $matchedSiteAccess->matcher;
58
59 // We already have semanticPathinfo (sub-request)
60 if ($request->attributes->has('semanticPathinfo')) {
61 $vpString = $request->attributes->get('viewParametersString');
62 if (!empty($vpString)) {
63 $request->attributes->set(
64 'viewParameters',
65 $this->generateViewParametersArray($vpString)
66 );
67 } else {
68 $request->attributes->set('viewParametersString', '');
69 $request->attributes->set('viewParameters', []);
70 }
71
72 return;
73 }
74
75 // Analyse the pathinfo if needed since it might contain the siteaccess (i.e. like in URI mode)
76 $pathinfo = rawurldecode($request->getPathInfo());
77 if ($siteAccess->matcher instanceof URILexer) {
78 $semanticPathinfo = $siteAccess->matcher->analyseURI($pathinfo);
79 } else {
80 $semanticPathinfo = $pathinfo;
81 }
82
83 // Get view parameters and cleaned up pathinfo (without view parameters string)
84 list($semanticPathinfo, $viewParameters, $viewParametersString) = $this->getViewParameters($semanticPathinfo);
85
86 // Storing the modified pathinfo in 'semanticPathinfo' request attribute, to keep a trace of it.
87 // Routers implementing RequestMatcherInterface should thus use this attribute instead of the original pathinfo
88 $request->attributes->set('semanticPathinfo', $semanticPathinfo);
89 $request->attributes->set('viewParameters', $viewParameters);
90 $request->attributes->set('viewParametersString', $viewParametersString);
91 }
92
103 private function getViewParameters($pathinfo)
104 {
105 // No view parameters, get out of here.
106 if (
107 strpos($pathinfo, ')/') === false
108 || ($vpStart = strpos($pathinfo, '/(')) === false
109 ) {
110 return [$pathinfo, [], ''];
111 }
112
113 $vpString = substr($pathinfo, $vpStart + 1);
114 $viewParameters = $this->generateViewParametersArray($vpString);
115
116 // Now remove the view parameters string from $semanticPathinfo
117 $pathinfo = substr($pathinfo, 0, $vpStart);
118
119 return [$pathinfo, $viewParameters, "/$vpString"];
120 }
121
129 private function generateViewParametersArray($vpString)
130 {
131 $vpString = trim($vpString, '/');
132 $viewParameters = [];
133
134 $vpSegments = explode('/', $vpString);
135 for ($i = 0, $iMax = count($vpSegments); $i < $iMax; ++$i) {
136 if (empty($vpSegments[$i])) {
137 continue;
138 }
139
140 // View parameter name.
141 // We extract it + the value from the following segment (next element in $vpSegments array)
142 if ($vpSegments[$i][0] === '(') {
143 $paramName = str_replace(['(', ')'], '', $vpSegments[$i]);
144 // A value is present (e.g. /(foo)/bar)
145 if (isset($vpSegments[$i + 1])) {
146 $viewParameters[$paramName] = $vpSegments[$i + 1];
147 unset($vpSegments[$i + 1]);
148 } else {
149 // No value (e.g. /(foo)) => set it to empty string
150 $viewParameters[$paramName] = '';
151 }
152 } elseif (isset($paramName)) {
153 // Orphan segment (no previous parameter name), e.g. /(foo)/bar/baz
154 // Add it to the previous parameter.
155 $viewParameters[$paramName] .= '/' . $vpSegments[$i];
156 }
157 }
158
159 return $viewParameters;
160 }
161 }
eZ\Bundle\EzPublishCoreBundle\EventListener\SiteAccessListener
SiteAccess match listener.
Definition: SiteAccessListener.php:23
eZ\Bundle\EzPublishCoreBundle\EventListener\SiteAccessListener\__construct
__construct(RouterInterface $defaultRouter, UrlAliasGenerator $urlAliasGenerator, HttpUtils $httpUtils)
Definition: SiteAccessListener.php:35
eZ\Bundle\EzPublishCoreBundle\EventListener\SiteAccessListener\generateViewParametersArray
generateViewParametersArray($vpString)
Generates the view parameters array from the view parameters string.
Definition: SiteAccessListener.php:129
eZ\Bundle\EzPublishCoreBundle\EventListener\SiteAccessListener\$httpUtils
$httpUtils
Definition: SiteAccessListener.php:33
eZ\Bundle\EzPublishCoreBundle\EventListener\SiteAccessListener\$defaultRouter
$defaultRouter
Definition: SiteAccessListener.php:27
eZ\Bundle\EzPublishCoreBundle\EventListener\SiteAccessListener\onSiteAccessMatch
onSiteAccessMatch(PostSiteAccessMatchEvent $event)
Definition: SiteAccessListener.php:49
eZ\Bundle\EzPublishCoreBundle\EventListener\SiteAccessListener\getViewParameters
getViewParameters($pathinfo)
Extracts view parameters from $pathinfo.
Definition: SiteAccessListener.php:103
eZ\Bundle\EzPublishCoreBundle\EventListener\SiteAccessListener\$urlAliasGenerator
$urlAliasGenerator
Definition: SiteAccessListener.php:30
eZ\Bundle\EzPublishCoreBundle\EventListener\SiteAccessListener\getSubscribedEvents
static getSubscribedEvents()
Definition: SiteAccessListener.php:42
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\Event\PostSiteAccessMatchEvent\getSiteAccess
getSiteAccess()
Returns matched SiteAccess instance.
Definition: PostSiteAccessMatchEvent.php:52
eZ\Publish\Core\MVC\Symfony\Event\PostSiteAccessMatchEvent\getRequest
getRequest()
Definition: PostSiteAccessMatchEvent.php:42
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\Generator\UrlAliasGenerator
URL generator for UrlAlias based links.
Definition: UrlAliasGenerator.php:21
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+.