-
-
Notifications
You must be signed in to change notification settings - Fork 900
/
Copy pathCollectionProvider.php
91 lines (75 loc) · 3.84 KB
/
CollectionProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Doctrine\Odm\State;
use ApiPlatform\Doctrine\Common\State\LinksHandlerLocatorTrait;
use ApiPlatform\Doctrine\Common\State\ModelTransformerLocatorTrait;
use ApiPlatform\Doctrine\Odm\Extension\AggregationCollectionExtensionInterface;
use ApiPlatform\Doctrine\Odm\Extension\AggregationResultCollectionExtensionInterface;
use ApiPlatform\Metadata\Exception\RuntimeException;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProviderInterface;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Container\ContainerInterface;
/**
* Collection state provider using the Doctrine ODM.
*/
final class CollectionProvider implements ProviderInterface
{
use LinksHandlerLocatorTrait;
use LinksHandlerTrait;
use ModelTransformerLocatorTrait;
/**
* @param AggregationCollectionExtensionInterface[] $collectionExtensions
*/
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, ManagerRegistry $managerRegistry, private readonly iterable $collectionExtensions = [], ?ContainerInterface $handleLinksLocator = null)
{
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
$this->handleLinksLocator = $handleLinksLocator;
$this->transformEntityLocator = $handleLinksLocator;
$this->managerRegistry = $managerRegistry;
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
{
$documentClass = $operation->getClass();
if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getDocumentClass()) {
$documentClass = $options->getDocumentClass();
}
/** @var DocumentManager $manager */
$manager = $this->managerRegistry->getManagerForClass($documentClass);
$repository = $manager->getRepository($documentClass);
if (!$repository instanceof DocumentRepository) {
throw new RuntimeException(\sprintf('The repository for "%s" must be an instance of "%s".', $documentClass, DocumentRepository::class));
}
$aggregationBuilder = $repository->createAggregationBuilder();
if ($handleLinks = $this->getLinksHandler($operation)) {
$handleLinks($aggregationBuilder, $uriVariables, ['documentClass' => $documentClass, 'operation' => $operation] + $context);
} else {
$this->handleLinks($aggregationBuilder, $uriVariables, $context, $documentClass, $operation);
}
foreach ($this->collectionExtensions as $extension) {
$extension->applyToCollection($aggregationBuilder, $documentClass, $operation, $context);
if ($extension instanceof AggregationResultCollectionExtensionInterface && $extension->supportsResult($documentClass, $operation, $context)) {
$result = $extension->getResult($aggregationBuilder, $documentClass, $operation, $context);
break;
}
}
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
$executeOptions = $attribute['execute_options'] ?? [];
$result = $result ?? $aggregationBuilder->hydrate($documentClass)->execute($executeOptions);
return match ($transformer = $this->getEntityTransformer($operation)) {
null => $result,
default => array_map($transformer, iterator_to_array($result)),
};
}
}