44
55use Doctrine \Common \Annotations \AnnotationReader ;
66use Doctrine \Common \Annotations \DocParser ;
7- use Doctrine \Common \EventManager ;
8- use Doctrine \DBAL \Platforms \MySqlPlatform ;
7+ use Doctrine \DBAL \DriverManager ;
8+ use Doctrine \ORM \Configuration ;
9+ use Doctrine \ORM \EntityManager ;
910use Doctrine \ORM \Mapping \ClassMetadata ;
1011use Doctrine \ORM \Mapping \Driver \AnnotationDriver ;
1112use Doctrine \ORM \Mapping \Driver \AttributeDriver ;
12- use ReflectionClass ;
13+ use Doctrine \ ORM \ Proxy \ ProxyFactory ;
1314use function class_exists ;
1415use function count ;
1516use const PHP_VERSION_ID ;
1617
1718class ClassMetadataFactory extends \Doctrine \ORM \Mapping \ClassMetadataFactory
1819{
1920
20- protected function initialize (): void
21+ /** @var string */
22+ private $ tmpDir ;
23+
24+ public function __construct (string $ tmpDir )
2125 {
22- $ parentReflection = new ReflectionClass (parent ::class);
23- $ driverProperty = $ parentReflection ->getProperty ('driver ' );
24- $ driverProperty ->setAccessible (true );
26+ $ this ->tmpDir = $ tmpDir ;
27+ }
2528
29+ protected function initialize (): void
30+ {
2631 $ drivers = [];
2732 if (class_exists (AnnotationReader::class)) {
2833 $ docParser = new DocParser ();
@@ -33,23 +38,21 @@ protected function initialize(): void
3338 $ drivers [] = new AttributeDriver ([]);
3439 }
3540
36- $ driverProperty ->setValue ($ this , count ($ drivers ) === 1 ? $ drivers [0 ] : new MappingDriverChain ($ drivers ));
41+ $ config = new Configuration ();
42+ $ config ->setMetadataDriverImpl (count ($ drivers ) === 1 ? $ drivers [0 ] : new MappingDriverChain ($ drivers ));
43+ $ config ->setAutoGenerateProxyClasses (ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED );
44+ $ config ->setProxyDir ($ this ->tmpDir );
45+ $ config ->setProxyNamespace ('__PHPStanDoctrine__ \\Proxy ' );
46+ $ connection = DriverManager::getConnection ([
47+ 'driver ' => 'pdo_sqlite ' ,
48+ 'memory ' => true ,
49+ ], $ config );
3750
38- $ evmProperty = $ parentReflection ->getProperty ('evm ' );
39- $ evmProperty ->setAccessible (true );
40- $ evmProperty ->setValue ($ this , new EventManager ());
41- $ this ->initialized = true ;
42-
43- $ targetPlatformProperty = $ parentReflection ->getProperty ('targetPlatform ' );
44- $ targetPlatformProperty ->setAccessible (true );
51+ $ em = new EntityManager ($ connection , $ config );
52+ $ this ->setEntityManager ($ em );
53+ parent ::initialize ();
4554
46- if (class_exists (MySqlPlatform::class)) {
47- $ platform = new MySqlPlatform ();
48- } else {
49- $ platform = new \Doctrine \DBAL \Platforms \MySQLPlatform ();
50- }
51-
52- $ targetPlatformProperty ->setValue ($ this , $ platform );
55+ $ this ->initialized = true ;
5356 }
5457
5558 /**
0 commit comments