Doctrine ORM 2.9同时使用AnnotationDriver和AttributeDriver来解析实体元数据

hel*_*ete 2 php doctrine-orm php-8 php-attributes

最近我们将应用程序升级到 PHP8。

由于PHP8引入了属性doctrine/orm从版本开始支持它们,2.9因此利用此功能增量(即不是一次所有实体)将实体元数据更新为属性格式似乎是个好主意。

为此,我需要以某种方式注册两者Doctrine\ORM\Mapping\Driver\AnnotationDriverDoctrine\ORM\Mapping\Driver\AttributeDriver解析元数据。

棘手的部分是为一组使用注释或属性修饰的实体注册两个解析器。从这一点来看,Doctrine\ORM\Configuration我所需要的似乎是不可能的。

我是否正确(假设这无法合理实现)或者可以通过某种不太黑客的方式来完成吗?

hel*_*ete 8

教义本身并不提供这种可能性。但我们可以实现一个自定义映射驱动程序来实现这一点。

实际的实现可能如下所示:

<?php                                                                           
                                                                                
namespace Utils\Doctrine;                                                    
                                                                                
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;                               
use Doctrine\ORM\Mapping\Driver\AttributeDriver;                                
use Doctrine\ORM\Mapping\MappingException;                                      
use Doctrine\Persistence\Mapping\ClassMetadata;                                 
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver;
                                                                                
class HybridMappingDriver extends AbstractAnnotationDriver                      
{                                                                               
    public function __construct(                                                
        private AnnotationDriver $annotationDriver,                                
        private AttributeDriver $attributeDriver,                                  
    ) {                                                                            
    }                                                                              
                                                                                
    public function loadMetadataForClass($className, ClassMetadata $metadata): void
    {                                                                           
        try {                                                                      
            $this->attributeDriver->loadMetadataForClass($className, $metadata);
            return;                                                             
        } catch (MappingException $me) {                                        
            // Class X is not a valid entity, so try the other driver            
            if (!preg_match('/^Class(.)*$/', $me->getMessage())) {// meh           
                throw $me;                                                         
            }                                                                      
        }                                                                       
        $this->annotationDriver->loadMetadataForClass($className, $metadata);   
    }                                                                            
                                                                                 
    public function isTransient($className): bool                                     
    {                                                                           
        return $this->attributeDriver->isTransient($className)                     
            || $this->annotationDriver->isTransient($className);                   
    }                                                                              
}
Run Code Online (Sandbox Code Playgroud)

简而言之:

  • 驱动程序首先尝试使用AttributeDriver,然后回退到,AnnotationDriver以防所检查的类未被评估为有效实体
  • 为了Doctrine\Persistence\Mapping\Driver\MappingDriver在扩展Doctrine\Persistence\Mapping\Driver\AnnotationDriver类后遵守接口,只需实现 2 个方法
  • 从示例实现中可以看出,两种方法都考虑了元数据映射驱动程序
  • 通过解析消息来区分各种MappingExceptions 一点也不优雅,但也没有更好的属性可以用来区分;每个映射错误案例具有不同的异常子类型或一些唯一的代码将有助于区分映射错误的各个原因

可以这样HybridMappingDriver连接:EntityManagerFactory

<?php

namespace App\Services\Doctrine;

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Proxy\AbstractProxyFactory as APF;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Utils\Doctrine\NullCache;

class EntityManagerFactory
{
    public static function create(
        array $params,
        MappingDriver $mappingDriver,
        bool $devMode,
    ): EntityManager {
        AnnotationRegistry::registerLoader('class_exists');
        $config = Setup::createConfiguration(
            $devMode,
            $params['proxy_dir'],
            new NullCache(), // must be an instance of Doctrine\Common\Cache\Cache
        );
        $config->setMetadataDriverImpl($mappingDriver); // <= this is the actual hook-up
        if (!$devMode) {
            $config->setAutoGenerateProxyClasses(APF::AUTOGENERATE_FILE_NOT_EXISTS);
        }

        return EntityManager::create($params['database'], $config);
    }
}
Run Code Online (Sandbox Code Playgroud)