1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file was created by developers working at BitBag |
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace BitBag\SyliusCmsPlugin\EventListener; |
12
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; |
14
|
|
|
use Sylius\Component\Resource\ResourceActions; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
21
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
22
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
23
|
|
|
|
24
|
|
|
final class ResourceDeleteSubscriber implements EventSubscriberInterface |
25
|
|
|
{ |
26
|
|
|
/** @var UrlGeneratorInterface */ |
27
|
|
|
private $router; |
28
|
|
|
|
29
|
|
|
/** @var SessionInterface */ |
30
|
|
|
private $session; |
31
|
|
|
|
32
|
|
|
public function __construct(UrlGeneratorInterface $router, SessionInterface $session) |
33
|
|
|
{ |
34
|
|
|
$this->router = $router; |
35
|
|
|
$this->session = $session; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function getSubscribedEvents(): array |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
KernelEvents::EXCEPTION => 'onResourceDelete', |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function onResourceDelete(ExceptionEvent $event): void |
46
|
|
|
{ |
47
|
|
|
$exception = $event->getThrowable(); |
48
|
|
|
if (!$exception instanceof ForeignKeyConstraintViolationException) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!$event->isMasterRequest() || 'html' !== $event->getRequest()->getRequestFormat()) { |
|
|
|
|
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$eventRequest = $event->getRequest(); |
57
|
|
|
$requestAttributes = $eventRequest->attributes; |
58
|
|
|
$originalRoute = $requestAttributes->get('_route'); |
59
|
|
|
|
60
|
|
|
if (!$this->isMethodDelete($eventRequest) || |
61
|
|
|
!$this->isProtectedRoute($originalRoute) || |
62
|
|
|
!$this->isAdminSection($requestAttributes->get('_sylius', [])) |
63
|
|
|
) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$resourceName = $this->getResourceNameFromRoute($originalRoute); |
68
|
|
|
|
69
|
|
|
if (null === $requestAttributes->get('_controller')) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @var FlashBagInterface $flashBag */ |
74
|
|
|
$flashBag = $this->session->getBag('flashes'); |
75
|
|
|
$flashBag->add('error', [ |
76
|
|
|
'message' => 'sylius.resource.delete_error', |
77
|
|
|
'parameters' => ['%resource%' => $resourceName], |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
$referrer = $eventRequest->headers->get('referer'); |
81
|
|
|
if (null !== $referrer) { |
|
|
|
|
82
|
|
|
$event->setResponse(new RedirectResponse($referrer)); |
83
|
|
|
|
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$event->setResponse($this->createRedirectResponse($originalRoute, ResourceActions::INDEX)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function getResourceNameFromRoute(string $route): string |
91
|
|
|
{ |
92
|
|
|
$route = str_replace('_bulk', '', $route); |
93
|
|
|
$routeArray = explode('_', $route); |
94
|
|
|
$routeArrayWithoutAction = array_slice($routeArray, 0, count($routeArray) - 1); |
95
|
|
|
$routeArrayWithoutPrefixes = array_slice($routeArrayWithoutAction, 2); |
96
|
|
|
|
97
|
|
|
return trim(implode(' ', $routeArrayWithoutPrefixes)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function createRedirectResponse(string $originalRoute, string $targetAction): RedirectResponse |
101
|
|
|
{ |
102
|
|
|
$redirectRoute = str_replace(ResourceActions::DELETE, $targetAction, $originalRoute); |
103
|
|
|
|
104
|
|
|
return new RedirectResponse($this->router->generate($redirectRoute)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function isMethodDelete(Request $request): bool |
108
|
|
|
{ |
109
|
|
|
return Request::METHOD_DELETE === $request->getMethod(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function isProtectedRoute(string $route): bool |
113
|
|
|
{ |
114
|
|
|
return 0 === strpos($route, 'bitbag'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function isAdminSection(array $syliusParameters): bool |
118
|
|
|
{ |
119
|
|
|
return array_key_exists('section', $syliusParameters) && 'admin' === $syliusParameters['section']; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.