Skip to content

Commit

Permalink
Adapt to new type hierarchy
Browse files Browse the repository at this point in the history
In recent versions of ORM and ODM, annotation driver no longer extend a
base class in doctrine/persistence. Instead of testing for that base
class, detecting the number of constructor arguments seems like a more
robust way to figure out what arguments to pass.
  • Loading branch information
greg0ire committed Apr 20, 2022
1 parent 61dc078 commit c2c3429
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Service/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use DoctrineModule\Options\Driver;
use Interop\Container\ContainerInterface;
use InvalidArgumentException;
use ReflectionClass;
use RuntimeException;

use function class_exists;
Expand Down Expand Up @@ -70,10 +71,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(
Expand Down
22 changes: 22 additions & 0 deletions tests/Service/DriverFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DoctrineModuleTest\Service;

use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use DoctrineModule\Service\DriverFactory;
Expand Down Expand Up @@ -90,4 +92,24 @@ 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],
],
],
]
);

$factory = new DriverFactory('testDriver');
$driver = $factory->__invoke($serviceManager, AnnotationDriver::class);
$this->assertInstanceOf(AnnotationDriver::class, $driver);
$this->assertInstanceOf(Reader::class, $driver->getReader());
}
}

0 comments on commit c2c3429

Please sign in to comment.