-
-
Notifications
You must be signed in to change notification settings - Fork 893
/
Copy pathItemProvider.php
76 lines (61 loc) · 2.95 KB
/
ItemProvider.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
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Odm\Extension\AggregationItemExtensionInterface;
use ApiPlatform\Doctrine\Odm\Extension\AggregationResultItemExtensionInterface;
use ApiPlatform\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;
/**
* Item state provider using the Doctrine ODM.
*
* @author Kévin Dunglas <kevin@dunglas.fr>
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
final class ItemProvider implements ProviderInterface
{
use LinksHandlerTrait;
/**
* @param AggregationItemExtensionInterface[] $itemExtensions
*/
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ManagerRegistry $managerRegistry, private readonly iterable $itemExtensions = [])
{
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
{
$resourceClass = $operation->getClass();
/** @var DocumentManager $manager */
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
$fetchData = $context['fetch_data'] ?? true;
if (!$fetchData) {
return $manager->getReference($resourceClass, reset($uriVariables));
}
$repository = $manager->getRepository($resourceClass);
if (!$repository instanceof DocumentRepository) {
throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".', $resourceClass, DocumentRepository::class));
}
$aggregationBuilder = $repository->createAggregationBuilder();
$this->handleLinks($aggregationBuilder, $uriVariables, $context, $resourceClass, $operation);
foreach ($this->itemExtensions as $extension) {
$extension->applyToItem($aggregationBuilder, $resourceClass, $uriVariables, $operation, $context);
if ($extension instanceof AggregationResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operation, $context)) {
return $extension->getResult($aggregationBuilder, $resourceClass, $operation, $context);
}
}
$executeOptions = $operation->getExtraProperties()['doctrine_mongodb']['execute_options'] ?? [];
return $aggregationBuilder->hydrate($resourceClass)->execute($executeOptions)->current() ?: null;
}
}