|
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\AdminBundle\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\Request; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
|
21
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
22
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Jan Góralski <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final 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
|
|
|
* @param UrlGeneratorInterface $router |
|
41
|
|
|
* @param SessionInterface $session |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(UrlGeneratorInterface $router, SessionInterface $session) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->router = $router; |
|
46
|
|
|
$this->session = $session; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function getSubscribedEvents() |
|
53
|
|
|
{ |
|
54
|
|
|
return [ |
|
55
|
|
|
KernelEvents::EXCEPTION => 'onResourceDelete', |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param GetResponseForExceptionEvent $event |
|
61
|
|
|
*/ |
|
62
|
|
|
public function onResourceDelete(GetResponseForExceptionEvent $event) |
|
63
|
|
|
{ |
|
64
|
|
|
$exception = $event->getException(); |
|
65
|
|
|
if (!$exception instanceof ForeignKeyConstraintViolationException) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!$event->isMasterRequest() || 'html' !== $event->getRequest()->getRequestFormat()) { |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$eventRequest = $event->getRequest(); |
|
74
|
|
|
$requestAttributes = $eventRequest->attributes; |
|
75
|
|
|
$originalRoute = $requestAttributes->get('_route'); |
|
76
|
|
|
|
|
77
|
|
|
if (!$this->isMethodDelete($eventRequest) || |
|
78
|
|
|
!$this->isSyliusRoute($originalRoute) || |
|
79
|
|
|
!$this->isAdminSection($requestAttributes->get('_sylius', [])) |
|
80
|
|
|
) { |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$resourceName = $this->getResourceNameFromRoute($originalRoute); |
|
85
|
|
|
|
|
86
|
|
|
if (null === $requestAttributes->get('_controller')) { |
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->session->getBag('flashes')->add('error', [ |
|
|
|
|
|
|
91
|
|
|
'message' => 'sylius.resource.delete_error', |
|
92
|
|
|
'parameters' => ['%resource%' => $resourceName], |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
$referrer = $eventRequest->headers->get('referer'); |
|
96
|
|
|
if (null !== $referrer) { |
|
97
|
|
|
$event->setResponse(new RedirectResponse($referrer)); |
|
|
|
|
|
|
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 $originalRoute |
|
121
|
|
|
* @param string $targetAction |
|
122
|
|
|
* |
|
123
|
|
|
* @return RedirectResponse |
|
124
|
|
|
*/ |
|
125
|
|
|
private function createRedirectResponse($originalRoute, $targetAction) |
|
126
|
|
|
{ |
|
127
|
|
|
$redirectRoute = str_replace(ResourceActions::DELETE, $targetAction, $originalRoute); |
|
128
|
|
|
|
|
129
|
|
|
return new RedirectResponse($this->router->generate($redirectRoute)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param Request $request |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
private function isMethodDelete(Request $request) |
|
138
|
|
|
{ |
|
139
|
|
|
return Request::METHOD_DELETE === $request->getMethod(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $route |
|
144
|
|
|
* |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
private function isSyliusRoute($route) |
|
148
|
|
|
{ |
|
149
|
|
|
return 0 === strpos($route, 'sylius'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param array $syliusParameters |
|
154
|
|
|
* |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
|
|
private function isAdminSection(array $syliusParameters) |
|
158
|
|
|
{ |
|
159
|
|
|
return array_key_exists('section', $syliusParameters) && 'admin' === $syliusParameters['section']; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: