/
RouterOperationPathResolver.php
52 lines (44 loc) · 1.54 KB
/
RouterOperationPathResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ApiPlatform\Core\Bridge\Symfony\Routing;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\PathResolver\OperationPathResolverInterface;
use Symfony\Component\Routing\RouterInterface;
/**
* Resolves the operations path using a Symfony route.
*
* @author Guilhem N. <egetick@gmail.com>
*/
final class RouterOperationPathResolver implements OperationPathResolverInterface
{
private $router;
private $deferred;
public function __construct(RouterInterface $router, OperationPathResolverInterface $deferred)
{
$this->router = $router;
$this->deferred = $deferred;
}
/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
*/
public function resolveOperationPath(string $resourceShortName, array $operation, bool $collection) : string
{
if (!isset($operation['route_name'])) {
return $this->deferred->resolveOperationPath($resourceShortName, $operation, $collection);
}
$route = $this->router->getRouteCollection()->get($operation['route_name']);
if (null === $route) {
throw new InvalidArgumentException(sprintf('The route "%s" of the resource "%s" was not found.', $operation['route_name'], $resourceShortName));
}
return $route->getPath();
}
}