Symfony 3.4 服务 - Doctrine\ORM\EntityManager 实例,布尔值给定

Symfony 3.4 Service - instance of Doctrine\ORM\EntityManager, boolean given

自从我从 3.0 更新到 Symfony 3.4 后,我目前有一个服务错误。

我有一个 SwitchUserListener,它在其构造中接收了几个对象,但它无法接收 EntityManager,即使我有它的类型提示。我还为我的服务添加了一个 public 密钥

我收到 FatalThrowableError,因为 class 是用布尔值而不是 EntityManager 对象实例化的。

错误信息

Type error: Argument 10 passed to Dbm\UserBundle\Security\SwitchUserListener::__construct() must be an instance of Doctrine\ORM\EntityManager, boolean given, called in C:\wamp64\www\Portail\var\cache\dev\ContainerWyiblvw\getSecurity_Authentication_SwitchuserListener_MainService.php on line 8

FatalThrowableError Line

SwitchUserListener->__construct(object(TokenStorage), object(EmailUserProvider), object(UserChecker), 'main', object(TraceableAccessDecisionManager), object(Logger), '_switch_user', 'ROLE_ALLOWED_TO_SWITCH', object(TraceableEventDispatcher), false, object(AuthorizationChecker)) in var\cache\dev\ContainerWyiblvw\getSecurity_Authentication_SwitchuserListener_MainService.php

这是我现在拥有的。

services.yml

services:
    _defaults:
        public: true
        autowire: true
        autoconfigure: true

    security.authentication.switchuser_listener:
        class: Dbm\UserBundle\Security\SwitchUserListener
        arguments:
            - '@security.token_storage'
            - ~
            - '@security.user_checker'
            - ~
            - '@security.access.decision_manager'
            - '@logger'
            - ''
            - ''
            - '@event_dispatcher'
            - '@doctrine.orm.entity_manager'
            - '@security.authorization_checker'
        tags:
            - { name: monolog.logger, channel: security }

SwitchUserListener.php

namespace Dbm\UserBundle\Security;
...
use Doctrine\ORM\EntityManager;

class SwitchUserListener implements ListenerInterface
{
    private $tokenStorage;
    private $provider;
    private $userChecker;
    private $providerKey;
    private $accessDecisionManager;
    private $usernameParameter;
    private $role;
    private $logger;
    private $dispatcher;
    private $em;
    private $authCheck;

    public function __construct(TokenStorageInterface $tokenStorage,
                                UserProviderInterface $provider,
                                UserCheckerInterface $userChecker,
                                $providerKey,
                                AccessDecisionManagerInterface $accessDecisionManager,
                                LoggerInterface $logger = null,
                                $usernameParameter = '_switch_user',
                                $role = 'ROLE_ALLOWED_TO_SWITCH',
                                EventDispatcherInterface $dispatcher = null,
                                EntityManager $em,
                                AuthorizationChecker $authCheck)
    {
        ...
    }

编辑

终于找到我的错误了。我正在覆盖 switchuser_listener 来自 "Symfony\Bundle\SecurityBundle\Resources\config\securityListeners.xml" 使用我自己的服务,我猜他们列表中的最后一个参数(第 10 个 hehe..heh...)在我更新之前不存在,迫使我也将它放在我的 services.yml 文件中

security_listeners.xml

    <service id="security.authentication.switchuser_listener" class="Symfony\Component\Security\Http\Firewall\SwitchUserListener" abstract="true">
        <tag name="monolog.logger" channel="security" />
        <argument type="service" id="security.token_storage" />
        <argument /> <!-- User Provider -->
        <argument /> <!-- User Checker -->
        <argument /> <!--  Provider Key -->
        <argument type="service" id="security.access.decision_manager" />
        <argument type="service" id="logger" on-invalid="null" />
        <argument>_switch_user</argument>
        <argument>ROLE_ALLOWED_TO_SWITCH</argument>
        <argument type="service" id="event_dispatcher" on-invalid="null"/>
        <argument>false</argument> <!-- Stateless -->
    </service>

下面的文字仍然有效,是问题的正确答案。 但原因就在这次编辑中。

编辑结束

我通过将 autowire 设置为 false 以某种方式解决了这个奇怪的问题,并且我还添加了一个额外的参数,其中 "false" 是(第 10 个参数)。

只有这样做我才能"Skip"这个问题。这样做有点脏。但它有效。

dependencyInjection 发送的参数不正确。 (至少,我是这么认为的)

这是一种解决方法,我不会将其标记为已回答,因此如果有人找到了实际答案,我会将其标记为正确答案

更改如下

services.yml 我只是在第 10 个位置添加了一个额外的空参数

services:
    _defaults:
        public: true
        autowire: true
        autoconfigure: true

    security.authentication.switchuser_listener:
        class: Dbm\UserBundle\Security\SwitchUserListener
        autowire: false
        arguments: 
            - '@security.token_storage'
            - ~
            - '@security.user_checker'
            - ''
            - '@security.access.decision_manager'
            - '@logger'
            - ''
            - ''
            - '@event_dispatcher'
            - ''  
            - '@doctrine.orm.entity_manager'
            - '@security.authorization_checker'
        tags:
            - { name: monolog.logger, channel: security }

SwitchUserListener.php 我只是在第 10 个位置添加了一个额外的参数

public function __construct(TokenStorageInterface $tokenStorage,
                            UserProviderInterface $provider,
                            UserCheckerInterface $userChecker,
                            $providerKey,
                            AccessDecisionManagerInterface $accessDecisionManager,
                            LoggerInterface $logger = null,
                            $usernameParameter = '_switch_user',
                            $role = 'ROLE_ALLOWED_TO_SWITCH',
                            EventDispatcherInterface $dispatcher = null,
                            $buggyParam, //Not doing anything with this
                            EntityManagerInterface $em,
                            AuthorizationChecker $authCheck)
{