8000 Deprecat service "session" · symfony/symfony@b8d9d91 · GitHub
[go: up one dir, main page]

Skip to content

Commit b8d9d91

Browse files
committed
Deprecat service "session"
1 parent 2941951 commit b8d9d91

File tree

37 files changed

+428
-77
lines changed
  • app/Session
  • SecurityBundle
  • Component
  • 37 files changed

    +428
    -77
    lines changed

    UPGRADE-5.3.md

    Lines changed: 6 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -23,6 +23,11 @@ Form
    2323
    * Deprecated passing an array as the second argument of the `RadioListMapper::mapDataToForms()` method, pass `\Traversable` instead.
    2424
    * Deprecated passing an array as the first argument of the `RadioListMapper::mapFormsToData()` method, pass `< 1E01 /span>\Traversable` instead.
    2525

    26+
    FrameworkBundle
    27+
    ---------------
    28+
    29+
    * Deprecate the `session` service and the `SessionInterface` alias, use the `\Symfony\Component\HttpFoundation\Request::getSession()` or the new `\Symfony\Component\HttpFoundation\RequestStack::getSession()` methods instead
    30+
    2631
    HttpFoundation
    2732
    --------------
    2833

    @@ -57,6 +62,7 @@ Security
    5762
    --------
    5863

    5964
    * Deprecated voters that do not return a valid decision when calling the `vote` method.
    65+
    * The `$session` constructor argument of `SessionTokenStorage` has been deprecated and replaced by the `$requestStack` one which expects a `\Symfony\Component\HttpFoundation\RequestStack`
    6066

    6167
    Serializer
    6268
    ----------

    UPGRADE-6.0.md

    Lines changed: 2 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -75,6 +75,7 @@ FrameworkBundle
    7575
    * The `form.factory`, `form.type.file`, `translator`, `security.csrf.token_manager`, `serializer`,
    7676
    `cache_clearer`, `filesystem` and `validator` services are now private.
    7777
    * Removed the `lock.RESOURCE_NAME` and `lock.RESOURCE_NAME.store` services and the `lock`, `LockInterface`, `lock.store` and `PersistingStoreInterface` aliases, use `lock.RESOURCE_NAME.factory`, `lock.factory` or `LockFactory` instead.
    78+
    * Remove the `session` service and the `SessionInterface` alias, use the `\Symfony\Component\HttpFoundation\Request::getSession()` or the new `\Symfony\Component\HttpFoundation\RequestStack::getSession()` methods instead
    7879

    7980
    HttpFoundation
    8081
    --------------
    @@ -174,6 +175,7 @@ Security
    174175
    `DefaultAuthenticationSuccessHandler`.
    175176
    * Removed the `AbstractRememberMeServices::$providerKey` property in favor of `AbstractRememberMeServices::$firewallName`
    176177
    * `AccessDecisionManager` now throw an exception when a voter does not return a valid decision.
    178+
    * The `$session` constructor argument of `SessionTokenStorage` has been replaced by the `$requestStack` one which expects a `\Symfony\Component\HttpFoundation\RequestStack` instead
    177179

    178180
    Serializer
    179181
    ----------

    src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

    Lines changed: 1 addition & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -24,6 +24,7 @@ CHANGELOG
    2424
    * added `assertFormValue()` and `assertNoFormValue()` in `WebTestCase`
    2525
    * Added "--as-tree=3" option to `translation:update` command to dump messages as a tree-like structure. The given value defines the level where to switch to inline YAML
    2626
    * Deprecated the `lock.RESOURCE_NAME` and `lock.RESOURCE_NAME.store` services and the `lock`, `LockInterface`, `lock.store` and `PersistingStoreInterface` aliases, use `lock.RESOURCE_NAME.factory`, `lock.factory` or `LockFactory` instead.
    27+
    * Deprecate the `session` service and the `SessionInterface` alias, use the `Request::getSession()` or the new `RequestStack::getSession()` methods instead
    2728

    2829
    5.1.0
    2930
    -----

    src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php

    Lines changed: 5 additions & 4 deletions
    Original file line numberDiff line numberDiff line change
    @@ -22,6 +22,7 @@
    2222
    use Symfony\Component\Form\FormFactoryInterface;
    2323
    use Symfony\Component\Form\FormInterface;
    2424
    use Symfony\Component\HttpFoundation\BinaryFileResponse;
    25+
    use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
    2526
    use Symfony\Component\HttpFoundation\JsonResponse;
    2627
    use Symfony\Component\HttpFoundation\RedirectResponse;
    2728
    use Symfony\Component\HttpFoundation\Request;
    @@ -200,11 +201,11 @@ protected function file($file, string $fileName = null, string $disposition = Re
    200201
    */
    201202
    protected function addFlash(string $type, $message): void
    202203
    {
    203-
    if (!$this->container->has('session')) {
    204-
    throw new \LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".');
    204+
    try {
    205+
    $this->container->get('request_stack')->getSession()->getFlashBag()->add($type, $message);
    206+
    } catch (SessionNotFoundException $e) {
    207+
    throw new \LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".', 0, $e);
    205208
    }
    206-
    207-
    $this->container->get('session')->getFlashBag()->add($type, $message);
    208209
    }
    209210

    210211
    /**

    src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SessionPass.php

    Lines changed: 23 additions & 3 deletions
    Original file line numberDiff line numberDiff line change
    @@ -22,21 +22,41 @@ class SessionPass implements CompilerPassInterface
    2222
    {
    2323
    public function process(ContainerBuilder $container)
    2424
    {
    25-
    if (!$container->hasDefinition('session')) {
    25+
    if (!$container->has('session.storage')) {
    2626
    return;
    2727
    }
    2828

    29+
    // BC layer: Make "session" an alias of ".session.do-not-use" when not overriden by the user
    30+
    if (!$container->has('session')) {
    31+
    $alias = $container->setAlias('session', '.session.do-not-use');
    32+
    $alias->setDeprecated('symfony/framework-bundle', '5.3', 'The "%alias_id%" service is deprecated, use "$requestStack->getSession()" instead.');
    33+
    34+
    return;
    35+
    }
    36+
    37+
    if ($container->hasDefinition('session')) {
    38+
    $definition = $container->getDefinition('session');
    39+
    $definition->setDeprecated('symfony/framework-bundle', '5.3', 'The "%service_id%" service is deprecated, use "$requestStack->getSession()" instead.');
    40+
    } else {
    41+
    $alias = $container->getAlias('session');
    42+
    $alias->setDeprecated('symfony/framework-bundle', '5.3', 'The "%alias_id%" alias is deprecated, use "$requestStack->getSession()" instead.');
    43+
    $definition = $container->findDefinition('session');
    44+
    }
    45+
    46+
    // Convert internal service `.session.do-not-use` into alias of `session`.
    47+
    $container->setAlias('.session.do-not-use', 'session');
    48+
    2949
    $bags = [
    3050
    'session.flash_bag' => $container->hasDefinition('session.flash_bag') ? $container->getDefinition('session.flash_bag') : null,
    3151
    'session.attribute_bag' => $container->hasDefinition('session.attribute_bag') ? $container->getDefinition('session.attribute_bag') : null,
    3252
    ];
    3353

    34-
    foreach ($container->getDefinition('session')->getArguments() as $v) {
    54+
    foreach ($definition->getArguments() as $v) {
    3555
    if (!$v instanceof Reference || !isset($bags[$bag = (string) $v]) || !\is_array($factory = $bags[$bag]->getFactory())) {
    3656
    continue;
    3757
    }
    3858

    39-
    if ([0, 1] !== array_keys($factory) || !$factory[0] instanceof Reference || 'session' !== (string) $factory[0]) {
    59+
    if ([0, 1] !== array_keys($factory) || !$factory[0] instanceof Reference || !\in_array((string) $factory[0], ['session', '.session.do-not-use'], true)) {
    4060
    continue;
    4161
    }
    4262

    src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php

    Lines changed: 2 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -18,6 +18,7 @@
    1818
    use Symfony\Component\DependencyInjection\ContainerInterface;
    1919
    use Symfony\Component\HttpFoundation\Request;
    2020
    use Symfony\Component\HttpFoundation\Response;
    21+
    use Symfony\Component\HttpFoundation\Session\Session;
    2122
    use Symfony\Component\HttpKernel\HttpKernelBrowser;
    2223
    use Symfony\Component\HttpKernel\KernelInterface;
    2324
    use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
    @@ -122,7 +123,7 @@ public function loginUser($user, string $firewallContext = 'main'): self
    122123

    123124
    $token = new TestBrowserToken($user->getRoles(), $user);
    124125
    $token->setAuthenticated(true);
    125-
    $session = $this->getContainer()->get('session');
    126+
    $session = new Session($this->getContainer()->get('test.service_container')->get('session.storage'));
    126127
    $session->set('_security_'.$firewallContext, serialize($token));
    127128
    $session->save();
    128129

    src/Symfony/Bundle/FrameworkBundle/Resources/config/security_csrf.php

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -27,7 +27,7 @@
    2727
    ->alias(TokenGeneratorInterface::class, 'security.csrf.token_generator')
    2828

    2929
    ->set('security.csrf.token_storage', SessionTokenStorage::class)
    30-
    ->args([service('session')])
    30+
    ->args([service('request_stack')])
    3131

    3232
    ->alias(TokenStorageInterface::class, 'security.csrf.token_storage')
    3333

    src/Symfony/Bundle/FrameworkBundle/Resources/config/session.php

    Lines changed: 10 additions & 7 deletions
    Original file line numberDiff line numberDiff line change
    @@ -11,6 +11,7 @@
    1111

    1212
    namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    1313

    14+
    use Symfony\Bundle\FrameworkBundle\Session\DeprecatedSessionFactory;
    1415
    use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
    1516
    use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
    1617
    use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
    @@ -33,15 +34,17 @@
    3334
    $container->parameters()->set('session.metadata.storage_key', '_sf2_meta');
    3435

    3536
    $container->services()
    36-
    ->set('session', Session::class)
    37-
    ->public()
    37+
    ->set('.session.do-not-use', Session::class) // to be removed in 6.0
    3838
    ->args([
    3939
    service('session.storage'),
    4040
    null, // AttributeBagInterface
    4141
    null, // FlashBagInterface
    4242
    [service('session_listener'), 'onSessionUsage'],
    4343
    ])
    44-
    ->alias(SessionInterface::class, 'session')
    44+
    ->set('.session.deprecated', SessionInterface::class) // to be removed in 6.0
    45+
    ->factory([inline_service(DeprecatedSessionFactory::class)->args([service('request_stack')]), 'getSession'])
    46+
    ->alias(SessionInterface::class, '.session.do-not-use')
    47+
    ->deprecate('symfony/framework-bundle', '5.3', 'The "%alias_id%" alias is deprecated, use "$requestStack->getSession()" instead.')
    4548
    ->alias(SessionStorageInterface::class, 'session.storage')
    4649
    ->alias(\SessionHandlerInterface::class, 'session.handler')
    4750

    @@ -65,12 +68,12 @@
    6568
    ])
    6669

    6770
    ->set('session.flash_bag', FlashBag::class)
    68-
    ->factory([service('session'), 'getFlashBag'])
    71+
    ->factory([service('.session.do-not-use'), 'getFlashBag'])
    6972
    ->deprecate('symfony/framework-bundle', '5.1', 'The "%service_id%" service is deprecated, use "$session->getFlashBag()" instead.')
    7073
    ->alias(FlashBagInterface::class, 'session.flash_bag')
    7174

    7275
    ->set('session.attribute_bag', AttributeBag::class)
    73-
    ->factory([service('session'), 'getBag'])
    76+
    ->factory([service('.session.do-not-use'), 'getBag'])
    7477
    ->args(['attributes'])
    7578
    ->deprecate('symfony/framework-bundle', '5.1', 'The "%service_id%" service is deprecated, use "$session->getAttributeBag()" instead.')
    7679

    @@ -94,8 +97,8 @@
    9497
    ->set('session_listener', SessionListener::class)
    9598
    ->args([
    9699
    service_locator([
    97-
    'session' => service('session')->ignoreOnInvalid(),
    98-
    'initialized_session' => service('session')->ignoreOnUninitialized(),
    100+
    'session' => service('.session.do-not-use')->ignoreOnInvalid(),
    101+
    'initialized_session' => service('.session.do-not-use')->ignoreOnUninitialized(),
    99102
    'logger' => service('logger')->ignoreOnInvalid(),
    100103
    'session_collector' => service('data_collector.request.session_collector')->ignoreOnInvalid(),
    101104
    ]),

    src/Symfony/Bundle/FrameworkBundle/Resources/config/test.php

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -38,7 +38,7 @@
    3838
    ->set('test.session.listener', TestSessionListener::class)
    3939
    ->args([
    4040
    service_locator([
    41-
    'session' => service('session')->ignoreOnInvalid(),
    41+
    'session' => service('.session.do-not-use')->ignoreOnInvalid(),
    4242
    ]),
    4343
    ])
    4444
    ->tag('kernel.event_subscriber')
    Lines changed: 46 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,46 @@
    1+
    <?php
    2+
    3+
    /*
    4+
    * This file is part of the Symfony package.
    5+
    *
    6+
    * (c) Fabien Potencier <fabien@symfony.com>
    7+
    *
    8+
    * For the full copyright and license information, please view the LICENSE
    9+
    * file that was distributed with this source code.
    10+
    */
    11+
    12+
    namespace Symfony\Bundle\FrameworkBundle\Session;
    13+
    14+
    use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
    15+
    use Symfony\Component\HttpFoundation\RequestStack;
    16+
    use Symfony\Component\HttpFoundation\Session\SessionInterface;
    17+
    18+
    /**
    19+
    * Provides session and trigger deprecation.
    20+
    *
    21+
    * Used by service that should trigger deprecation when accessed by the user.
    22+
    *
    23+
    * @author Jérémy Derussé <jeremy@derusse.com>
    24+
    *
    25+
    * @internal to be removed in 6.0
    26+
    */
    27+
    class DeprecatedSessionFactory
    28+
    {
    29+
    private $requestStack;
    30+
    31+
    public function __construct(RequestStack $requestStack)
    32+
    {
    33+
    $this->requestStack = $requestStack;
    34+
    }
    35+
    36+
    public function getSession(): ?SessionInterface
    37+
    {
    38+
    trigger_deprecation('symfony/framework-bundle', '5.3', 'The "session" service is deprecated, use "$requestStack->getSession()" instead.');
    39+
    40+
    try {
    41+
    return $this->requestStack->getSession();
    42+
    } catch (SessionNotFoundException $e) {
    43+
    return null;
    44+
    }
    45+
    }
    46+
    }

    0 commit comments

    Comments
     (0)
    0