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

Service definition changes for EntityValueResolver #1812

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@
<argument type="service" id="doctrine" />
</service>

<service id="Symfony\Bridge\Doctrine\ArgumentResolver\UserInjector">
<argument type="service" id="security.token_storage" on-invalid="ignore" />
<tag name="doctrine.orm.entity_value_resolver_expression_modifier_injector" key="userInjector"/>
</service>

<service id="doctrine.orm.entity_value_resolver" class="Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver">
<argument type="service" id="doctrine" />
<argument type="service" id="doctrine.orm.entity_value_resolver.expression_language" on-invalid="ignore" />
Expand Down
7 changes: 6 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function getConfigTreeBuilder(): TreeBuilder
private function addDbalSection(ArrayNodeDefinition $node): void
{
// Key that should not be rewritten to the connection config
$excludedKeys = ['default_connection' => true, 'driver_schemes' => true, 'driver_scheme' => true, 'types' => true, 'type' => true];
$excludedKeys = ['default_connection' => true, 'driver_schemes' => true, 'driver_scheme' => true, 'types' => true, 'type' => true, 'default_expression_modifier_injector' => true];

$node
->children()
Expand Down Expand Up @@ -167,6 +167,11 @@ private function addDbalSection(ArrayNodeDefinition $node): void
->end()
->end()
->end()
->children()
->arrayNode('default_expression_modifier_injector')->scalarPrototype()->end()
->beforeNormalization()->castToArray()->end()
->end()
->end()
->fixXmlConfig('connection')
->append($this->getDbalConnectionsNode())
->end();
Expand Down
14 changes: 13 additions & 1 deletion src/DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use InvalidArgumentException;
use LogicException;
use Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver;
use Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolverExpressionModifiersInjectorInterface;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
Expand All @@ -50,6 +51,7 @@
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -87,6 +89,7 @@
* driver_schemes: array<string, string>,
* default_connection: string,
* types: array<string, string>,
* default_expression_modifier_injector: array<string>
* }
*/
class DoctrineExtension extends AbstractDoctrineExtension
Expand Down Expand Up @@ -181,6 +184,7 @@
$container->setAlias('doctrine.dbal.event_manager', new Alias(sprintf('doctrine.dbal.%s_connection.event_manager', $this->defaultConnection), false));

$container->setParameter('doctrine.dbal.connection_factory.types', $config['types']);
$container->setParameter('doctrine.dbal.default_expression_modifier_injector', $config['default_expression_modifier_injector']);

$container->getDefinition('doctrine.dbal.connection_factory.dsn_parser')->setArgument(0, array_merge(ConnectionFactory::DEFAULT_SCHEME_MAP, $config['driver_schemes']));

Expand Down Expand Up @@ -496,7 +500,6 @@
// available in Symfony 6.2 and higher
if (! class_exists(EntityValueResolver::class)) {
$container->removeDefinition('doctrine.orm.entity_value_resolver');
$container->removeDefinition('doctrine.orm.entity_value_resolver.expression_language');
} else {
if (! class_exists(ExpressionLanguage::class)) {
$container->removeDefinition('doctrine.orm.entity_value_resolver.expression_language');
Expand Down Expand Up @@ -533,6 +536,15 @@
$controllerResolverDefaults['evict_cache'] ?? null,
$controllerResolverDefaults['disabled'] ?? false,
]));
} else {
$container->getDefinition('doctrine.orm.entity_value_resolver')->setArgument(2, (new Definition(MapEntity::class)));
Copy link
Member

Choose a reason for hiding this comment

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

why this change ?

Copy link
Author

@eltharin eltharin Jul 12, 2024

Choose a reason for hiding this comment

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

If not I can't set my new argument, I have this error :

``
In DefinitionErrorExceptionPass.php line 48:

Invalid constructor argument 4 for service "doctrine.orm.entity_value_resolver": argument 3 must be defined before. Check your service definition.
``

Copy link
Author

Choose a reason for hiding this comment

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

I can just remove the if to have only one initialisation.

}

if (class_exists(ExpressionLanguage::class) && interface_exists(EntityValueResolverExpressionModifiersInjectorInterface::class)) {
/** @psalm-suppress UndefinedClass */
$container->registerForAutoconfiguration(EntityValueResolverExpressionModifiersInjectorInterface::class)->addTag('doctrine.orm.entity_value_resolver_expression_modifier_injector');
$container->getDefinition('doctrine.orm.entity_value_resolver')->setArgument(3, new AutowireLocator('doctrine.orm.entity_value_resolver_expression_modifier_injector', 'key'));
$container->getDefinition('doctrine.orm.entity_value_resolver')->setArgument(4, $container->getParameter('doctrine.dbal.default_expression_modifier_injector'));

Check warning on line 547 in src/DependencyInjection/DoctrineExtension.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/DoctrineExtension.php#L545-L547

Added lines #L545 - L547 were not covered by tests
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ public function testControllerResolver(bool $simpleEntityManagerConfig): void

$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());
$this->assertEquals([new Reference('doctrine'), new Reference('doctrine.orm.entity_value_resolver.expression_language', $container::IGNORE_ON_INVALID_REFERENCE), new Definition(MapEntity::class)], $controllerResolver->getArguments());

$container = $this->getContainer();

Expand Down
Loading