/
AdminFilterSubscriber.php
80 lines (64 loc) · 2.13 KB
/
AdminFilterSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\AdminBundle\EventListener;
use Sylius\Bundle\UiBundle\Storage\FilterStorageInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
final class AdminFilterSubscriber implements EventSubscriberInterface
{
public function __construct(private FilterStorageInterface $filterStorage)
{
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$this->isMainRequest($event)) {
return;
}
$eventRequest = $event->getRequest();
if ('html' !== $eventRequest->getRequestFormat()) {
return;
}
$requestAttributes = $eventRequest->attributes;
if (
null === $requestAttributes->get('_controller') ||
!$this->isIndexResourceRoute($requestAttributes->get('_route', '')) ||
!$this->isAdminSection($requestAttributes->get('_sylius', []))
) {
return;
}
if ($this->filterStorage->all() !== $eventRequest->query->all()) {
$this->filterStorage->set($eventRequest->query->all());
}
}
private function isMainRequest(RequestEvent $event): bool
{
if (\method_exists($event, 'isMainRequest')) {
return $event->isMainRequest();
}
/** @phpstan-ignore-next-line */
return $event->isMasterRequest();
}
private function isIndexResourceRoute(string $route): bool
{
return str_ends_with($route, 'index');
}
private function isAdminSection(array $syliusParameters): bool
{
return isset($syliusParameters['section']) && 'admin' === $syliusParameters['section'];
}
}