|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Routing\UrlGenerator; |
|
6
|
|
|
|
|
7
|
|
|
use LAG\AdminBundle\Metadata\AdminResource; |
|
8
|
|
|
use LAG\AdminBundle\Metadata\OperationInterface; |
|
9
|
|
|
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface; |
|
10
|
|
|
use LAG\AdminBundle\Routing\Parameter\ParametersMapper; |
|
11
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
12
|
|
|
use Symfony\Component\String\Inflector\EnglishInflector; |
|
13
|
|
|
|
|
14
|
|
|
use function Symfony\Component\String\u; |
|
15
|
|
|
|
|
16
|
|
|
class UrlGenerator implements UrlGeneratorInterface |
|
17
|
|
|
{ |
|
18
|
|
|
public function __construct( |
|
19
|
|
|
private RouterInterface $router, |
|
20
|
|
|
private ResourceRegistryInterface $resourceRegistry, |
|
21
|
|
|
) { |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function generatePath( |
|
25
|
|
|
AdminResource $resource, |
|
26
|
|
|
OperationInterface $operation, |
|
27
|
|
|
): string { |
|
28
|
|
|
$resource = $operation->getResource(); |
|
29
|
|
|
$resourceName = (new EnglishInflector())->pluralize($resource->getName())[0]; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
$path = u($resource->getPrefix()) |
|
|
|
|
|
|
32
|
|
|
->replace('{resourceName}', $resourceName) |
|
33
|
|
|
; |
|
34
|
|
|
|
|
35
|
|
|
foreach ($operation->getRouteParameters() as $parameter => $requirement) { |
|
36
|
|
|
$path = $path |
|
37
|
|
|
->append('/') |
|
38
|
|
|
->append('{'.$parameter.'}') |
|
39
|
|
|
; |
|
40
|
|
|
} |
|
41
|
|
|
$operationPath = u($operation->getPath()); |
|
42
|
|
|
|
|
43
|
|
|
if ($operationPath->length() > 0) { |
|
44
|
|
|
$operationPath = $operationPath->ensureStart('/'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $path |
|
48
|
|
|
->append($operationPath->toString()) |
|
49
|
|
|
->toString() |
|
50
|
|
|
; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function generateFromRouteName(string $routeName, array $routeParameters = [], mixed $data = null): string |
|
54
|
|
|
{ |
|
55
|
|
|
$mappedRouteParameters = $routeParameters; |
|
56
|
|
|
|
|
57
|
|
|
if ($data !== null) { |
|
58
|
|
|
$mapper = new ParametersMapper(); |
|
59
|
|
|
$mappedRouteParameters = $mapper->map($data, $routeParameters); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this->router->generate($routeName, $mappedRouteParameters); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function generateFromOperationName(string $resourceName, string $operationName, mixed $data = null): string |
|
66
|
|
|
{ |
|
67
|
|
|
$resource = $this->resourceRegistry->get($resourceName); |
|
68
|
|
|
$operation = $resource->getOperation($operationName); |
|
69
|
|
|
|
|
70
|
|
|
return $this->generateFromRouteName( |
|
71
|
|
|
$operation->getRoute(), |
|
|
|
|
|
|
72
|
|
|
array_keys($operation->getRouteParameters()), |
|
|
|
|
|
|
73
|
|
|
$data, |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|