Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The "session.storage.factory.service" service is deprecated,

Tags:

php

symfony

I have updated symfony to 5.3 and get deprecations logs like

User Deprecated: Since symfony/framework-bundle 5.3: The session.storage.factory.service service is deprecated, use session.storage.factory.native, session.storage.factory.php_bridge or session.storage.factory.mock_file instead.

I think it is caused by using TokenStrageInterface::getToken() but I cannot find the solution to solve it.

The code I use is like this.

<?php


namespace App\EventSubscriber;


use App\Entity\User;
use Gedmo\Loggable\LoggableListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;

class DoctrineExtensionSubscriber implements EventSubscriberInterface
{
    /**
     * @var LoggableListener
     */
    private LoggableListener $loggableListener;

    /**
     * @var TokenStorageInterface
     */
    private TokenStorageInterface $tokenStorage;

    public function __construct(LoggableListener      $loggableListener,
                                TokenStorageInterface $tokenStorage
    )
    {
        $this->loggableListener = $loggableListener;
        $this->tokenStorage = $tokenStorage;
    }


    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::CONTROLLER => [
                'onKernelController',
                -10,
            ],
        ];
    }

    public function onKernelController(ControllerEvent $event): void
    {
        if (!$event->isMainRequest()) {
            return;
        }
        if ($this->tokenStorage?->getToken()?->isAuthenticated() === true) {
            $user = $this->tokenStorage->getToken()->getUser();

            $controller = $event->getController();

            if (is_array($event->getController())) {
                $controller = $event->getController()[0];
            }

            if ($user instanceof User) {
                $this->loggableListener->setUsername($user->getFullName());
                return;
            }
            $this->loggableListener->setUsername('Anonymous');
        }
    }
}

I've thought to edit config file, but I can't determine where to change. Please see the following code for config/packages/framework.yaml:

# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
  secret: '%env(APP_SECRET)%'
  #csrf_protection: true
  #http_method_override: true

  # Enables session support. Note that the session will ONLY be started if you read or write from it.
  # Remove or comment this section to explicitly disable session support.
  session:
    #handler_id: null
    cookie_secure: auto
    cookie_samesite: lax
    save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'

  #esi: true
  #fragments: true
  php_errors:
    log: true
like image 732
Satoshi Avatar asked Aug 24 '21 15:08

Satoshi


Video Answer


1 Answers

Your framework.yaml session section should look like:

    session:
        handler_id: null
        cookie_secure: auto
        cookie_samesite: lax
        storage_factory_id: session.storage.factory.native

This is the default config you get with a new 5.3 project. You can leave the save_path entry if you want. The storage_value_id was introduced in 5.3.

According to the storage_factory_id docs, the default value should already be factory.native. Which implies that you don't actually need the entry at all.

However, bin/console debug:config framework session shows different results if you leave it out. Not sure it that is an error or not.

In any event, add the storage_factory_id and the error should go away.

like image 130
Cerad Avatar answered Nov 04 '22 19:11

Cerad