Skip to content

Commit da4c493

Browse files
soyukavinceAmstoutz
authored andcommitted
fix(mongodb): add LoggerAwareTrait and ManagerRegistryAwareTrait, improve logging support for filters
1 parent a27de22 commit da4c493

File tree

8 files changed

+152
-8
lines changed

8 files changed

+152
-8
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Doctrine\Common\Filter;
15+
16+
use Psr\Log\LoggerInterface;
17+
18+
interface LoggerAwareInterface
19+
{
20+
public function hasLogger(): bool;
21+
22+
public function getLogger(): LoggerInterface;
23+
24+
public function setLogger(LoggerInterface $logger): void;
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Doctrine\Common\Filter;
15+
16+
use Psr\Log\LoggerInterface;
17+
use Psr\Log\NullLogger;
18+
19+
trait LoggerAwareTrait
20+
{
21+
private ?LoggerInterface $logger = null;
22+
23+
public function hasLogger(): bool
24+
{
25+
return $this->logger instanceof LoggerInterface;
26+
}
27+
28+
public function getLogger(): LoggerInterface
29+
{
30+
return $this->logger ??= new NullLogger();
31+
}
32+
33+
public function setLogger(LoggerInterface $logger): void
34+
{
35+
$this->logger = $logger;
36+
}
37+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Doctrine\Common\Filter;
15+
16+
use ApiPlatform\Metadata\Exception\RuntimeException;
17+
use Doctrine\Persistence\ManagerRegistry;
18+
19+
trait ManagerRegistryAwareTrait
20+
{
21+
private ?ManagerRegistry $managerRegistry = null;
22+
23+
public function hasManagerRegistry(): bool
24+
{
25+
return $this->managerRegistry instanceof ManagerRegistry;
26+
}
27+
28+
public function getManagerRegistry(): ManagerRegistry
29+
{
30+
if (!$this->hasManagerRegistry()) {
31+
throw new RuntimeException('ManagerRegistry must be initialized before accessing it.');
32+
}
33+
34+
return $this->managerRegistry;
35+
}
36+
37+
public function setManagerRegistry(ManagerRegistry $managerRegistry): void
38+
{
39+
$this->managerRegistry = $managerRegistry;
40+
}
41+
}

src/Doctrine/Odm/Extension/ParameterExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Doctrine\Odm\Extension;
1515

16+
use ApiPlatform\Doctrine\Common\Filter\LoggerAwareInterface;
1617
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
1718
use ApiPlatform\Doctrine\Common\ParameterValueExtractorTrait;
1819
use ApiPlatform\Doctrine\Odm\Filter\AbstractFilter;
@@ -22,6 +23,7 @@
2223
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
2324
use Doctrine\ODM\MongoDB\Aggregation\Builder;
2425
use Psr\Container\ContainerInterface;
26+
use Psr\Log\LoggerInterface;
2527

2628
/**
2729
* Reads operation parameters and execute its filter.
@@ -35,6 +37,7 @@ final class ParameterExtension implements AggregationCollectionExtensionInterfac
3537
public function __construct(
3638
private readonly ContainerInterface $filterLocator,
3739
private readonly ?ManagerRegistry $managerRegistry = null,
40+
private readonly ?LoggerInterface $logger = null,
3841
) {
3942
}
4043

@@ -67,6 +70,10 @@ private function applyFilter(Builder $aggregationBuilder, ?string $resourceClass
6770
$filter->setManagerRegistry($this->managerRegistry);
6871
}
6972

73+
if ($this->logger && $filter instanceof LoggerAwareInterface && !$filter->hasLogger()) {
74+
$filter->setLogger($this->logger);
75+
}
76+
7077
if ($filter instanceof AbstractFilter && !$filter->getProperties()) {
7178
$propertyKey = $parameter->getProperty() ?? $parameter->getKey();
7279

src/Doctrine/Odm/Filter/IriFilter.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,63 @@
1313

1414
namespace ApiPlatform\Doctrine\Odm\Filter;
1515

16+
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
17+
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareTrait;
1618
use ApiPlatform\Doctrine\Common\Filter\OpenApiFilterTrait;
1719
use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait;
1820
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
1921
use ApiPlatform\Metadata\Operation;
2022
use ApiPlatform\Metadata\ParameterProviderFilterInterface;
2123
use ApiPlatform\State\ParameterProvider\IriConverterParameterProvider;
2224
use Doctrine\ODM\MongoDB\Aggregation\Builder;
25+
use Doctrine\ODM\MongoDB\DocumentManager;
2326

2427
/**
2528
* @author Vincent Amstoutz <[email protected]>
2629
*/
27-
final class IriFilter implements FilterInterface, OpenApiParameterFilterInterface, ParameterProviderFilterInterface
30+
final class IriFilter implements FilterInterface, OpenApiParameterFilterInterface, ParameterProviderFilterInterface, ManagerRegistryAwareInterface
2831
{
2932
use BackwardCompatibleFilterDescriptionTrait;
33+
use ManagerRegistryAwareTrait;
3034
use OpenApiFilterTrait;
3135

3236
public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
3337
{
3438
$parameter = $context['parameter'];
39+
$property = $parameter->getProperty();
3540
$value = $parameter->getValue();
3641

37-
$isIterable = is_iterable($value);
38-
if ($isIterable) {
39-
$ids = array_map(static fn (object $object) => $object->getId(), iterator_to_array($value));
40-
} else {
41-
$ids = \is_object($value) ? $value->getId() : $value;
42+
$documentManager = $this->getManagerRegistry()?->getManagerForClass($resourceClass);
43+
44+
if (!$documentManager instanceof DocumentManager) {
45+
return;
46+
}
47+
48+
$classMetadata = $documentManager->getClassMetadata($resourceClass);
49+
50+
if (!$classMetadata->hasReference($property)) {
51+
return;
52+
}
53+
54+
$method = $classMetadata->isCollectionValuedAssociation($property) ? 'includesReferenceTo' : 'references';
55+
56+
if (is_iterable($value)) {
57+
$match = $aggregationBuilder->match();
58+
$or = $match->expr();
59+
60+
foreach ($value as $v) {
61+
$or->addOr($match->expr()->field($property)->{$method}($v));
62+
}
63+
64+
$match->addAnd($or);
65+
66+
return;
4267
}
4368

4469
$aggregationBuilder
4570
->match()
46-
->field('id')
47-
->{$isIterable ? 'in' : 'equals'}($ids);
71+
->field($property)
72+
->{$method}($value);
4873
}
4974

5075
public static function getParameterProvider(): string

src/Doctrine/Orm/Extension/ParameterExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Doctrine\Orm\Extension;
1515

16+
use ApiPlatform\Doctrine\Common\Filter\LoggerAwareInterface;
1617
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
1718
use ApiPlatform\Doctrine\Common\ParameterValueExtractorTrait;
1819
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
@@ -23,6 +24,7 @@
2324
use Doctrine\ORM\QueryBuilder;
2425
use Doctrine\Persistence\ManagerRegistry;
2526
use Psr\Container\ContainerInterface;
27+
use Psr\Log\LoggerInterface;
2628

2729
/**
2830
* Reads operation parameters and execute its filter.
@@ -36,6 +38,7 @@ final class ParameterExtension implements QueryCollectionExtensionInterface, Que
3638
public function __construct(
3739
private readonly ContainerInterface $filterLocator,
3840
private readonly ?ManagerRegistry $managerRegistry = null,
41+
private readonly ?LoggerInterface $logger = null,
3942
) {
4043
}
4144

@@ -68,6 +71,10 @@ private function applyFilter(QueryBuilder $queryBuilder, QueryNameGeneratorInter
6871
$filter->setManagerRegistry($this->managerRegistry);
6972
}
7073

74+
if ($this->logger && $filter instanceof LoggerAwareInterface && !$filter->hasLogger()) {
75+
$filter->setLogger($this->logger);
76+
}
77+
7178
if ($filter instanceof AbstractFilter && !$filter->getProperties()) {
7279
$propertyKey = $parameter->getProperty() ?? $parameter->getKey();
7380

src/Symfony/Bundle/Resources/config/doctrine_mongodb_odm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
<service id="api_platform.doctrine_mongodb.odm.extension.parameter_extension" class="ApiPlatform\Doctrine\Odm\Extension\ParameterExtension" public="false">
139139
<argument type="service" id="api_platform.filter_locator" />
140140
<argument type="service" id="doctrine_mongodb" on-invalid="null"/>
141+
<argument type="service" id="logger" on-invalid="null"/>
141142
<tag name="api_platform.doctrine_mongodb.odm.aggregation_extension.collection" priority="32" />
142143
<tag name="api_platform.doctrine_mongodb.odm.aggregation_extension.item"/>
143144
</service>

src/Symfony/Bundle/Resources/config/doctrine_orm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
<service id="api_platform.doctrine.orm.extension.parameter_extension" class="ApiPlatform\Doctrine\Orm\Extension\ParameterExtension" public="false">
152152
<argument type="service" id="api_platform.filter_locator" />
153153
<argument type="service" id="doctrine" on-invalid="null"/>
154+
<argument type="service" id="logger" on-invalid="null"/>
154155
<tag name="api_platform.doctrine.orm.query_extension.collection" priority="-16" />
155156
<tag name="api_platform.doctrine.orm.query_extension.item" priority="-9" />
156157
</service>

0 commit comments

Comments
 (0)