These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
||
26 | use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
||
27 | use ApiPlatform\Core\Util\ClassInfoTrait; |
||
28 | use Symfony\Component\PropertyAccess\PropertyAccess; |
||
29 | use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
||
30 | use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface; |
||
31 | use Symfony\Component\Routing\RouterInterface; |
||
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | * |
||
36 | * @author Kévin Dunglas <[email protected]> |
||
37 | */ |
||
38 | final class IriConverter implements IriConverterInterface |
||
39 | { |
||
40 | use ClassInfoTrait; |
||
41 | |||
42 | private $propertyNameCollectionFactory; |
||
43 | private $propertyMetadataFactory; |
||
44 | private $itemDataProvider; |
||
45 | private $routeNameResolver; |
||
46 | private $router; |
||
47 | private $propertyAccessor; |
||
48 | private $identifiersExtractor; |
||
49 | |||
50 | public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null, IdentifiersExtractorInterface $identifiersExtractor = null) |
||
51 | { |
||
52 | $this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
||
53 | $this->propertyMetadataFactory = $propertyMetadataFactory; |
||
54 | $this->itemDataProvider = $itemDataProvider; |
||
55 | $this->routeNameResolver = $routeNameResolver; |
||
56 | $this->router = $router; |
||
57 | $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor(); |
||
58 | |||
59 | if (null === $identifiersExtractor) { |
||
60 | @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); |
||
61 | $this->identifiersExtractor = new IdentifiersExtractor($this->propertyNameCollectionFactory, $this->propertyMetadataFactory, $this->propertyAccessor); |
||
62 | } else { |
||
63 | $this->identifiersExtractor = $identifiersExtractor; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function getItemFromIri(string $iri, array $context = []) |
||
71 | { |
||
72 | try { |
||
73 | $parameters = $this->router->match($iri); |
||
74 | } catch (RoutingExceptionInterface $e) { |
||
75 | throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e); |
||
76 | } |
||
77 | |||
78 | if (!isset($parameters['_api_resource_class'], $parameters['id'])) { |
||
79 | throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri)); |
||
80 | } |
||
81 | |||
82 | if ($item = $this->itemDataProvider->getItem($parameters['_api_resource_class'], $parameters['id'], null, $context)) { |
||
83 | return $item; |
||
84 | } |
||
85 | |||
86 | throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri)); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
||
93 | { |
||
94 | $resourceClass = $this->getObjectClass($item); |
||
95 | $routeName = $this->routeNameResolver->getRouteName($resourceClass, OperationType::ITEM); |
||
96 | |||
97 | try { |
||
98 | $identifiers = $this->generateIdentifiersUrl($this->identifiersExtractor->getIdentifiersFromItem($item)); |
||
99 | |||
100 | return $this->router->generate($routeName, ['id' => implode(';', $identifiers)], $referenceType); |
||
101 | } catch (RuntimeException $e) { |
||
102 | throw new InvalidArgumentException(sprintf( |
||
103 | 'Unable to generate an IRI for the item of type "%s"', |
||
104 | $resourceClass |
||
105 | ), $e->getCode(), $e); |
||
106 | } catch (RoutingExceptionInterface $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 | } |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | View Code Duplication | public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
|
118 | { |
||
119 | try { |
||
120 | return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::COLLECTION), [], $referenceType); |
||
121 | } catch (RoutingExceptionInterface $e) { |
||
122 | throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | View Code Duplication | public function getItemIriFromResourceClass(string $resourceClass, array $identifiers, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
|
130 | { |
||
131 | try { |
||
132 | return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::ITEM), $identifiers, $referenceType); |
||
133 | } catch (RoutingExceptionInterface $e) { |
||
134 | throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | View Code Duplication | public function getSubresourceIriFromResourceClass(string $resourceClass, array $context, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
|
142 | { |
||
143 | try { |
||
144 | return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::SUBRESOURCE, $context), $context['subresource_identifiers'], $referenceType); |
||
0 ignored issues
–
show
|
|||
145 | } catch (RoutingExceptionInterface $e) { |
||
146 | throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Generate the identifier url. |
||
152 | * |
||
153 | * @param array $identifiers |
||
154 | * |
||
155 | * @return string[] |
||
156 | */ |
||
157 | private function generateIdentifiersUrl(array $identifiers): array |
||
158 | { |
||
159 | if (1 === count($identifiers)) { |
||
160 | return [rawurlencode((string) array_values($identifiers)[0])]; |
||
161 | } |
||
162 | |||
163 | foreach ($identifiers as $name => $value) { |
||
164 | $identifiers[$name] = sprintf('%s=%s', $name, $value); |
||
165 | } |
||
166 | |||
167 | return $identifiers; |
||
168 | } |
||
169 | } |
||
170 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.