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\Exception\InvalidArgumentException; |
15
|
|
|
use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; |
16
|
|
|
use Symfony\Component\Routing\RequestContext; |
17
|
|
|
use Symfony\Component\Routing\RouterInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Resolves the operations path using a symfony route. |
21
|
|
|
* |
22
|
|
|
* @author Guilhem N. <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class RouterOperationPathResolver implements OperationPathResolverInterface |
25
|
|
|
{ |
26
|
|
|
private $router; |
27
|
|
|
private $requestContext; |
28
|
|
|
private $deferred; |
29
|
|
|
|
30
|
|
|
public function __construct(RouterInterface $router, RequestContext $requestContext, OperationPathResolverInterface $deferred) |
31
|
|
|
{ |
32
|
|
|
$this->router = $router; |
33
|
|
|
$this->requestContext = $requestContext; |
34
|
|
|
$this->deferred = $deferred; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function resolveOperationPath(string $resourceShortName, array $operation, bool $collection) : string |
41
|
|
|
{ |
42
|
|
|
if (!isset($operation['route_name'])) { |
43
|
|
|
return $this->deferred->resolveOperationPath($resourceShortName, $operation, $collection); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$route = $this->router->getRouteCollection()->get($operation['route_name']); |
47
|
|
|
if (null === $route) { |
48
|
|
|
throw new InvalidArgumentException(sprintf('The route "%s" of the resource "%s" was not found.', $operation['route_name'], $resourceShortName)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $route->getPath(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getContext(): RequestContext |
55
|
|
|
{ |
56
|
|
|
return $this->requestContext; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|