我必须在 Doctrine\Common\Persistence\Mapping\Driver\Annotation::getAllClassNames 中进行一些更改,因为 Doctrine 使用 require_one 并且我希望它使用我的自定义 spl 自动加载器。
我的问题是:如何告诉 Doctrine 使用我自己的代码而不更改 Symfony 的供应商文件夹?
您可以通过将参数
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>
我自己没有测试过这一点,但这就是我过去重写此类事情的方式。
在我的例子中,我覆盖了 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;
}
}