|
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\OperationType; |
|
17
|
|
|
use ApiPlatform\Core\Api\OperationTypeDeprecationHelper; |
|
18
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
|
19
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
* |
|
24
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
final class RouteNameResolver implements RouteNameResolverInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private $router; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(RouterInterface $router) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->router = $router; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getRouteName(string $resourceClass, $operationType, array $context = []): string |
|
39
|
|
|
{ |
|
40
|
|
|
$operationType = OperationTypeDeprecationHelper::getOperationType($operationType); |
|
41
|
|
|
|
|
42
|
|
|
foreach ($this->router->getRouteCollection()->all() as $routeName => $route) { |
|
43
|
|
|
$currentResourceClass = $route->getDefault('_api_resource_class'); |
|
44
|
|
|
$operation = $route->getDefault(sprintf('_api_%s_operation_name', $operationType)); |
|
45
|
|
|
$methods = $route->getMethods(); |
|
46
|
|
|
|
|
47
|
|
|
if ($resourceClass === $currentResourceClass && null !== $operation && (empty($methods) || in_array('GET', $methods, true))) { |
|
48
|
|
|
if ($operationType === OperationType::SUBRESOURCE && false === $this->isSameSubresource($context, $route->getDefault('_api_subresource_context'))) { |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $routeName; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
throw new InvalidArgumentException(sprintf('No %s route associated with the type "%s".', $operationType, $resourceClass)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function isSameSubresource(array $context, array $currentContext): bool |
|
60
|
|
|
{ |
|
61
|
|
|
$subresources = array_keys($context['subresource_resources']); |
|
62
|
|
|
$currentSubresources = []; |
|
63
|
|
|
|
|
64
|
|
|
foreach ($currentContext['identifiers'] as $identiferContext) { |
|
65
|
|
|
$currentSubresources[] = $identiferContext[1]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($currentSubresources === $subresources) { |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|