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\Orm; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; |
17
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface; |
18
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator; |
19
|
|
|
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface; |
20
|
|
|
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; |
21
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
22
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
23
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Collection data provider for the Doctrine ORM. |
27
|
|
|
* |
28
|
|
|
* @author Kévin Dunglas <[email protected]> |
29
|
|
|
* @author Samuel ROZE <[email protected]> |
30
|
|
|
* @final |
31
|
|
|
*/ |
32
|
|
|
class CollectionDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface |
33
|
|
|
{ |
34
|
|
|
private $managerRegistry; |
35
|
|
|
private $collectionExtensions; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param QueryCollectionExtensionInterface[] $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 EntityManagerInterface; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
* |
54
|
|
|
* @throws RuntimeException |
55
|
|
|
*/ |
56
|
|
|
public function getCollection(string $resourceClass, string $operationName = null, array $context = []) |
57
|
|
|
{ |
58
|
|
|
/** @var EntityManagerInterface $manager */ |
59
|
|
|
$manager = $this->managerRegistry->getManagerForClass($resourceClass); |
60
|
|
|
|
61
|
|
|
$repository = $manager->getRepository($resourceClass); |
62
|
|
|
if (!method_exists($repository, 'createQueryBuilder')) { |
63
|
|
|
throw new RuntimeException('The repository class must have a "createQueryBuilder" method.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$queryBuilder = $repository->createQueryBuilder('o'); |
67
|
|
|
$queryNameGenerator = new QueryNameGenerator(); |
68
|
|
|
foreach ($this->collectionExtensions as $extension) { |
69
|
|
|
$extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context); |
70
|
|
|
|
71
|
|
|
if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) { |
|
|
|
|
72
|
|
|
return $extension->getResult($queryBuilder, $resourceClass, $operationName, $context); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $queryBuilder->getQuery()->getResult(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.