1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\ResourceBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; |
15
|
|
|
use Sylius\Component\Resource\ResourceActions; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
19
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
20
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
21
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
22
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Jan Góralski <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ResourceDeleteSubscriber implements EventSubscriberInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var UrlGeneratorInterface |
31
|
|
|
*/ |
32
|
|
|
private $router; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var SessionInterface |
36
|
|
|
*/ |
37
|
|
|
private $session; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TranslatorInterface |
41
|
|
|
*/ |
42
|
|
|
private $translator; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param UrlGeneratorInterface $router |
46
|
|
|
* @param SessionInterface $session |
47
|
|
|
* @param TranslatorInterface $translator |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
UrlGeneratorInterface $router, |
51
|
|
|
SessionInterface $session, |
52
|
|
|
TranslatorInterface $translator |
53
|
|
|
) { |
54
|
|
|
$this->router = $router; |
55
|
|
|
$this->session = $session; |
56
|
|
|
$this->translator = $translator; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public static function getSubscribedEvents() |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
KernelEvents::EXCEPTION => 'onResourceDelete', |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param GetResponseForExceptionEvent $event |
71
|
|
|
*/ |
72
|
|
|
public function onResourceDelete(GetResponseForExceptionEvent $event) |
73
|
|
|
{ |
74
|
|
|
$exception = $event->getException(); |
75
|
|
|
if (!$exception instanceof ForeignKeyConstraintViolationException) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$requestAttributes = $event->getRequest()->attributes; |
80
|
|
|
if (null === $requestAttributes->get('_controller')) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$originalRoute = $requestAttributes->get('_route'); |
85
|
|
|
$resourceName = $this->getResourceNameFromRoute($originalRoute); |
86
|
|
|
|
87
|
|
|
$this->session->getBag('flashes')->add( |
88
|
|
|
'error', |
89
|
|
|
$this->translator->trans('sylius.resource.delete_error', ['%resource%' => $resourceName], 'flashes') |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$referrer = $event->getRequest()->headers->get('referer'); |
93
|
|
|
|
94
|
|
|
if ($this->refersFromShow($referrer)) { |
95
|
|
|
$event->setResponse( |
96
|
|
|
$this->createRedirectResponse($originalRoute, ResourceActions::SHOW, ['id' => $requestAttributes->get('id')]) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$event->setResponse($this->createRedirectResponse($originalRoute, ResourceActions::INDEX)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $route |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
private function getResourceNameFromRoute($route) |
111
|
|
|
{ |
112
|
|
|
$routeArray = explode('_', $route); |
113
|
|
|
$routeArrayWithoutAction = array_slice($routeArray, 0, count($routeArray) - 1); |
114
|
|
|
$routeArrayWithoutPrefixes = array_slice($routeArrayWithoutAction, 2); |
115
|
|
|
|
116
|
|
|
return trim(implode(' ', $routeArrayWithoutPrefixes)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $referrer |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
private function refersFromShow($referrer) |
125
|
|
|
{ |
126
|
|
|
$referrerArray = explode('/', $referrer); |
127
|
|
|
|
128
|
|
|
return is_numeric(array_pop($referrerArray)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $originalRoute |
133
|
|
|
* @param string $targetAction |
134
|
|
|
* @param array $parameters |
135
|
|
|
* |
136
|
|
|
* @return RedirectResponse |
137
|
|
|
*/ |
138
|
|
|
private function createRedirectResponse($originalRoute, $targetAction, array $parameters = []) |
139
|
|
|
{ |
140
|
|
|
$redirectRoute = str_replace(ResourceActions::DELETE, $targetAction, $originalRoute); |
141
|
|
|
|
142
|
|
|
return new RedirectResponse($this->router->generate($redirectRoute, $parameters)); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|