-
-
Notifications
You must be signed in to change notification settings - Fork 963
Expand file tree
/
Copy pathItemProvider.php
More file actions
106 lines (87 loc) · 4.38 KB
/
ItemProvider.php
File metadata and controls
106 lines (87 loc) · 4.38 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?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\Orm\State;
use ApiPlatform\Doctrine\Common\State\LinksHandlerLocatorTrait;
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Doctrine\Orm\Extension\QueryResultItemExtensionInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Metadata\Exception\RuntimeException;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\Util\StateOptionsTrait;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Container\ContainerInterface;
/**
* Item state provider using the Doctrine ORM.
*
* @author Kévin Dunglas <kevin@dunglas.fr>
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
final class ItemProvider implements ProviderInterface
{
use LinksHandlerLocatorTrait;
use LinksHandlerTrait;
use StateOptionsTrait;
/**
* @param QueryItemExtensionInterface[] $itemExtensions
*/
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, ManagerRegistry $managerRegistry, private readonly iterable $itemExtensions = [], ?ContainerInterface $handleLinksLocator = null)
{
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
$this->handleLinksLocator = $handleLinksLocator;
$this->managerRegistry = $managerRegistry;
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
{
$entityClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
/** @var EntityManagerInterface|null $manager */
$manager = $this->managerRegistry->getManagerForClass($entityClass);
if (null === $manager) {
throw new RuntimeException(\sprintf('No manager found for class "%s". Are you sure it\'s an entity?', $entityClass));
}
$fetchData = $context['fetch_data'] ?? true;
if (!$fetchData && \array_key_exists('id', $uriVariables)) {
// todo : if uriVariables don't contain the id, this fails. This should behave like it does in the following code
return $manager->getReference($entityClass, $uriVariables);
}
$repository = $manager->getRepository($entityClass);
if ($method = $this->getStateOptionsRepositoryMethod($operation)) {
if (!method_exists($repository, $method)) {
throw new RuntimeException(\sprintf('The repository method "%s::%s" does not exist.', $repository::class, $method));
}
$queryBuilder = $repository->{$method}();
if (!$queryBuilder instanceof QueryBuilder) {
throw new RuntimeException(\sprintf('The repository method "%s" must return a %s instance.', $method, QueryBuilder::class));
}
} else {
if (!method_exists($repository, 'createQueryBuilder')) {
throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
}
$queryBuilder = $repository->createQueryBuilder('o');
}
$queryNameGenerator = new QueryNameGenerator();
if ($handleLinks = $this->getLinksHandler($operation)) {
$handleLinks($queryBuilder, $uriVariables, $queryNameGenerator, ['entityClass' => $entityClass, 'operation' => $operation] + $context);
} else {
$this->handleLinks($queryBuilder, $uriVariables, $queryNameGenerator, $context, $entityClass, $operation);
}
foreach ($this->itemExtensions as $extension) {
$extension->applyToItem($queryBuilder, $queryNameGenerator, $entityClass, $uriVariables, $operation, $context);
if ($extension instanceof QueryResultItemExtensionInterface && $extension->supportsResult($entityClass, $operation, $context)) {
return $extension->getResult($queryBuilder, $entityClass, $operation, $context);
}
}
return $queryBuilder->getQuery()->getOneOrNullResult();
}
}