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\PHPCR; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\PHPCR\Extension\QueryCollectionExtensionInterface; |
17
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\PHPCR\Extension\QueryResultExtensionInterface; |
18
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\PHPCR\Util\QueryNameGenerator; |
19
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\ContextAwareQueryCollectionExtensionInterface; |
20
|
|
|
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; |
21
|
|
|
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; |
22
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
23
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
24
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Builder; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Collection data provider for the Doctrine MongoDB ODM. |
28
|
|
|
*/ |
29
|
|
|
class CollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface |
30
|
|
|
{ |
31
|
|
|
private $managerRegistry; |
32
|
|
|
private $collectionExtensions; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param QueryCollectionExtensionInterface[]|ContextAwareQueryCollectionExtensionInterface[] $collectionExtensions |
36
|
|
|
*/ |
37
|
|
|
public function __construct(ManagerRegistry $managerRegistry, /* iterable */ $collectionExtensions = []) |
38
|
|
|
{ |
39
|
|
|
$this->managerRegistry = $managerRegistry; |
40
|
|
|
$this->collectionExtensions = $collectionExtensions; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool |
44
|
|
|
{ |
45
|
|
|
return null !== $this->managerRegistry->getManagerForClass($resourceClass); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
* |
51
|
|
|
* @throws RuntimeException |
52
|
|
|
*/ |
53
|
|
|
public function getCollection(string $resourceClass, string $operationName = null, array $context = []) |
54
|
|
|
{ |
55
|
|
|
$manager = $this->managerRegistry->getManagerForClass($resourceClass); |
56
|
|
|
|
57
|
|
|
$repository = $manager->getRepository($resourceClass); |
58
|
|
|
if (!method_exists($repository, 'createAggregationBuilder')) { |
59
|
|
|
throw new RuntimeException('The repository class must have a "createAggregationBuilder" method.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @var Builder $aggregationBuilder */ |
63
|
|
|
$aggregationBuilder = $repository->createAggregationBuilder(); |
64
|
|
|
$queryNameGenerator = new QueryNameGenerator(); |
65
|
|
|
foreach ($this->collectionExtensions as $extension) { |
66
|
|
|
$extension->applyToCollection($aggregationBuilder, $queryNameGenerator, $resourceClass, $operationName, $context); |
67
|
|
|
|
68
|
|
|
if ($extension instanceof QueryResultExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) { |
|
|
|
|
69
|
|
|
return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $aggregationBuilder->hydrate($resourceClass)->execute()->toArray(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.