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\IdentifiersExtractor; |
17
|
|
|
use ApiPlatform\Core\Api\IdentifiersExtractorInterface; |
18
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
19
|
|
|
use ApiPlatform\Core\Api\OperationType; |
20
|
|
|
use ApiPlatform\Core\Api\UrlGeneratorInterface; |
21
|
|
|
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; |
22
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
23
|
|
|
use ApiPlatform\Core\Exception\ItemNotFoundException; |
24
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
25
|
|
|
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer; |
26
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
27
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
28
|
|
|
use ApiPlatform\Core\Util\ClassInfoTrait; |
29
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
30
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
31
|
|
|
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface; |
32
|
|
|
use Symfony\Component\Routing\RouterInterface; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
* |
37
|
|
|
* @author Kévin Dunglas <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
final class IriConverter implements IriConverterInterface |
40
|
|
|
{ |
41
|
|
|
use ClassInfoTrait; |
42
|
|
|
|
43
|
|
|
private $itemDataProvider; |
44
|
|
|
private $routeNameResolver; |
45
|
|
|
private $router; |
46
|
|
|
private $identifiersExtractor; |
47
|
|
|
private $identifierDenormalizer; |
48
|
|
|
|
49
|
|
|
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null, IdentifiersExtractorInterface $identifiersExtractor = null, ChainIdentifierDenormalizer $identifierDenormalizer = null) |
50
|
|
|
{ |
51
|
|
|
$this->itemDataProvider = $itemDataProvider; |
52
|
|
|
$this->routeNameResolver = $routeNameResolver; |
53
|
|
|
$this->router = $router; |
54
|
|
|
$this->identifierDenormalizer = $identifierDenormalizer; |
55
|
|
|
|
56
|
|
|
if (null === $identifiersExtractor) { |
57
|
|
|
@trigger_error('Not injecting ItemIdentifiersExtractor is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3', E_USER_DEPRECATED); |
58
|
|
|
$this->identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactory, $propertyMetadataFactory, $propertyAccessor ?? PropertyAccess::createPropertyAccessor()); |
59
|
|
|
} else { |
60
|
|
|
$this->identifiersExtractor = $identifiersExtractor; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (null === $identifierDenormalizer) { |
64
|
|
|
@trigger_error(sprintf('Not injecting "%s" is deprecated since API Platform 2.2 and will not be possible anymore in API Platform 3.', ChainIdentifierDenormalizer::class), E_USER_DEPRECATED); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function getItemFromIri(string $iri, array $context = []) |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
$parameters = $this->router->match($iri); |
75
|
|
|
} catch (RoutingExceptionInterface $e) { |
76
|
|
|
throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!isset($parameters['_api_resource_class'], $parameters['id'])) { |
80
|
|
|
throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$identifiers = $parameters['id']; |
84
|
|
|
|
85
|
|
|
if ($this->identifierDenormalizer) { |
86
|
|
|
$identifiers = $this->identifierDenormalizer->denormalize((string) $parameters['id'], $parameters['_api_resource_class']); |
87
|
|
|
$context[ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] = true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($item = $this->itemDataProvider->getItem($parameters['_api_resource_class'], $identifiers, null, $context)) { |
91
|
|
|
return $item; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function getIriFromPlainIdentifier(string $id, string $resourceClass, array $context = []): string |
101
|
|
|
{ |
102
|
|
|
return $this->getIriFromItem($this->itemDataProvider->getItem($resourceClass, $id, null, $context + ['fetch_data' => true])); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
109
|
|
|
{ |
110
|
|
|
$resourceClass = $this->getObjectClass($item); |
111
|
|
|
$routeName = $this->routeNameResolver->getRouteName($resourceClass, OperationType::ITEM); |
112
|
|
|
|
113
|
|
|
try { |
114
|
|
|
$identifiers = $this->generateIdentifiersUrl($this->identifiersExtractor->getIdentifiersFromItem($item)); |
115
|
|
|
|
116
|
|
|
return $this->router->generate($routeName, ['id' => implode(';', $identifiers)], $referenceType); |
117
|
|
|
} catch (RuntimeException $e) { |
118
|
|
|
throw new InvalidArgumentException(sprintf( |
119
|
|
|
'Unable to generate an IRI for the item of type "%s"', |
120
|
|
|
$resourceClass |
121
|
|
|
), $e->getCode(), $e); |
122
|
|
|
} catch (RoutingExceptionInterface $e) { |
123
|
|
|
throw new InvalidArgumentException(sprintf( |
124
|
|
|
'Unable to generate an IRI for the item of type "%s"', |
125
|
|
|
$resourceClass |
126
|
|
|
), $e->getCode(), $e); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
134
|
|
|
{ |
135
|
|
|
try { |
136
|
|
|
return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::COLLECTION), [], $referenceType); |
137
|
|
|
} catch (RoutingExceptionInterface $e) { |
138
|
|
|
throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function getItemIriFromResourceClass(string $resourceClass, array $identifiers, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
146
|
|
|
{ |
147
|
|
|
try { |
148
|
|
|
return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::ITEM), $identifiers, $referenceType); |
149
|
|
|
} catch (RoutingExceptionInterface $e) { |
150
|
|
|
throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function getSubresourceIriFromResourceClass(string $resourceClass, array $context, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
158
|
|
|
{ |
159
|
|
|
try { |
160
|
|
|
return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::SUBRESOURCE, $context), $context['subresource_identifiers'], $referenceType); |
161
|
|
|
} catch (RoutingExceptionInterface $e) { |
162
|
|
|
throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Generate the identifier url. |
168
|
|
|
* |
169
|
|
|
* @param array $identifiers |
170
|
|
|
* |
171
|
|
|
* @return string[] |
172
|
|
|
*/ |
173
|
|
|
private function generateIdentifiersUrl(array $identifiers): array |
174
|
|
|
{ |
175
|
|
|
if (1 === \count($identifiers)) { |
176
|
|
|
return [rawurlencode((string) array_values($identifiers)[0])]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
foreach ($identifiers as $name => $value) { |
180
|
|
|
$identifiers[$name] = sprintf('%s=%s', $name, $value); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $identifiers; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|