Skip to content

Commit

Permalink
bug #847 Fix dealing with Definition objects in SetDoctrineAnnotatedP…
Browse files Browse the repository at this point in the history
…refixesPass (nicolas-grekas)

This PR was merged into the 1.0-dev branch.

Discussion
----------

Fix dealing with Definition objects in SetDoctrineAnnotatedPrefixesPass

Fix #845

Can happen when [`DoctrineOrmMappingsPass::createAnnotationMappingDriver()`](https://github.com/doctrine/DoctrineBundle/blob/155a870ecbd035963e6b450e0f235e69c48cab30/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php#L124) is used in a third-party bundle.

Commits
-------

3fff156 Fix dealing with Definition objects in SetDoctrineAnnotatedPrefixesPass
  • Loading branch information
weaverryan committed Mar 23, 2021
2 parents 24b5149 + 3fff156 commit a395a85
Showing 1 changed file with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

namespace Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass;

use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class SetDoctrineAnnotatedPrefixesPass implements CompilerPassInterface
Expand All @@ -35,16 +38,35 @@ public function process(ContainerBuilder $container)
}

$managerName = $m[1];
$methodCalls = $metadataDriverImpl->getMethodCalls();

foreach ($metadataDriverImpl->getMethodCalls() as [$method, $arguments]) {
if ('addDriver' === $method) {
$isAnnotated = 'doctrine.orm.'.$managerName.'_annotation_metadata_driver' === (string) $arguments[0];
$annotatedPrefixes[$managerName][] = [
$arguments[1],
$isAnnotated ? new Reference($arguments[0]) : null,
];
foreach ($methodCalls as $i => [$method, $arguments]) {
if ('addDriver' !== $method) {
continue;
}

if ($arguments[0] instanceof Definition) {
$class = $arguments[0]->getClass();
$namespace = substr($class, 0, strrpos($class, '\\'));

if ('Doctrine\ORM\Mapping\Driver' === $namespace ? AnnotationDriver::class !== $class : !is_subclass_of($class, AbstractAnnotationDriver::class)) {
continue;
}

$id = sprintf('.%d_annotation_metadata_driver~%s', $i, ContainerBuilder::hash($arguments));
$container->setDefinition($id, $arguments[0]);
$arguments[0] = new Reference($id);
$methodCalls[$i] = $arguments;
}

$isAnnotated = false !== strpos($arguments[0], '_annotation_metadata_driver');
$annotatedPrefixes[$managerName][] = [
$arguments[1],
$isAnnotated ? new Reference($arguments[0]) : null,
];
}

$metadataDriverImpl->setMethodCalls($methodCalls);
}

if (null !== $annotatedPrefixes) {
Expand Down

0 comments on commit a395a85

Please sign in to comment.