Skip to content

Commit 294b0fb

Browse files
authored
Add DocumentValueResolver and MapDocument (#774)
* Add Symfony 6.3 to the test matrix * Introduce DocumentValueResolver and MapDocument
1 parent a14aee5 commit 294b0fb

File tree

14 files changed

+560
-51
lines changed

14 files changed

+560
-51
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
symfony-version:
3232
- "5.4.x"
3333
- "6.2.x"
34+
- "6.3.x"
3435
driver-version:
3536
- "stable"
3637
dependencies:
@@ -45,6 +46,8 @@ jobs:
4546
exclude:
4647
- php-version: "7.4"
4748
symfony-version: "6.2.x"
49+
- php-version: "7.4"
50+
symfony-version: "6.3.x"
4851

4952
services:
5053
mongodb:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Bundle\MongoDBBundle\ArgumentResolver;
6+
7+
use Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver;
8+
use Symfony\Component\HttpFoundation\Request;
9+
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
10+
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
11+
12+
/** @internal */
13+
final class DocumentValueResolver implements ValueResolverInterface
14+
{
15+
public function __construct(
16+
private EntityValueResolver $entityValueResolver,
17+
) {
18+
}
19+
20+
public function resolve(Request $request, ArgumentMetadata $argument): array
21+
{
22+
return $this->entityValueResolver->resolve($request, $argument);
23+
}
24+
}

Attribute/MapDocument.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Bundle\MongoDBBundle\Attribute;
6+
7+
use Attribute;
8+
use Doctrine\Bundle\MongoDBBundle\ArgumentResolver\DocumentValueResolver;
9+
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
10+
11+
#[Attribute(Attribute::TARGET_PARAMETER)]
12+
class MapDocument extends MapEntity
13+
{
14+
public function __construct(
15+
public ?string $class = null,
16+
public ?string $objectManager = null,
17+
public ?string $expr = null,
18+
public ?array $mapping = null,
19+
public ?array $exclude = null,
20+
public ?bool $stripNull = null,
21+
public array|string|null $id = null,
22+
bool $disabled = false,
23+
string $resolver = DocumentValueResolver::class,
24+
) {
25+
parent::__construct($class, $objectManager, $expr, $mapping, $exclude, $stripNull, $id, null, $disabled, $resolver);
26+
}
27+
}

DependencyInjection/DoctrineMongoDBExtension.php

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\Bundle\MongoDBBundle\DependencyInjection;
66

77
use Doctrine\Bundle\MongoDBBundle\Attribute\AsDocumentListener;
8+
use Doctrine\Bundle\MongoDBBundle\Attribute\MapDocument;
89
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\FixturesCompilerPass;
910
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
1011
use Doctrine\Bundle\MongoDBBundle\EventSubscriber\EventSubscriberInterface;
@@ -18,14 +19,14 @@
1819
use InvalidArgumentException;
1920
use Jean85\PrettyVersions;
2021
use Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver;
21-
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
2222
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
2323
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
2424
use Symfony\Component\Cache\Adapter\ApcuAdapter;
2525
use Symfony\Component\Cache\Adapter\ArrayAdapter;
2626
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
2727
use Symfony\Component\Cache\Adapter\RedisAdapter;
2828
use Symfony\Component\Config\FileLocator;
29+
use Symfony\Component\Config\Loader\FileLoader;
2930
use Symfony\Component\DependencyInjection\Alias;
3031
use Symfony\Component\DependencyInjection\ChildDefinition;
3132
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -149,39 +150,7 @@ public function load(array $configs, ContainerBuilder $container)
149150

150151
$this->loadMessengerServices($container);
151152

152-
// available in Symfony 6.2 and higher
153-
if (! class_exists(EntityValueResolver::class)) {
154-
$container->removeDefinition('doctrine_mongodb.odm.entity_value_resolver');
155-
$container->removeDefinition('doctrine_mongodb.odm.entity_value_resolver.expression_language');
156-
} else {
157-
if (! class_exists(ExpressionLanguage::class)) {
158-
$container->removeDefinition('doctrine_mongodb.odm.entity_value_resolver.expression_language');
159-
}
160-
161-
$controllerResolverDefaults = [];
162-
163-
if (! $config['controller_resolver']['enabled']) {
164-
$controllerResolverDefaults['disabled'] = true;
165-
}
166-
167-
if (! $config['controller_resolver']['auto_mapping']) {
168-
$controllerResolverDefaults['mapping'] = [];
169-
}
170-
171-
if ($controllerResolverDefaults) {
172-
$container->getDefinition('doctrine_mongodb.odm.entity_value_resolver')->setArgument(2, (new Definition(MapEntity::class))->setArguments([
173-
null,
174-
null,
175-
null,
176-
$controllerResolverDefaults['mapping'] ?? null,
177-
null,
178-
null,
179-
null,
180-
null,
181-
$controllerResolverDefaults['disabled'] ?? false,
182-
]));
183-
}
184-
}
153+
$this->loadEntityValueResolverServices($container, $loader, $config);
185154
}
186155

187156
/**
@@ -432,6 +401,46 @@ private function loadMessengerServices(ContainerBuilder $container): void
432401
$loader->load('messenger.xml');
433402
}
434403

404+
/** @param array<string, mixed> $config */
405+
private function loadEntityValueResolverServices(ContainerBuilder $container, FileLoader $loader, array $config): void
406+
{
407+
// available in Symfony 6.2 and higher
408+
if (! class_exists(EntityValueResolver::class)) {
409+
return;
410+
}
411+
412+
$loader->load('value_resolver.xml');
413+
414+
if (! class_exists(ExpressionLanguage::class)) {
415+
$container->removeDefinition('doctrine_mongodb.odm.document_value_resolver.expression_language');
416+
}
417+
418+
$controllerResolverDefaults = [];
419+
420+
if (! $config['controller_resolver']['enabled']) {
421+
$controllerResolverDefaults['disabled'] = true;
422+
}
423+
424+
if (! $config['controller_resolver']['auto_mapping']) {
425+
$controllerResolverDefaults['mapping'] = [];
426+
}
427+
428+
if ($controllerResolverDefaults === []) {
429+
return;
430+
}
431+
432+
$container->getDefinition('doctrine_mongodb.odm.entity_value_resolver')->setArgument(2, (new Definition(MapDocument::class))->setArguments([
433+
null,
434+
null,
435+
null,
436+
$controllerResolverDefaults['mapping'] ?? null,
437+
null,
438+
null,
439+
null,
440+
$controllerResolverDefaults['disabled'] ?? false,
441+
]));
442+
}
443+
435444
/**
436445
* Normalizes the driver options array
437446
*

Resources/config/mongodb.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,5 @@
208208
<service id="doctrine_mongodb.odm.symfony.fixtures.loader" class="Doctrine\Bundle\MongoDBBundle\Loader\SymfonyFixturesLoader" public="false">
209209
<argument type="service" id="service_container" />
210210
</service>
211-
212-
<service id="doctrine_mongodb.odm.entity_value_resolver" class="Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver">
213-
<argument type="service" id="doctrine_mongodb" />
214-
<argument type="service" id="doctrine_mongodb.odm.entity_value_resolver.expression_language" on-invalid="ignore" />
215-
<tag name="controller.argument_value_resolver" priority="110" />
216-
</service>
217-
218-
<service id="doctrine_mongodb.odm.entity_value_resolver.expression_language" class="Symfony\Component\ExpressionLanguage\ExpressionLanguage" />
219211
</services>
220212
</container>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<service id="doctrine_mongodb.odm.entity_value_resolver" class="Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver">
9+
<argument type="service" id="doctrine_mongodb" />
10+
<argument type="service" id="doctrine_mongodb.odm.document_value_resolver.expression_language" on-invalid="ignore" />
11+
</service>
12+
13+
<service id="doctrine_mongodb.odm.document_value_resolver.expression_language" class="Symfony\Component\ExpressionLanguage\ExpressionLanguage" />
14+
15+
<service id="doctrine_mongodb.odm.document_value_resolver" class="Doctrine\Bundle\MongoDBBundle\ArgumentResolver\DocumentValueResolver">
16+
<argument type="service" id="doctrine_mongodb.odm.entity_value_resolver" />
17+
<tag name="Doctrine\Bundle\MongoDBBundle\ArgumentResolver\DocumentValueResolver" priority="110">controller.argument_value_resolver</tag>
18+
</service>
19+
</services>
20+
</container>

0 commit comments

Comments
 (0)