覆盖 Dotrine 注释驱动程序

问题描述 投票:0回答:2

我必须在 Doctrine\Common\Persistence\Mapping\Driver\Annotation::getAllClassNames 中进行一些更改,因为 Doctrine 使用 require_one 并且我希望它使用我的自定义 spl 自动加载器。

我的问题是:如何告诉 Doctrine 使用我自己的代码而不更改 Symfony 的供应商文件夹?

php symfony doctrine-orm
2个回答
3
投票

您可以通过将参数

doctrine.orm.metadata.annotation.class
设置为您的parameters.ini/yml/xml 中的类名称来实现此目的。

来自 Doctrine/Bundle/DoctrineBundle/Resources/config/orm.xml:

 <parameter key="doctrine.orm.metadata.annotation.class">Doctrine\ORM\Mapping\Driver\AnnotationDriver</parameter>

我自己没有测试过这一点,但这就是我过去重写此类事情的方式。


0
投票

在我的例子中,我覆盖了 Doctrine 包的 MappingDriver。 现在(2023 年 12 月)改变

doctrine.orm.metadata.annotation.class
不是一个选择,我们需要使用 Symfony DC 超级能力,这是工作示例:

use Doctrine\Bundle\DoctrineBundle\Mapping\MappingDriver;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\Driver\MappingDriver as MappingDriverInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute;

#[Attribute\AsDecorator('.doctrine.orm.default_metadata_driver')]
final class DoctrineMappingDriverOverride extends MappingDriver implements MappingDriverInterface
{
    private MappingDriverInterface $driver;
    private ContainerInterface $idGeneratorLocator;

    public function __construct(
        #[Attribute\Autowire(service: '.doctrine.orm.default_metadata_driver.inner')]
        MappingDriverInterface $driver,
        #[Attribute\Autowire(service: 'doctrine.id_generator_locator')]
        ContainerInterface $idGeneratorLocator,
    ) {
        parent::__construct($driver, $idGeneratorLocator);
        $this->driver = $driver;
        $this->idGeneratorLocator = $idGeneratorLocator;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.