diff --git a/phpstan.neon b/phpstan.neon index 676b1733..fa61ab43 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,5 +5,6 @@ parameters: - src - tests includes: + - vendor/phpstan/phpstan-phpunit/extension.neon - vendor/phpstan/phpstan-phpunit/rules.neon - vendor/jangregor/phpstan-prophecy/extension.neon diff --git a/src/Service/DriverFactory.php b/src/Service/DriverFactory.php index 48b35e21..f0229a9c 100644 --- a/src/Service/DriverFactory.php +++ b/src/Service/DriverFactory.php @@ -5,8 +5,6 @@ namespace DoctrineModule\Service; use Doctrine\Common\Annotations; -use Doctrine\ORM\Mapping\Driver\AttributeDriver; -use Doctrine\Persistence\Mapping\Driver\AnnotationDriver; use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator; use Doctrine\Persistence\Mapping\Driver\FileDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriver; @@ -14,12 +12,12 @@ use DoctrineModule\Options\Driver; use Interop\Container\ContainerInterface; use InvalidArgumentException; +use ReflectionClass; use RuntimeException; use function class_exists; use function get_class; use function interface_exists; -use function is_subclass_of; use function sprintf; /** @@ -70,10 +68,10 @@ protected function createDriver(ContainerInterface $container, Driver $options): // Not all drivers (DriverChain) require paths. $paths = $options->getPaths(); + $constructor = (new ReflectionClass($class))->getConstructor(); // Special options for AnnotationDrivers. if ( - $class !== AttributeDriver::class && - ($class === AnnotationDriver::class || is_subclass_of($class, AnnotationDriver::class)) + $constructor !== null && $constructor->getNumberOfParameters() === 2 ) { $reader = new Annotations\AnnotationReader(); $reader = new Annotations\CachedReader( diff --git a/tests/Service/DriverFactoryTest.php b/tests/Service/DriverFactoryTest.php index b9d05503..5060ae39 100644 --- a/tests/Service/DriverFactoryTest.php +++ b/tests/Service/DriverFactoryTest.php @@ -4,6 +4,9 @@ namespace DoctrineModuleTest\Service; +use Doctrine\Common\Annotations\Reader; +use Doctrine\Common\Cache\ArrayCache; +use Doctrine\ORM\Mapping\Driver\AnnotationDriver; use Doctrine\ORM\Mapping\Driver\AttributeDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use DoctrineModule\Service\DriverFactory; @@ -90,4 +93,28 @@ public function testCreateAttributeDriver(): void $driver = $factory->__invoke($serviceManager, AttributeDriver::class); $this->assertInstanceOf(AttributeDriver::class, $driver); } + + public function testCreateAnnotationDriver(): void + { + $serviceManager = new ServiceManager(); + $serviceManager->setService( + 'config', + [ + 'doctrine' => [ + 'driver' => [ + 'testDriver' => ['class' => AnnotationDriver::class], + ], + ], + ] + ); + $serviceManager->setService( + 'doctrine.cache.array', + new ArrayCache() + ); + + $factory = new DriverFactory('testDriver'); + $driver = $factory->__invoke($serviceManager, AnnotationDriver::class); + $this->assertInstanceOf(AnnotationDriver::class, $driver); + $this->assertInstanceOf(Reader::class, $driver->getReader()); + } }