|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\ThemeBundle\EventSubscriber; |
|
15
|
|
|
|
|
16
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA; |
|
17
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire; |
|
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
|
22
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
23
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
24
|
|
|
use Zikula\ThemeBundle\Controller\Dashboard\UserDashboardController; |
|
25
|
|
|
|
|
26
|
|
|
class CreateThemedResponseSubscriber implements EventSubscriberInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private bool $installed; |
|
29
|
|
|
|
|
30
|
|
|
private bool $debug; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
private readonly AdminUrlGenerator $adminUrlGenerator, |
|
34
|
|
|
#[Autowire('%env(ZIKULA_INSTALLED)%')] |
|
35
|
|
|
string $installed, |
|
36
|
|
|
#[Autowire('%env(APP_DEBUG)%')] |
|
37
|
|
|
string $debug, |
|
38
|
|
|
) { |
|
39
|
|
|
$this->installed = '0.0.0' !== $installed; |
|
40
|
|
|
$this->debug = !empty($debug); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function getSubscribedEvents(): array |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
|
|
KernelEvents::REQUEST => ['createThemedResponse'], |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function createThemedResponse(RequestEvent $event): void |
|
51
|
|
|
{ |
|
52
|
|
|
$request = $event->getRequest(); |
|
53
|
|
|
if (!$event->isMainRequest() || $request->isXmlHttpRequest()) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
if (!$this->installed) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$route = $request->attributes->get('_route', ''); |
|
61
|
|
|
if (str_starts_with($route, 'home')) { // TODO remove hardcoded assumption -> check if controller is a dashboard |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$dashboard = UserDashboardController::class; // TODO $this->themeEngine->getActiveDashboardControllerClass(); |
|
66
|
|
|
$routeParameters = $request->attributes->get('_route_params'); |
|
67
|
|
|
|
|
68
|
|
|
// menu indexes |
|
69
|
|
|
$index = -1; |
|
70
|
|
|
$subIndex = -1; |
|
71
|
|
|
|
|
72
|
|
|
$url = $this->adminUrlGenerator |
|
|
|
|
|
|
73
|
|
|
->setDashboard($dashboard) |
|
74
|
|
|
->setRoute($route, $routeParameters) |
|
75
|
|
|
->set(EA::MENU_INDEX, $index) |
|
76
|
|
|
->set(EA::SUBMENU_INDEX, $subIndex) |
|
77
|
|
|
->generateUrl() |
|
78
|
|
|
; |
|
79
|
|
|
// $event->setResponse(new RedirectResponse($url)); |
|
80
|
|
|
return; |
|
81
|
|
|
|
|
82
|
|
|
$queryParams = $requestParams = $attributes = []; |
|
|
|
|
|
|
83
|
|
|
$attributes['_controller'] = [$dashboard, 'index']; |
|
84
|
|
|
$attributes['_route'] = $route; |
|
85
|
|
|
$attributes['_route_params'] = $routeParameters; |
|
86
|
|
|
|
|
87
|
|
|
$subRequest = $request->duplicate($queryParams, $requestParams, $attributes); |
|
88
|
|
|
|
|
89
|
|
|
$event->setResponse( |
|
90
|
|
|
$event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST) |
|
91
|
|
|
); |
|
92
|
|
|
/** |
|
93
|
|
|
TODO |
|
94
|
|
|
|
|
95
|
|
|
check if EAB is available |
|
96
|
|
|
check if EAB is activated for admin/user area (ThemeBundle configuration) |
|
97
|
|
|
|
|
98
|
|
|
Benefit: works with plain Symfony as well as EAB! |
|
99
|
|
|
*/ |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|