/
ItemDataProvider.php
165 lines (138 loc) · 6.05 KB
/
ItemDataProvider.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?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.
*/
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\PropertyNotFoundException;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
/**
* Item data provider for the Doctrine ORM.
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
class ItemDataProvider implements ItemDataProviderInterface
{
private $managerRegistry;
private $propertyNameCollectionFactory;
private $propertyMetadataFactory;
private $itemExtensions;
/**
* @param ManagerRegistry $managerRegistry
* @param PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory
* @param PropertyMetadataFactoryInterface $propertyMetadataFactory
* @param QueryItemExtensionInterface[] $itemExtensions
*/
public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions = [])
{
$this->managerRegistry = $managerRegistry;
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
$this->propertyMetadataFactory = $propertyMetadataFactory;
$this->itemExtensions = $itemExtensions;
}
/**
* {@inheritdoc}
*
* @throws RuntimeException
*/
public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false)
{
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
if (null === $manager) {
throw new ResourceClassNotSupportedException();
}
$identifiers = $this->normalizeIdentifiers($id, $manager, $resourceClass);
if (!$fetchData && $manager instanceof EntityManagerInterface) {
return $manager->getReference($resourceClass, $identifiers);
}
$repository = $manager->getRepository($resourceClass);
if (!method_exists($repository, 'createQueryBuilder')) {
throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
}
$queryBuilder = $repository->createQueryBuilder('o');
$queryNameGenerator = new QueryNameGenerator();
$this->addWhereForIdentifiers($identifiers, $queryBuilder);
foreach ($this->itemExtensions as $extension) {
$extension->applyToItem($queryBuilder, $queryNameGenerator, $resourceClass, $identifiers, $operationName);
if ($extension instanceof QueryResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
}
}
return $queryBuilder->getQuery()->getOneOrNullResult();
}
/**
* Add WHERE conditions to the query for one or more identifiers (simple or composite).
*
* @param array $identifiers
* @param QueryBuilder $queryBuilder
*/
private function addWhereForIdentifiers(array $identifiers, QueryBuilder $queryBuilder)
{
foreach ($identifiers as $identifier => $value) {
$placeholder = ':id_'.$identifier;
$expression = $queryBuilder->expr()->eq(
'o.'.$identifier,
$placeholder
);
$queryBuilder->andWhere($expression);
$queryBuilder->setParameter($placeholder, $value);
}
}
/**
* Transform and check the identifier, composite or not.
*
* @param int|string $id
* @param ObjectManager $manager
* @param string $resourceClass
*
* @throws PropertyNotFoundException
*
* @return array
*/
private function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass) : array
{
$identifierValues = [$id];
$doctrineMetadataIdentifier = $manager->getClassMetadata($resourceClass)->getIdentifier();
if (count($doctrineMetadataIdentifier) >= 2) {
$identifiers = explode(';', $id);
$identifiersMap = [];
// first transform identifiers to a proper key/value array
foreach ($identifiers as $identifier) {
$keyValue = explode('=', $identifier);
$identifiersMap[$keyValue[0]] = $keyValue[1];
}
}
$identifiers = [];
$i = 0;
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
$identifier = $propertyMetadata->isIdentifier();
if (null === $identifier || false === $identifier) {
continue;
}
$identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;
if (null === $identifier) {
throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName));
}
$identifiers[$propertyName] = $identifier;
++$i;
}
return $identifiers;
}
}