编译器通过 - 解析目标实体不加载

Compiler pass - resolve target entity doesn't load

我正在处理一个包,我需要从配置参数加载一个学说resolve_target_entities。

应该是我的解决方案,事实是使用捆绑包似乎无法加载 "compiler pass class".

这是我的捆绑包class

class PersonalBundle extends Bundle
{
    public function build(ContainerBuilder $container){
        parent::build($container);
        $container->addCompilerPass(new ResolveTargetEntitiesPass());
    }
}

这是 ResolveTargetEntitiesPass class

class ResolveTargetEntitiesPass implements CompilerPassInterface
{

    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container)
    {
        // Gets the custom entity defined by the user (or the default one)
        $customEntityClass = $container->getParameter('personal.custom_class');
        // Skip the resolve_target_entities part if user has not defined a different entity
        if (DefaultClassInterface::DEFAULT_ENTITY_CLASS == $customEntityClass) {
            return;
        }
        // Throws exception if the class isn't found
        if (!class_exists($customEntityClass)) {
            throw new ClassNotFoundException(sprintf("Can't find class %s ", $customEntityClass));
        }

        // Get the doctrine ResolveTargetEntityListener
        $def = $container->findDefinition('doctrine.orm.listeners.resolve_target_entity');
        // Adds the resolve_target_enitity parameter
        $def->addMethodCall('addResolveTargetEntity', array(
            DefaultClassInterface::DEFAULT_ENTITY_CLASS, $customEntityClass, array()
        ));
        // This was added due this problem
        // 
        if (version_compare(Version::VERSION, '2.5.0-DEV') < 0 && !$def->hasTag('doctrine.event_listener')) {
            $def->addTag('doctrine.event_listener', array('event' => 'loadClassMetadata'));
        } elseif (!$def->hasTag('doctrine.event_subscriber')) {
            $def->addTag('doctrine.event_subscriber');
        }
    }
}

当我使用 class 时会引发此错误

Expected value of type "PersonalBundle\Entity\DefaultClass" for association field "PersonalBundle\Entity\Group#$defaultClass", got "App\Entity\CustomClass" instead.

正如我所说,似乎无法加载 ResolveTargetEntitiesPass... 谢谢

所以我解决了更改编译器传递优先级的问题。 我尝试将捆绑包移到 config/bundle.php 中的顶部,它开始工作,然后在 https://symfony.com/blog/new-in-symfony-3-2-compiler-passes-improvements 之后,我保留了默认类型,但增加了优先级(从默认值 0 到 1)。 我不确定哪个服务是 "downgraded",所以如果有人有想法,欢迎提出来。

<?php
// ...
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

class PersonalBundle extends Bundle
{
    public function build(ContainerBuilder $container){
        parent::build($container);
        $container->addCompilerPass(new ResolveTargetEntitiesPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1);
    }
}