Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wire up type aliases support in EntityValueResolver #1790

Open
wants to merge 1 commit into
base: 2.12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/DependencyInjection/DoctrineExtension.php
Expand Up @@ -32,6 +32,7 @@
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
use InvalidArgumentException;
use LogicException;
use ReflectionClass;
use Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
Expand Down Expand Up @@ -625,6 +626,22 @@ protected function ormLoad(array $config, ContainerBuilder $container)
$def
->addTag('doctrine.event_listener', ['event' => Events::loadClassMetadata])
->addTag('doctrine.event_listener', ['event' => Events::onClassMetadataNotFound]);

// Symfony 7.1 and higher expose type alias support in the EntityValueResolver
if (class_exists(EntityValueResolver::class)) {
$valueResolverReflection = new ReflectionClass(EntityValueResolver::class);

if ($valueResolverReflection->hasMethod('addTypeAlias')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using a constructor argument instead of an adder can save us from using reflection at all. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially that is what I tried to do, but I figured explicitly checking support without depending on constructor argument count and order was more desirable. This also seems more in line with other parts of the extension which also call add* or set* methods. I do agree that reflection is generally undesirable.
Having this as a method also allows for easier extension of the ValueResolver beyond the bundle, since the property holding this mapping is otherwise private; although this can also be achieved with explicit service configuration in the framework.

Also, setting the argument on its position by key doesn't seem to work. Replacing the reflection code in this PR with the following snippet:

$valueResolverDefinition->setArgument(3, $config['resolve_target_entities']);

Results in an error: TypeError: Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver::__construct(): Argument #3 ($defaults) must be of type Symfony\Bridge\Doctrine\Attribute\MapEntity, array given
Even when using 4 in the above code snippet. Using explicit argument names (->setArgument('$typeAliases', ...)) doesn't work as this causes a container compilation error on older bridge versions because the argument does not exist; this would break backwards compatibility (or still need reflection to check the existance of the property/argument).

$valueResolverDefinition = $container->getDefinition('doctrine.orm.entity_value_resolver');

foreach ($config['resolve_target_entities'] as $name => $implementation) {
$valueResolverDefinition->addMethodCall('addTypeAlias', [
$name,
$implementation,
]);
}
}
}
}

$container->registerForAutoconfiguration(ServiceEntityRepositoryInterface::class)
Expand Down
6 changes: 6 additions & 0 deletions tests/ContainerTest.php
Expand Up @@ -15,6 +15,7 @@
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver;
use Symfony\Bridge\Doctrine\CacheWarmer\ProxyCacheWarmer;
use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector;
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
Expand All @@ -23,6 +24,7 @@
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;

use function class_exists;
use function interface_exists;

class ContainerTest extends TestCase
Expand All @@ -39,6 +41,10 @@ public function testContainer(): void
$this->assertInstanceOf(Reader::class, $container->get('doctrine.orm.metadata.annotation_reader'));
}

if (class_exists(EntityValueResolver::class)) {
$this->assertInstanceOf(EntityValueResolver::class, $container->get('doctrine.orm.entity_value_resolver'));
}

$this->assertInstanceOf(DoctrineDataCollector::class, $container->get('data_collector.doctrine'));
$this->assertInstanceOf(DBALConfiguration::class, $container->get('doctrine.dbal.default_connection.configuration'));
$this->assertInstanceOf(EventManager::class, $container->get('doctrine.dbal.default_connection.event_manager'));
Expand Down
13 changes: 12 additions & 1 deletion tests/DependencyInjection/DoctrineExtensionTest.php
Expand Up @@ -37,6 +37,7 @@
use LogicException;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand Down Expand Up @@ -1445,14 +1446,24 @@ public function testControllerResolver(bool $simpleEntityManagerConfig): void
$config['orm'] = [];
}

$config['orm']['controller_resolver'] = ['auto_mapping' => true];
$config['orm']['controller_resolver'] = ['auto_mapping' => true];
$config['orm']['resolve_target_entities'] = ['Throwable' => 'stdClass'];

$extension->load([$config], $container);

$controllerResolver = $container->getDefinition('doctrine.orm.entity_value_resolver');

$this->assertEquals([new Reference('doctrine'), new Reference('doctrine.orm.entity_value_resolver.expression_language', $container::IGNORE_ON_INVALID_REFERENCE)], $controllerResolver->getArguments());

$resolverReflection = new ReflectionClass(EntityValueResolver::class);
$calls = $controllerResolver->getMethodCalls();

if ($resolverReflection->hasMethod('addTypeAlias')) {
self::assertEquals([['addTypeAlias', ['Throwable', 'stdClass']]], $calls);
} else {
self::assertEmpty($calls);
}

$container = $this->getContainer();

$config['orm']['controller_resolver'] = [
Expand Down