4
4
5
5
use Doctrine \Common \Annotations \AnnotationReader ;
6
6
use 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 ;
9
10
use Doctrine \ORM \Mapping \ClassMetadata ;
10
11
use Doctrine \ORM \Mapping \Driver \AnnotationDriver ;
11
12
use Doctrine \ORM \Mapping \Driver \AttributeDriver ;
12
- use ReflectionClass ;
13
+ use Doctrine \ ORM \ Proxy \ ProxyFactory ;
13
14
use function class_exists ;
14
15
use function count ;
15
16
use const PHP_VERSION_ID ;
16
17
17
18
class ClassMetadataFactory extends \Doctrine \ORM \Mapping \ClassMetadataFactory
18
19
{
19
20
20
- protected function initialize (): void
21
+ /** @var string */
22
+ private $ tmpDir ;
23
+
24
+ public function __construct (string $ tmpDir )
21
25
{
22
- $ parentReflection = new ReflectionClass (parent ::class);
23
- $ driverProperty = $ parentReflection ->getProperty ('driver ' );
24
- $ driverProperty ->setAccessible (true );
26
+ $ this ->tmpDir = $ tmpDir ;
27
+ }
25
28
29
+ protected function initialize (): void
30
+ {
26
31
$ drivers = [];
27
32
if (class_exists (AnnotationReader::class)) {
28
33
$ docParser = new DocParser ();
@@ -33,23 +38,21 @@ protected function initialize(): void
33
38
$ drivers [] = new AttributeDriver ([]);
34
39
}
35
40
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 );
37
50
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 ();
45
54
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 ;
53
56
}
54
57
55
58
/**
0 commit comments