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 the EntityArgumentResolver #754

Merged
merged 1 commit into from
Mar 5, 2023
Merged
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
9 changes: 9 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ public function getConfigTreeBuilder()
->scalarNode('safe')->info('Deprecated. Please use the "w" option instead.')->end()
->end()
->end()
->arrayNode('controller_resolver')
->canBeDisabled()
->children()
->booleanNode('auto_mapping')
->defaultTrue()
->info('Set to false to disable using route placeholders as lookup criteria when the object id doesn\'t match the argument name')
->end()
->end()
->end()
->end();

return $treeBuilder;
Expand Down
37 changes: 37 additions & 0 deletions DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Doctrine\Common\EventSubscriber;
use Doctrine\ODM\MongoDB\DocumentManager;
use InvalidArgumentException;
use Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
Expand All @@ -30,6 +32,7 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Messenger\MessageBusInterface;

use function array_keys;
Expand Down Expand Up @@ -142,6 +145,40 @@ public function load(array $configs, ContainerBuilder $container)
}

$this->loadMessengerServices($container);

// available in Symfony 6.2 and higher
if (! class_exists(EntityValueResolver::class)) {
$container->removeDefinition('doctrine_mongodb.odm.entity_value_resolver');
$container->removeDefinition('doctrine_mongodb.odm.entity_value_resolver.expression_language');
} else {
if (! class_exists(ExpressionLanguage::class)) {
$container->removeDefinition('doctrine_mongodb.odm.entity_value_resolver.expression_language');
}

$controllerResolverDefaults = [];

if (! $config['controller_resolver']['enabled']) {
$controllerResolverDefaults['disabled'] = true;
}

if (! $config['controller_resolver']['auto_mapping']) {
$controllerResolverDefaults['mapping'] = [];
}

if ($controllerResolverDefaults) {
$container->getDefinition('doctrine_mongodb.odm.entity_value_resolver')->setArgument(2, (new Definition(MapEntity::class))->setArguments([
null,
null,
null,
$controllerResolverDefaults['mapping'] ?? null,
null,
null,
null,
null,
$controllerResolverDefaults['disabled'] ?? false,
]));
}
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions Resources/config/mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,13 @@
<service id="doctrine_mongodb.odm.symfony.fixtures.loader" class="Doctrine\Bundle\MongoDBBundle\Loader\SymfonyFixturesLoader" public="false">
<argument type="service" id="service_container" />
</service>

<service id="doctrine_mongodb.odm.entity_value_resolver" class="Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver">
<argument type="service" id="doctrine_mongodb" />
<argument type="service" id="doctrine_mongodb.odm.entity_value_resolver.expression_language" on-invalid="ignore" />
<tag name="controller.argument_value_resolver" priority="110" />
</service>

<service id="doctrine_mongodb.odm.entity_value_resolver.expression_language" class="Symfony\Component\ExpressionLanguage\ExpressionLanguage" />
</services>
</container>
8 changes: 8 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public function testDefaults(): void
'persistent_collection_dir' => '%kernel.cache_dir%/doctrine/odm/mongodb/PersistentCollections',
'persistent_collection_namespace' => 'PersistentCollections',
'types' => [],
'controller_resolver' => [
'enabled' => true,
'auto_mapping' => true,
],
];

$this->assertEquals($defaults, $options);
Expand Down Expand Up @@ -203,6 +207,10 @@ public function testFullConfiguration(array $config): void
],
'resolve_target_documents' => ['Foo\BarInterface' => 'Bar\FooClass'],
'types' => [],
'controller_resolver' => [
'enabled' => true,
'auto_mapping' => true,
],
];

$this->assertEquals($expected, $options);
Expand Down
26 changes: 26 additions & 0 deletions Tests/DependencyInjection/DoctrineMongoDBExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
use Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\DocumentListenerBundle\EventListener\TestAttributeListener;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Container;
Expand Down Expand Up @@ -347,4 +348,29 @@ private function assertDICDefinitionMethodCall(Definition $definition, string $m

$this->fail("Method '" . $methodName . "' is expected to be called once, definition does not contain a call though.");
}

/** @requires function \Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver::__construct */
public function testControllerResolver(): void
{
$container = $this->getContainer();
$extension = new DoctrineMongoDBExtension();
$extension->load(self::buildConfiguration(), $container);

$controllerResolver = $container->getDefinition('doctrine_mongodb.odm.entity_value_resolver');

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

$container = $this->getContainer();

$extension->load(self::buildConfiguration([
'controller_resolver' => [
'enabled' => false,
'auto_mapping' => false,
],
]), $container);

$container->setDefinition('controller_resolver_defaults', $container->getDefinition('doctrine_mongodb.odm.entity_value_resolver')->getArgument(2))->setPublic(true);
$container->compile();
$this->assertEquals(new MapEntity(null, null, null, [], null, null, null, null, true), $container->get('controller_resolver_defaults'));
}
}