1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace FSi\Bundle\AdminBundle\Admin\Context; |
13
|
|
|
|
14
|
|
|
use FSi\Bundle\AdminBundle\Admin\Context\Request\HandlerInterface; |
15
|
|
|
use FSi\Bundle\AdminBundle\Admin\Element; |
16
|
|
|
use FSi\Bundle\AdminBundle\Event\AdminEvent; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
|
20
|
|
|
abstract class ContextAbstract implements ContextInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var HandlerInterface[] |
24
|
|
|
*/ |
25
|
|
|
private $requestHandlers; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
private $template; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param HandlerInterface[] $requestHandlers |
34
|
|
|
* @param string|null |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $requestHandlers, ?string $template = null) |
37
|
|
|
{ |
38
|
|
|
$this->requestHandlers = $requestHandlers; |
39
|
|
|
$this->template = $template; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function supports(string $route, Element $element): bool |
43
|
|
|
{ |
44
|
|
|
if ($route !== $this->getSupportedRoute()) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->supportsElement($element); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function handleRequest(Request $request): ?Response |
52
|
|
|
{ |
53
|
|
|
$event = $this->createEvent($request); |
54
|
|
|
|
55
|
|
|
foreach ($this->requestHandlers as $handler) { |
56
|
|
|
$response = $handler->handleRequest($event, $request); |
57
|
|
|
if (null !== $response) { |
58
|
|
|
return $response; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function hasTemplateName(): bool |
66
|
|
|
{ |
67
|
|
|
return null !== $this->template; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getTemplateName(): ?string |
71
|
|
|
{ |
72
|
|
|
return $this->template; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
abstract protected function getSupportedRoute(): string; |
76
|
|
|
|
77
|
|
|
abstract protected function supportsElement(Element $element): bool; |
78
|
|
|
|
79
|
|
|
abstract protected function createEvent(Request $request): AdminEvent; |
80
|
|
|
} |
81
|
|
|
|