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

SessionSetDynamicNameListener.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\MVCEvents;
11 use eZ\Publish\Core\MVC\Symfony\Event\PostSiteAccessMatchEvent;
12 use eZ\Publish\Core\MVC\Symfony\SiteAccess;
13 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14 use Symfony\Component\HttpFoundation\Session\SessionInterface;
15 use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
16 use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
17 use Symfony\Component\HttpKernel\HttpKernelInterface;
18
24 class SessionSetDynamicNameListener implements EventSubscriberInterface
25 {
26 const MARKER = '{siteaccess_hash}';
27
31 const SESSION_NAME_PREFIX = 'eZSESSID';
32
34 private $configResolver;
35
37 private $session;
38
40 private $sessionStorage;
41
47 public function __construct(ConfigResolverInterface $configResolver, SessionInterface $session = null, SessionStorageInterface $sessionStorage = null)
48 {
49 $this->configResolver = $configResolver;
50 $this->session = $session;
51 $this->sessionStorage = $sessionStorage;
52 }
53
54 public static function getSubscribedEvents()
55 {
56 return [
57 MVCEvents::SITEACCESS => ['onSiteAccessMatch', 250],
58 ];
59 }
60
61 public function onSiteAccessMatch(PostSiteAccessMatchEvent $event)
62 {
63 if (
64 !(
65 $event->getRequestType() === HttpKernelInterface::MASTER_REQUEST
66 && isset($this->session)
67 && !$this->session->isStarted()
68 && $this->sessionStorage instanceof NativeSessionStorage
69 )
70 ) {
71 return;
72 }
73
74 $sessionOptions = (array)$this->configResolver->getParameter('session');
75 $sessionName = isset($sessionOptions['name']) ? $sessionOptions['name'] : $this->session->getName();
76 $sessionOptions['name'] = $this->getSessionName($sessionName, $event->getSiteAccess());
77 $this->sessionStorage->setOptions($sessionOptions);
78 }
79
86 private function getSessionName($sessionName, SiteAccess $siteAccess)
87 {
88 // Add session prefix if needed.
89 if (strpos($sessionName, static::SESSION_NAME_PREFIX) !== 0) {
90 $sessionName = static::SESSION_NAME_PREFIX . '_' . $sessionName;
91 }
92
93 // Check if uniqueness marker is present. If so, session name will be unique for current siteaccess.
94 if (strpos($sessionName, self::MARKER) !== false) {
95 $sessionName = str_replace(self::MARKER, md5($siteAccess->name), $sessionName);
96 }
97
98 return $sessionName;
99 }
100 }
eZ\Bundle\EzPublishCoreBundle\EventListener\SessionSetDynamicNameListener
SiteAccess match listener.
Definition: SessionSetDynamicNameListener.php:25
eZ\Bundle\EzPublishCoreBundle\EventListener\SessionSetDynamicNameListener\onSiteAccessMatch
onSiteAccessMatch(PostSiteAccessMatchEvent $event)
Definition: SessionSetDynamicNameListener.php:61
eZ\Bundle\EzPublishCoreBundle\EventListener\SessionSetDynamicNameListener\MARKER
const MARKER
Definition: SessionSetDynamicNameListener.php:26
eZ\Bundle\EzPublishCoreBundle\EventListener\SessionSetDynamicNameListener\getSubscribedEvents
static getSubscribedEvents()
Definition: SessionSetDynamicNameListener.php:54
eZ\Bundle\EzPublishCoreBundle\EventListener\SessionSetDynamicNameListener\$sessionStorage
$sessionStorage
Definition: SessionSetDynamicNameListener.php:40
eZ\Bundle\EzPublishCoreBundle\EventListener\SessionSetDynamicNameListener\$session
$session
Definition: SessionSetDynamicNameListener.php:37
eZ\Bundle\EzPublishCoreBundle\EventListener\SessionSetDynamicNameListener\__construct
__construct(ConfigResolverInterface $configResolver, SessionInterface $session=null, SessionStorageInterface $sessionStorage=null)
Definition: SessionSetDynamicNameListener.php:47
eZ\Bundle\EzPublishCoreBundle\EventListener\SessionSetDynamicNameListener\SESSION_NAME_PREFIX
const SESSION_NAME_PREFIX
Prefix for session name.
Definition: SessionSetDynamicNameListener.php:31
eZ\Bundle\EzPublishCoreBundle\EventListener\SessionSetDynamicNameListener\getSessionName
getSessionName($sessionName, SiteAccess $siteAccess)
Definition: SessionSetDynamicNameListener.php:86
eZ\Bundle\EzPublishCoreBundle\EventListener\SessionSetDynamicNameListener\$configResolver
$configResolver
Definition: SessionSetDynamicNameListener.php:34
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\getRequestType
getRequestType()
Returns the request type the kernel is currently processing.
Definition: PostSiteAccessMatchEvent.php:63
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\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\Bundle\EzPublishCoreBundle\EventListener
Definition: BackgroundIndexingTerminateListener.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.