|
3 | 3 | namespace PHPStan;
|
4 | 4 |
|
5 | 5 | use Doctrine\Common\Annotations\AnnotationReader;
|
| 6 | +use Doctrine\Common\Annotations\AnnotationRegistry; |
6 | 7 | use Doctrine\Common\EventManager;
|
7 | 8 | use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
|
8 | 9 | use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
|
|
14 | 15 | class DoctrineClassMetadataProvider
|
15 | 16 | {
|
16 | 17 |
|
17 |
| - /** |
18 |
| - * @var EntityManager |
19 |
| - */ |
20 |
| - private $em; |
| 18 | + /** @var EntityManager */ |
| 19 | + private $em; |
21 | 20 |
|
22 |
| - /** |
23 |
| - * @var string |
24 |
| - */ |
25 |
| - private $repositoryClass; |
| 21 | + /** @var string */ |
| 22 | + private $repositoryClass; |
26 | 23 |
|
27 |
| - public function __construct(string $repositoryClass, array $mapping) |
28 |
| - { |
29 |
| - $configuration = new Configuration(); |
30 |
| - $configuration->setDefaultRepositoryClassName($repositoryClass); |
31 |
| - $configuration->setMetadataDriverImpl($this->setupMappingDriver($mapping)); |
32 |
| - $configuration->setProxyDir('/dev/null'); |
33 |
| - $configuration->setProxyNamespace('__DP__'); |
34 |
| - $configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL); |
35 |
| - $evm = new EventManager(); |
36 |
| - $this->em = EntityManager::create( |
37 |
| - \Doctrine\DBAL\DriverManager::getConnection(['host' => '/:memory:', 'driver' => 'pdo_sqlite'], $configuration, $evm), |
38 |
| - $configuration, |
39 |
| - $evm |
40 |
| - ); |
41 |
| - $this->repositoryClass = $repositoryClass; |
42 |
| - } |
| 24 | + /** |
| 25 | + * @param string $repositoryClass |
| 26 | + * @param mixed[] $mapping |
| 27 | + */ |
| 28 | + public function __construct(string $repositoryClass, array $mapping) |
| 29 | + { |
| 30 | + AnnotationRegistry::registerLoader('class_exists'); |
| 31 | + $configuration = new Configuration(); |
| 32 | + $configuration->setDefaultRepositoryClassName($repositoryClass); |
| 33 | + $configuration->setMetadataDriverImpl($this->setupMappingDriver($mapping)); |
| 34 | + $configuration->setProxyDir('/dev/null'); |
| 35 | + $configuration->setProxyNamespace('__DP__'); |
| 36 | + $configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL); |
| 37 | + $evm = new EventManager(); |
| 38 | + $this->em = EntityManager::create( |
| 39 | + \Doctrine\DBAL\DriverManager::getConnection(['host' => '/:memory:', 'driver' => 'pdo_sqlite'], $configuration, $evm), |
| 40 | + $configuration, |
| 41 | + $evm |
| 42 | + ); |
| 43 | + $this->repositoryClass = $repositoryClass; |
| 44 | + } |
43 | 45 |
|
44 |
| - private function setupMappingDriver(array $mapping): MappingDriver |
45 |
| - { |
46 |
| - $driver = new MappingDriverChain(); |
47 |
| - foreach ($mapping as $namespace => $config) { |
48 |
| - switch ($config['type']) { |
49 |
| - case 'annotation': |
50 |
| - $nested = new Mapping\Driver\AnnotationDriver(new AnnotationReader(), $config['paths']); |
51 |
| - break; |
52 |
| - case 'yml': |
53 |
| - case 'yaml': |
54 |
| - $nested = new Mapping\Driver\YamlDriver($config['paths']); |
55 |
| - break; |
56 |
| - case 'xml': |
57 |
| - $nested = new Mapping\Driver\XmlDriver($config['paths']); |
58 |
| - break; |
59 |
| - default: |
60 |
| - throw new \InvalidArgumentException('Unknown mapping type: ' . $config['type']); |
61 |
| - } |
62 |
| - $driver->addDriver($nested, $namespace); |
63 |
| - } |
64 |
| - return $driver; |
65 |
| - } |
| 46 | + /** |
| 47 | + * @param mixed[] $mapping |
| 48 | + */ |
| 49 | + private function setupMappingDriver(array $mapping): MappingDriver |
| 50 | + { |
| 51 | + $driver = new MappingDriverChain(); |
| 52 | + foreach ($mapping as $namespace => $config) { |
| 53 | + switch ($config['type']) { |
| 54 | + case 'annotation': |
| 55 | + $nested = new Mapping\Driver\AnnotationDriver(new AnnotationReader(), $config['paths']); |
| 56 | + break; |
| 57 | + case 'yml': |
| 58 | + case 'yaml': |
| 59 | + $nested = new Mapping\Driver\YamlDriver($config['paths']); |
| 60 | + break; |
| 61 | + case 'xml': |
| 62 | + $nested = new Mapping\Driver\XmlDriver($config['paths']); |
| 63 | + break; |
| 64 | + default: |
| 65 | + throw new \InvalidArgumentException('Unknown mapping type: ' . $config['type']); |
| 66 | + } |
| 67 | + $driver->addDriver($nested, $namespace); |
| 68 | + } |
| 69 | + return $driver; |
| 70 | + } |
66 | 71 |
|
67 |
| - public function getBaseRepositoryClass(): string |
68 |
| - { |
69 |
| - return $this->repositoryClass; |
70 |
| - } |
| 72 | + public function getBaseRepositoryClass(): string |
| 73 | + { |
| 74 | + return $this->repositoryClass; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param string $className |
| 79 | + * @return Mapping\ClassMetadataInfo |
| 80 | + * @throws \Doctrine\Common\Persistence\Mapping\MappingException |
| 81 | + * @throws \ReflectionException |
| 82 | + */ |
| 83 | + public function getMetadataFor(string $className): Mapping\ClassMetadataInfo |
| 84 | + { |
| 85 | + return $this->em->getClassMetadata($className); |
| 86 | + } |
71 | 87 |
|
72 |
| - /** |
73 |
| - * @throws \Doctrine\Common\Persistence\Mapping\MappingException |
74 |
| - * @throws \ReflectionException |
75 |
| - */ |
76 |
| - public function getMetadataFor(string $className): Mapping\ClassMetadataInfo |
77 |
| - { |
78 |
| - return $this->em->getClassMetadata($className); |
79 |
| - } |
80 |
| - |
81 | 88 | }
|
0 commit comments