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
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Routing; |
13
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
15
|
|
|
use ApiPlatform\Core\Api\UrlGeneratorInterface; |
16
|
|
|
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; |
17
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
18
|
|
|
use ApiPlatform\Core\Exception\ItemNotFoundException; |
19
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
20
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
21
|
|
|
use ApiPlatform\Core\Metadata\Util\ItemIdentifiersExtractor; |
22
|
|
|
use ApiPlatform\Core\Util\ClassInfoTrait; |
23
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
24
|
|
|
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface; |
25
|
|
|
use Symfony\Component\Routing\RouterInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
* |
30
|
|
|
* @author Kévin Dunglas <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
final class IriConverter implements IriConverterInterface |
33
|
|
|
{ |
34
|
|
|
use ClassInfoTrait; |
35
|
|
|
|
36
|
|
|
private $itemDataProvider; |
37
|
|
|
private $routeNameResolver; |
38
|
|
|
private $router; |
39
|
|
|
private $itemIdentifiersExtractor; |
40
|
|
|
|
41
|
|
|
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null) |
42
|
|
|
{ |
43
|
|
|
$this->itemDataProvider = $itemDataProvider; |
44
|
|
|
$this->routeNameResolver = $routeNameResolver; |
45
|
|
|
$this->router = $router; |
46
|
|
|
|
47
|
|
|
$this->itemIdentifiersExtractor = new ItemIdentifiersExtractor($propertyNameCollectionFactory, $propertyMetadataFactory, $propertyAccessor); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getItemFromIri(string $iri, array $context = []) |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
$parameters = $this->router->match($iri); |
57
|
|
|
} catch (RoutingExceptionInterface $e) { |
58
|
|
|
throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!isset($parameters['_api_resource_class'], $parameters['id'])) { |
62
|
|
|
throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($item = $this->itemDataProvider->getItem($parameters['_api_resource_class'], $parameters['id'], null, $context)) { |
66
|
|
|
return $item; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
76
|
|
|
{ |
77
|
|
|
$resourceClass = $this->getObjectClass($item); |
78
|
|
|
$routeName = $this->routeNameResolver->getRouteName($resourceClass, false); |
79
|
|
|
|
80
|
|
|
$identifiers = $this->generateIdentifiersUrl($this->itemIdentifiersExtractor->getIdentifiersFromItem($item)); |
81
|
|
|
|
82
|
|
|
return $this->router->generate($routeName, ['id' => implode(';', $identifiers)], $referenceType); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
89
|
|
|
{ |
90
|
|
|
try { |
91
|
|
|
return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, true), [], $referenceType); |
92
|
|
|
} catch (RoutingExceptionInterface $e) { |
93
|
|
|
throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Generate the identifier url. |
99
|
|
|
* |
100
|
|
|
* @param array $identifiers |
101
|
|
|
* |
102
|
|
|
* @return string[] |
103
|
|
|
*/ |
104
|
|
|
private function generateIdentifiersUrl(array $identifiers): array |
105
|
|
|
{ |
106
|
|
|
if (1 === count($identifiers)) { |
107
|
|
|
return [rawurlencode(array_values($identifiers)[0])]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
foreach ($identifiers as $name => $value) { |
111
|
|
|
$identifiers[$name] = sprintf('%s=%s', $name, $value); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $identifiers; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|