1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\EventListener\Admin; |
6
|
|
|
|
7
|
|
|
use LAG\AdminBundle\Event\Events\ResourceCreateEvent; |
8
|
|
|
use LAG\AdminBundle\Metadata\AdminResource; |
9
|
|
|
use LAG\AdminBundle\Metadata\CollectionOperationInterface; |
10
|
|
|
use LAG\AdminBundle\Metadata\Create; |
11
|
|
|
use LAG\AdminBundle\Metadata\OperationInterface; |
12
|
|
|
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface; |
13
|
|
|
use Symfony\Component\String\Inflector\EnglishInflector; |
14
|
|
|
|
15
|
|
|
use function Symfony\Component\String\u; |
16
|
|
|
|
17
|
|
|
class CreateListener |
18
|
|
|
{ |
19
|
|
|
public function __construct( |
20
|
|
|
private RouteNameGeneratorInterface $routeNameGenerator, |
21
|
|
|
) { |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function __invoke(ResourceCreateEvent $event): void |
25
|
|
|
{ |
26
|
|
|
$resource = $event->getResource(); |
27
|
|
|
$operations = []; |
28
|
|
|
$resource = $this->addResourceDefault($resource); |
29
|
|
|
|
30
|
|
|
foreach ($resource->getOperations() as $operation) { |
31
|
|
|
$operation = $this->addOperationDefault($resource, $operation)->withResource($resource); |
32
|
|
|
$operations[$operation->getName()] = $operation; |
33
|
|
|
} |
34
|
|
|
$resource = $resource->withOperations($operations); |
35
|
|
|
$event->setResource($resource); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function addResourceDefault(AdminResource $resource): AdminResource |
39
|
|
|
{ |
40
|
|
|
if (!$resource->getTitle()) { |
41
|
|
|
$inflector = new EnglishInflector(); |
42
|
|
|
$resource = $resource->withTitle($inflector->pluralize($resource->getName())[0]); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($resource->getPrefix() === null) { |
|
|
|
|
46
|
|
|
$resource = $resource->withPrefix($resource->getName()); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $resource; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function addOperationDefault(AdminResource $resource, OperationInterface $operation): OperationInterface |
53
|
|
|
{ |
54
|
|
|
if (!$operation->getName()) { |
55
|
|
|
$operation = $operation->withName( |
56
|
|
|
u(\get_class($operation)) |
57
|
|
|
->afterLast('\\') |
58
|
|
|
->snake() |
59
|
|
|
->lower() |
60
|
|
|
->toString() |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!$operation->getResourceName()) { |
|
|
|
|
65
|
|
|
$operation = $operation->withResourceName($resource->getName()); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!$operation->getTitle()) { |
69
|
|
|
$operation = $operation->withTitle(match ($operation->getName()) { |
70
|
|
|
'index' => 'List', |
71
|
|
|
default => u($operation->getName())->title(true)->toString(), |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (!$operation->getRoute()) { |
76
|
|
|
$route = $this->routeNameGenerator->generateRouteName($resource, $operation); |
77
|
|
|
$operation = $operation->withRoute($route); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!$operation->getRouteParameters()) { |
81
|
|
|
if (!$operation instanceof CollectionOperationInterface && !$operation instanceof Create) { |
82
|
|
|
$operation = $operation->withRouteParameters(array_keys($operation->getIdentifiers())); |
83
|
|
|
} else { |
84
|
|
|
$operation = $operation->withRouteParameters([]); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $operation; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|