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