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\Core\Bridge\Doctrine\MongoDB; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\AggregationItemExtensionInterface; |
17
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\AggregationResultItemExtensionInterface; |
18
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\IdentifierManagerTrait; |
19
|
|
|
use ApiPlatform\Core\DataProvider\DenormalizedIdentifiersAwareItemDataProviderInterface; |
20
|
|
|
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; |
21
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
22
|
|
|
use ApiPlatform\Core\Identifier\IdentifierConverterInterface; |
23
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
24
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
25
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
26
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Builder; |
27
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Item data provider for the Doctrine MongoDB ODM. |
31
|
|
|
*/ |
32
|
|
|
final class ItemDataProvider implements DenormalizedIdentifiersAwareItemDataProviderInterface, RestrictedDataProviderInterface |
33
|
|
|
{ |
34
|
|
|
use IdentifierManagerTrait; |
35
|
|
|
|
36
|
|
|
private $managerRegistry; |
37
|
|
|
private $itemExtensions; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param AggregationItemExtensionInterface[] $itemExtensions |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, iterable $itemExtensions = []) |
43
|
|
|
{ |
44
|
|
|
$this->managerRegistry = $managerRegistry; |
45
|
|
|
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
46
|
|
|
$this->propertyMetadataFactory = $propertyMetadataFactory; |
47
|
|
|
$this->itemExtensions = $itemExtensions; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool |
51
|
|
|
{ |
52
|
|
|
return null !== $this->managerRegistry->getManagerForClass($resourceClass); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
* |
58
|
|
|
* @throws RuntimeException |
59
|
|
|
*/ |
60
|
|
|
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) |
61
|
|
|
{ |
62
|
|
|
$manager = $this->managerRegistry->getManagerForClass($resourceClass); |
63
|
|
|
|
64
|
|
|
if (!($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false)) { |
65
|
|
|
$id = $this->normalizeIdentifiers($id, $manager, $resourceClass); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$fetchData = $context['fetch_data'] ?? true; |
69
|
|
|
if (!$fetchData && $manager instanceof DocumentManager) { |
70
|
|
|
return $manager->getReference($resourceClass, reset($id)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$repository = $manager->getRepository($resourceClass); |
74
|
|
|
if (!method_exists($repository, 'createAggregationBuilder')) { |
75
|
|
|
throw new RuntimeException('The repository class must have a "createAggregationBuilder" method.'); |
76
|
|
|
} |
77
|
|
|
/** @var Builder $aggregationBuilder */ |
78
|
|
|
$aggregationBuilder = $repository->createAggregationBuilder(); |
79
|
|
|
|
80
|
|
|
foreach ($id as $propertyName => $value) { |
81
|
|
|
$aggregationBuilder->match()->field($propertyName)->equals($value); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
foreach ($this->itemExtensions as $extension) { |
85
|
|
|
$extension->applyToItem($aggregationBuilder, $resourceClass, $id, $operationName); |
86
|
|
|
|
87
|
|
|
if ($extension instanceof AggregationResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) { |
88
|
|
|
return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $aggregationBuilder->hydrate($resourceClass)->execute()->getSingleResult(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|