vendor/se7enxweb/legacy-bridge/bundle/LegacyMapper/Session.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. namespace eZ\Bundle\EzPublishLegacyBundle\LegacyMapper;
  7. use eZ\Publish\Core\MVC\Legacy\Event\PreBuildKernelEvent;
  8. use eZ\Publish\Core\MVC\Legacy\LegacyEvents;
  9. use eZ\Publish\Core\MVC\Symfony\RequestStackAware;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
  13. /**
  14. * Maps the session parameters to the legacy parameters.
  15. */
  16. class Session implements EventSubscriberInterface
  17. {
  18. use RequestStackAware;
  19. /**
  20. * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
  21. */
  22. private $session;
  23. /**
  24. * @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface|null
  25. */
  26. private $sessionStorage;
  27. /**
  28. * @var string
  29. */
  30. private $sessionStorageKey;
  31. public function __construct(?SessionStorageInterface $sessionStorage = null, $sessionStorageKey = null, SessionInterface $session = null)
  32. {
  33. $this->sessionStorage = $sessionStorage;
  34. $this->sessionStorageKey = $sessionStorageKey;
  35. $this->session = $session;
  36. }
  37. public static function getSubscribedEvents()
  38. {
  39. return [
  40. LegacyEvents::PRE_BUILD_LEGACY_KERNEL => ['onBuildKernelHandler', 128],
  41. ];
  42. }
  43. /**
  44. * Adds the session settings to the parameters that will be injected
  45. * into the legacy kernel.
  46. *
  47. * @param \eZ\Publish\Core\MVC\Legacy\Event\PreBuildKernelEvent $event
  48. */
  49. public function onBuildKernelHandler(PreBuildKernelEvent $event)
  50. {
  51. $sessionInfos = [
  52. 'configured' => false,
  53. 'started' => false,
  54. 'name' => false,
  55. 'namespace' => false,
  56. 'has_previous' => false,
  57. 'storage' => false,
  58. ];
  59. $request = $this->getCurrentRequest();
  60. // In Symfony 5.3+, the session service is request-scoped and may be null when
  61. // injected via @?session. Additionally, PRE_BUILD_LEGACY_KERNEL fires before
  62. // AbstractSessionListener sets the session on the request, so $request->hasSession()
  63. // can also be false. Use $this->sessionStorage (always injected) directly and
  64. // derive name/started from PHP native session state.
  65. if ($this->sessionStorage !== null) {
  66. // Try to get a session object for name/started; fall back to PHP native functions.
  67. $session = $this->session ?? ($request !== null && $request->hasSession() ? $request->getSession() : null);
  68. $sessionInfos['configured'] = true;
  69. $sessionInfos['name'] = $session !== null ? $session->getName() : session_name();
  70. $sessionInfos['started'] = $session !== null ? $session->isStarted() : (session_status() === PHP_SESSION_ACTIVE);
  71. $sessionInfos['namespace'] = $this->sessionStorageKey;
  72. $sessionInfos['has_previous'] = $request !== null ? $request->hasPreviousSession() : false;
  73. $sessionInfos['storage'] = $this->sessionStorage;
  74. }
  75. $legacyKernelParameters = $event->getParameters();
  76. $legacyKernelParameters->set('session', $sessionInfos);
  77. // Deactivate session cookie settings in legacy kernel.
  78. // This will force using settings defined in Symfony.
  79. $sessionSettings = [
  80. 'site.ini/Session/CookieTimeout' => false,
  81. 'site.ini/Session/CookiePath' => false,
  82. 'site.ini/Session/CookieDomain' => false,
  83. 'site.ini/Session/CookieSecure' => false,
  84. 'site.ini/Session/CookieHttponly' => false,
  85. ];
  86. $legacyKernelParameters->set(
  87. 'injected-settings',
  88. $sessionSettings + (array)$legacyKernelParameters->get('injected-settings')
  89. );
  90. }
  91. }