|
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\MongoDbOdm; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationCollectionExtensionInterface; |
|
17
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationResultCollectionExtensionInterface; |
|
18
|
|
|
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; |
|
19
|
|
|
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; |
|
20
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
|
21
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
22
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
23
|
|
|
use Doctrine\ODM\MongoDB\Repository\DocumentRepository; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Collection data provider for the Doctrine MongoDB ODM. |
|
27
|
|
|
* |
|
28
|
|
|
* @experimental |
|
29
|
|
|
* |
|
30
|
|
|
* @author Alan Poulain <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
final class CollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface |
|
33
|
|
|
{ |
|
34
|
|
|
private $managerRegistry; |
|
35
|
|
|
private $collectionExtensions; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param AggregationCollectionExtensionInterface[] $collectionExtensions |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(ManagerRegistry $managerRegistry, iterable $collectionExtensions = []) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->managerRegistry = $managerRegistry; |
|
43
|
|
|
$this->collectionExtensions = $collectionExtensions; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->managerRegistry->getManagerForClass($resourceClass) instanceof DocumentManager; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
* |
|
54
|
|
|
* @throws RuntimeException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getCollection(string $resourceClass, string $operationName = null, array $context = []) |
|
57
|
|
|
{ |
|
58
|
|
|
/** @var DocumentManager $manager */ |
|
59
|
|
|
$manager = $this->managerRegistry->getManagerForClass($resourceClass); |
|
60
|
|
|
|
|
61
|
|
|
$repository = $manager->getRepository($resourceClass); |
|
62
|
|
|
if (!$repository instanceof DocumentRepository) { |
|
63
|
|
|
throw new RuntimeException(sprintf('The repository for "%s" must be an instance of "%s".', $resourceClass, DocumentRepository::class)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$aggregationBuilder = $repository->createAggregationBuilder(); |
|
67
|
|
|
foreach ($this->collectionExtensions as $extension) { |
|
68
|
|
|
$extension->applyToCollection($aggregationBuilder, $resourceClass, $operationName, $context); |
|
69
|
|
|
|
|
70
|
|
|
if ($extension instanceof AggregationResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) { |
|
71
|
|
|
return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $aggregationBuilder->hydrate($resourceClass)->execute(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|