1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Controller; |
6
|
|
|
|
7
|
|
|
use LAG\AdminBundle\Metadata\AdminResource; |
8
|
|
|
use LAG\AdminBundle\Request\Context\ContextProviderInterface; |
9
|
|
|
use LAG\AdminBundle\Request\Uri\UriVariablesExtractorInterface; |
10
|
|
|
use LAG\AdminBundle\Routing\UrlGenerator\UrlGeneratorInterface; |
11
|
|
|
use LAG\AdminBundle\State\DataProcessorInterface; |
12
|
|
|
use LAG\AdminBundle\State\DataProviderInterface; |
13
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Twig\Environment; |
18
|
|
|
|
19
|
|
|
class ItemOperationController |
20
|
|
|
{ |
21
|
|
|
public function __construct( |
22
|
|
|
private UriVariablesExtractorInterface $uriVariablesExtractor, |
23
|
|
|
private ContextProviderInterface $contextProvider, |
24
|
|
|
private DataProviderInterface $dataProvider, |
25
|
|
|
private DataProcessorInterface $dataProcessor, |
26
|
|
|
private FormFactoryInterface $formFactory, |
27
|
|
|
private Environment $environment, |
28
|
|
|
private UrlGeneratorInterface $urlGenerator, |
29
|
|
|
) { |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function __invoke(Request $request, AdminResource $resource): Response |
33
|
|
|
{ |
34
|
|
|
$operation = $resource->getCurrentOperation(); |
35
|
|
|
$uriVariables = $this->uriVariablesExtractor->extractVariables($operation, $request); |
36
|
|
|
$context = $this->contextProvider->getContext($operation, $request); |
37
|
|
|
$data = $this->dataProvider->provide($operation, $uriVariables, $context); |
38
|
|
|
$form = null; |
39
|
|
|
|
40
|
|
|
if ($operation->getFormType()) { |
41
|
|
|
$form = $this->formFactory->create($operation->getFormType(), $data, $operation->getFormOptions()); |
42
|
|
|
$form->handleRequest($request); |
43
|
|
|
|
44
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
45
|
|
|
$data = $form->getData(); |
46
|
|
|
$this->dataProcessor->process($data, $operation, $uriVariables, $context); |
47
|
|
|
|
48
|
|
|
return new RedirectResponse($this->urlGenerator->generateFromRouteName( |
49
|
|
|
$operation->getTargetRoute() ?? $operation->getRoute(), |
|
|
|
|
50
|
|
|
$operation->getTargetRouteParameters() ?? $operation->getRouteParameters(), |
|
|
|
|
51
|
|
|
)); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return new Response($this->environment->render($operation->getTemplate(), [ |
56
|
|
|
'resource' => $resource, |
57
|
|
|
'operation' => $operation, |
58
|
|
|
'data' => $data, |
59
|
|
|
'form' => $form?->createView(), |
60
|
|
|
])); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|