vendor/symfony/http-kernel/EventListener/SessionListener.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\EventListener;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. /**
  15. * Sets the session in the request.
  16. *
  17. * When the passed container contains a "session_storage" entry which
  18. * holds a NativeSessionStorage instance, the "cookie_secure" option
  19. * will be set to true whenever the current main request is secure.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. *
  23. * @final
  24. */
  25. class SessionListener extends AbstractSessionListener
  26. {
  27. public function onKernelRequest(RequestEvent $event)
  28. {
  29. parent::onKernelRequest($event);
  30. if (!$event->isMainRequest() || (!$this->container->has('session') && !$this->container->has('session_factory'))) {
  31. return;
  32. }
  33. if ($this->container->has('session_storage')
  34. && ($storage = $this->container->get('session_storage')) instanceof NativeSessionStorage
  35. && ($mainRequest = $this->container->get('request_stack')->getMainRequest())
  36. && $mainRequest->isSecure()
  37. ) {
  38. $storage->setOptions(['cookie_secure' => true]);
  39. }
  40. }
  41. protected function getSession(): ?SessionInterface
  42. {
  43. if ($this->container->has('session')) {
  44. return $this->container->get('session');
  45. }
  46. if ($this->container->has('session_factory')) {
  47. return $this->container->get('session_factory')->createSession();
  48. }
  49. return null;
  50. }
  51. }