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 21, 2022
1 parent 61dc078 commit 9b5d144
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Service/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
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;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
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;

/**
Expand Down Expand Up @@ -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(
Expand Down
28 changes: 28 additions & 0 deletions tests/Service/DriverFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,4 +93,29 @@ 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);
assert($driver instanceof AnnotationDriver);
$this->assertInstanceOf(Reader::class, $driver->getReader());
}
}

0 comments on commit 9b5d144

Please sign in to comment.