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