|
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\Symfony\Routing; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
|
17
|
|
|
use ApiPlatform\Core\Api\OperationType; |
|
18
|
|
|
use ApiPlatform\Core\Api\UrlGeneratorInterface; |
|
19
|
|
|
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; |
|
20
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
|
21
|
|
|
use ApiPlatform\Core\Exception\ItemNotFoundException; |
|
22
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
|
23
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
|
24
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
|
25
|
|
|
use ApiPlatform\Core\Util\ClassInfoTrait; |
|
26
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
27
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
28
|
|
|
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface; |
|
29
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
* |
|
34
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
final class IriConverter implements IriConverterInterface |
|
37
|
|
|
{ |
|
38
|
|
|
use ClassInfoTrait; |
|
39
|
|
|
|
|
40
|
|
|
private $propertyNameCollectionFactory; |
|
41
|
|
|
private $propertyMetadataFactory; |
|
42
|
|
|
private $itemDataProvider; |
|
43
|
|
|
private $routeNameResolver; |
|
44
|
|
|
private $router; |
|
45
|
|
|
private $propertyAccessor; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
|
50
|
|
|
$this->propertyMetadataFactory = $propertyMetadataFactory; |
|
51
|
|
|
$this->itemDataProvider = $itemDataProvider; |
|
52
|
|
|
$this->routeNameResolver = $routeNameResolver; |
|
53
|
|
|
$this->router = $router; |
|
54
|
|
|
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getItemFromIri(string $iri, array $context = []) |
|
61
|
|
|
{ |
|
62
|
|
|
try { |
|
63
|
|
|
$parameters = $this->router->match($iri); |
|
64
|
|
|
} catch (RoutingExceptionInterface $e) { |
|
65
|
|
|
throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (!isset($parameters['_api_resource_class'], $parameters['id'])) { |
|
69
|
|
|
throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($item = $this->itemDataProvider->getItem($parameters['_api_resource_class'], $parameters['id'], null, $context)) { |
|
73
|
|
|
return $item; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
|
83
|
|
|
{ |
|
84
|
|
|
$resourceClass = $this->getObjectClass($item); |
|
85
|
|
|
$routeName = $this->routeNameResolver->getRouteName($resourceClass, false); |
|
86
|
|
|
|
|
87
|
|
|
$identifiers = $this->generateIdentifiersUrl($this->getIdentifiersFromItem($item)); |
|
88
|
|
|
|
|
89
|
|
|
return $this->router->generate($routeName, ['id' => implode(';', $identifiers)], $referenceType); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
try { |
|
98
|
|
|
return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::COLLECTION), [], $referenceType); |
|
99
|
|
|
} catch (RoutingExceptionInterface $e) { |
|
100
|
|
|
throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
View Code Duplication |
public function getItemIriFromResourceClass(string $resourceClass, array $identifiers, int $referenceType = UrlGeneratorInterface::ABS_PATH): string { |
|
|
|
|
|
|
108
|
|
|
try { |
|
109
|
|
|
return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::ITEM), $identifiers, $referenceType); |
|
110
|
|
|
} catch (RoutingExceptionInterface $e) { |
|
111
|
|
|
throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
View Code Duplication |
public function getSubresourceIriFromResourceClass(string $resourceClass, array $identifiers, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
try { |
|
121
|
|
|
return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::SUBRESOURCE), $identifiers, $referenceType); |
|
122
|
|
|
} catch (RoutingExceptionInterface $e) { |
|
123
|
|
|
throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Find identifiers from an Item (Object). |
|
129
|
|
|
* |
|
130
|
|
|
* @param object $item |
|
131
|
|
|
* |
|
132
|
|
|
* @throws RuntimeException |
|
133
|
|
|
* |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
private function getIdentifiersFromItem($item): array |
|
137
|
|
|
{ |
|
138
|
|
|
$identifiers = []; |
|
139
|
|
|
$resourceClass = $this->getObjectClass($item); |
|
140
|
|
|
|
|
141
|
|
|
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) { |
|
142
|
|
|
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
|
143
|
|
|
|
|
144
|
|
|
$identifier = $propertyMetadata->isIdentifier(); |
|
145
|
|
|
if (null === $identifier || false === $identifier) { |
|
146
|
|
|
continue; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName); |
|
150
|
|
|
|
|
151
|
|
|
if (!is_object($identifiers[$propertyName])) { |
|
152
|
|
|
continue; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$relatedResourceClass = $this->getObjectClass($identifiers[$propertyName]); |
|
156
|
|
|
$relatedItem = $identifiers[$propertyName]; |
|
157
|
|
|
|
|
158
|
|
|
unset($identifiers[$propertyName]); |
|
159
|
|
|
|
|
160
|
|
|
foreach ($this->propertyNameCollectionFactory->create($relatedResourceClass) as $relatedPropertyName) { |
|
161
|
|
|
$propertyMetadata = $this->propertyMetadataFactory->create($relatedResourceClass, $relatedPropertyName); |
|
162
|
|
|
|
|
163
|
|
|
if ($propertyMetadata->isIdentifier()) { |
|
164
|
|
|
if (isset($identifiers[$propertyName])) { |
|
165
|
|
|
throw new RuntimeException(sprintf('Composite identifiers not supported in "%s" through relation "%s" of "%s" used as identifier', $relatedResourceClass, $propertyName, $resourceClass)); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$identifiers[$propertyName] = $this->propertyAccessor->getValue($relatedItem, $relatedPropertyName); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if (!isset($identifiers[$propertyName])) { |
|
173
|
|
|
throw new RuntimeException(sprintf('No identifier found in "%s" through relation "%s" of "%s" used as identifier', $relatedResourceClass, $propertyName, $resourceClass)); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $identifiers; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Generate the identifier url. |
|
182
|
|
|
* |
|
183
|
|
|
* @param array $identifiers |
|
184
|
|
|
* |
|
185
|
|
|
* @return string[] |
|
186
|
|
|
*/ |
|
187
|
|
|
private function generateIdentifiersUrl(array $identifiers): array |
|
188
|
|
|
{ |
|
189
|
|
|
if (1 === count($identifiers)) { |
|
190
|
|
|
return [rawurlencode((string) array_values($identifiers)[0])]; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
foreach ($identifiers as $name => $value) { |
|
194
|
|
|
$identifiers[$name] = sprintf('%s=%s', $name, $value); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $identifiers; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.