Skip to content

Restore SessionListener class for backward compatibility with Silex #22171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

namespace Symfony\Bundle\FrameworkBundle\EventListener;

use Symfony\Component\HttpKernel\EventListener\SessionListener as BaseSessionListener;
use Symfony\Component\HttpKernel\EventListener\ContainerAwareSessionListener;

@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use %s instead.', SessionListener::class, BaseSessionListener::class), E_USER_DEPRECATED);
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use %s instead.', ContainerAwareSessionListener::class, BaseSessionListener::class), E_USER_DEPRECATED);

/**
* Sets the session in the request.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated since version 3.3, to be removed in 4.0. Use {@link BaseSessionListener} instead
* @deprecated since version 3.3, to be removed in 4.0. Use {@link ContainerAwareSessionListener} instead
*/
class SessionListener extends BaseSessionListener
class SessionListener extends ContainerAwareSessionListener
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,15 @@

@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use Symfony\Component\HttpKernel\EventListener\TestSessionListener instead.', TestSessionListener::class), E_USER_DEPRECATED);

use Symfony\Component\HttpKernel\EventListener\AbstractTestSessionListener;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\EventListener\ContainerAwareTestSessionListener;

/**
* TestSessionListener.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated since version 3.3, to be removed in 4.0.
* @deprecated since version 3.3, to be removed in 4.0. Use {@link ContainerAwareTestSessionListener} instead
*/
class TestSessionListener extends AbstractTestSessionListener
class TestSessionListener extends ContainerAwareTestSessionListener
{
protected $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

protected function getSession()
{
if (!$this->container->has('session')) {
return;
}

return $this->container->get('session');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

<service id="session.handler.write_check" class="Symfony\Component\HttpFoundation\Session\Storage\Handler\WriteCheckSessionHandler" public="false" />

<service id="session_listener" class="Symfony\Component\HttpKernel\EventListener\SessionListener">
<service id="session_listener" class="Symfony\Component\HttpKernel\EventListener\ContainerAwareSessionListener">
<tag name="kernel.event_subscriber" />
<tag name="container.service_subscriber" id="session" />
<argument type="service" id="container" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<service id="test.client.cookiejar" class="Symfony\Component\BrowserKit\CookieJar" shared="false" />

<service id="test.session.listener" class="Symfony\Component\HttpKernel\EventListener\TestSessionListener">
<service id="test.session.listener" class="Symfony\Component\HttpKernel\EventListener\ContainerAwareTestSessionListener">
<tag name="kernel.event_subscriber" />
<tag name="container.service_subscriber" id="session" />
<argument type="service" id="container" />
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\EventListener;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
* Sets the session in the request.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @final since version 3.3
*/
class ContainerAwareSessionListener extends SessionListener implements ServiceSubscriberInterface
{
private $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

protected function getSession()
{
if (!$this->container->has('session')) {
return;
}

return $this->container->get('session');
}

/**
* {@inheritdoc}
*/
public static function getSubscribedServices()
{
return array(
'session' => '?'.SessionInterface::class,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\EventListener;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
* Sets the session in the request.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @final since version 3.3
*/
class ContainerAwareTestSessionListener extends TestSessionListener implements ServiceSubscriberInterface
{
private $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

protected function getSession()
{
if (!$this->container->has('session')) {
return;
}

return $this->container->get('session');
}

/**
* {@inheritdoc}
*/
public static function getSubscribedServices()
{
return array(
'session' => '?'.SessionInterface::class,
);
}
}
42 changes: 22 additions & 20 deletions src/Symfony/Component/HttpKernel/EventListener/SessionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,44 @@

namespace Symfony\Component\HttpKernel\EventListener;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Sets the session in the request.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @final since version 3.3
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class SessionListener extends AbstractSessionListener implements ServiceSubscriberInterface
abstract class SessionListener implements EventSubscriberInterface
{
private $container;

public function __construct(ContainerInterface $container)
public function onKernelRequest(GetResponseEvent $event)
{
$this->container = $container;
}
if (!$event->isMasterRequest()) {
return;
}

protected function getSession()
{
if (!$this->container->has('session')) {
$request = $event->getRequest();
$session = $this->getSession();
if (null === $session || $request->hasSession()) {
return;
}

return $this->container->get('session');
$request->setSession($session);
}

/**
* {@inheritdoc}
*/
public static function getSubscribedServices()
public static function getSubscribedEvents()
{
return array(
'session' => '?'.SessionInterface::class,
KernelEvents::REQUEST => array('onKernelRequest', 128),
);
}

/**
* Gets the session object.
*
* @return SessionInterface|null A SessionInterface instance or null if no session is available
*/
abstract protected function getSession();
}
Loading