如何处理 Symfony 5.3 弃用?

How to handle Symfony 5.3 deprecations?

在将 Symfony 从 4.4 -> 5.3 升级后,我遇到了一些无法解决的问题。

这里有 3 个弃用示例:

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.
User Deprecated: Since symfony/security-core 5.3: The "Symfony\Component\Security\Core\Event\AuthenticationFailureEvent" class is deprecated, use "Symfony\Component\Security\Http\Event\LoginFailureEvent"
User Deprecated: Since symfony/http-kernel 5.3: "Symfony\Component\HttpKernel\Event\KernelEvent::isMasterRequest()" is deprecated, use "isMainRequest()" instead.

30 次弃用中只有 3 次弃用。我的问题是:如何解决此警告的正确方法。 (我需要解决,因为我有很多 Requests/minute 并且每次在 dev.log 中写入此警告 - 这个文件正在爆炸)

我想从第三个警告开始。这是踪迹:

../vendor/symfony/http-kernel/Event/KernelEvent.php:88 {▶}
../vendor/symfony/web-link/EventListener/AddLinkHeaderListener.php:41 {▶}
../vendor/symfony/event-dispatcher/Debug/WrappedListener.php:117 {▶}
../vendor/symfony/event-dispatcher/EventDispatcher.php:230 {▶}
../vendor/symfony/event-dispatcher/EventDispatcher.php:59 {▶}
../vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:151 {▶}
../vendor/symfony/http-kernel/HttpKernel.php:190 {▶}
../vendor/symfony/http-kernel/HttpKernel.php:178 {▶}
../vendor/symfony/http-kernel/HttpKernel.php:79 {▶}
../vendor/symfony/http-kernel/EventListener/ErrorListener.php:60 {▶}
../vendor/symfony/event-dispatcher/Debug/WrappedListener.php:117 {▶}
../vendor/symfony/event-dispatcher/EventDispatcher.php:230 {▶}
../vendor/symfony/event-dispatcher/EventDispatcher.php:59 {▶}
../vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:151 {▶}
../vendor/symfony/http-kernel/HttpKernel.php:218 {▶}
../vendor/symfony/http-kernel/HttpKernel.php:90 {▶}
../vendor/symfony/http-kernel/Kernel.php:199 {▶}
../public/index.php:25 {▶}

所有文件直接来自 symfony,与我无关,这就是为什么我的问题是从哪里开始解决这个警告。

好的,现在我已免费弃用,但从 4.4 升级到 5.3 后还有一些事情要做

为了找到所有警告,我创建了一个新的 symfony 项目来比较不同的文件

  1. KernelEvent::isMasterRequest()" is deprecated, use "isMainRequest()

    查看您的文件 src/Kernel.php - 您可以在那里找到已弃用的方法

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

    打开您的 framework.yaml 并与以下代码进行比较。也许它可以帮助你:

    session:
      handler_id: session.handler.native_file
      save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
      # 2 Days lifetime (172800 seconds)
      cookie_lifetime: 172800
      cookie_secure: 'auto'
      cookie_samesite: 'lax'
      #storage_id: session.storage.mock_file
      storage_factory_id: session.storage.factory.native  
    
  3. The "Symfony\Component\HttpFoundation\Session\SessionInterface are deprecated

    我在我的旧 LoginAuthenticator 中找到了这个(默认情况下 'App\Security...')。将其替换为 use Symfony\Component\HttpFoundation\RequestStack; 现在您可以注入它并使其作为 $this->requestStack 可用,然后将所有 $this->session 更改为 $this->requestStack->getSession() 完成!

  4. 大多数其他弃用都与 symfony 5.1 中引入的 new authenticator-based system 有关。你必须编辑你的 security.yaml。我给你看我的改变:

    security:
        enable_authenticator_manager: true
        password_hashers:
           Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
        providers:
            users_in_memory: { memory: null }
            app_user_provider:
               entity:
                 class: App\Entity\User
                 property: username
        firewalls:
            main: 
                # default stuff
                provider: app_user_provider
                http_basic: ~
                form_login:
                    default_target_path: mission 
                    use_referer: true
                    target_path_parameter: redirect_url
                    login_path: login
                    check_path: login
                entry_point: form_login
                access_denied_handler: App\Security\AccessDeniedHandler
                custom_authenticator: App\Security\CustomAuthenticator
    

    为了使身份验证警告更易于理解,请使用制造商包创建一个新的 CustomAuthenticator。现在您可以将旧 Authenticator 与新 Authenticator 进行比较,并将新方法放入旧 Authenticator。您需要进行一些更改。

就是这样。大多数弃用现在应该解决了。