|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Event\Subscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use LAG\AdminBundle\Configuration\AdminConfiguration; |
|
7
|
|
|
use LAG\AdminBundle\Event\AdminEvent; |
|
8
|
|
|
use LAG\AdminBundle\Event\AdminEvents; |
|
9
|
|
|
use LAG\AdminBundle\Event\EntityEvent; |
|
10
|
|
|
use LAG\AdminBundle\Event\MenuEvent; |
|
11
|
|
|
use LAG\AdminBundle\Event\ViewEvent; |
|
12
|
|
|
use LAG\AdminBundle\Exception\Exception; |
|
13
|
|
|
use LAG\AdminBundle\Factory\ActionFactory; |
|
14
|
|
|
use LAG\AdminBundle\Factory\DataProviderFactory; |
|
15
|
|
|
use LAG\AdminBundle\Factory\ViewFactory; |
|
16
|
|
|
use LAG\AdminBundle\LAGAdminBundle; |
|
17
|
|
|
use LAG\AdminBundle\Utils\StringUtils; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
22
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
23
|
|
|
|
|
24
|
|
|
class AdminSubscriber implements EventSubscriberInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ActionFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
private $actionFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ViewFactory |
|
33
|
|
|
*/ |
|
34
|
|
|
private $viewFactory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var EventDispatcherInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $eventDispatcher; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var DataProviderFactory |
|
43
|
|
|
*/ |
|
44
|
|
|
private $dataProviderFactory; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Session |
|
48
|
|
|
*/ |
|
49
|
|
|
private $session; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var TranslatorInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
private $translator; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function getSubscribedEvents() |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
|
|
AdminEvents::HANDLE_REQUEST => 'handleRequest', |
|
63
|
|
|
AdminEvents::VIEW => 'createView', |
|
64
|
|
|
AdminEvents::ENTITY_LOAD => 'loadEntities', |
|
65
|
|
|
AdminEvents::ENTITY_SAVE => 'saveEntity', |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* AdminSubscriber constructor. |
|
71
|
|
|
* |
|
72
|
|
|
* @param ActionFactory $actionFactory |
|
73
|
|
|
* @param ViewFactory $viewFactory |
|
74
|
|
|
* @param DataProviderFactory $dataProviderFactory |
|
75
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
76
|
|
|
* @param SessionInterface $session |
|
77
|
|
|
* @param TranslatorInterface $translator |
|
78
|
|
|
*/ |
|
79
|
|
|
public function __construct( |
|
80
|
|
|
ActionFactory $actionFactory, |
|
81
|
|
|
ViewFactory $viewFactory, |
|
82
|
|
|
DataProviderFactory $dataProviderFactory, |
|
83
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
84
|
|
|
SessionInterface $session, |
|
85
|
|
|
TranslatorInterface $translator |
|
86
|
|
|
) { |
|
87
|
|
|
$this->actionFactory = $actionFactory; |
|
88
|
|
|
$this->viewFactory = $viewFactory; |
|
89
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
90
|
|
|
$this->dataProviderFactory = $dataProviderFactory; |
|
91
|
|
|
$this->session = $session; |
|
|
|
|
|
|
92
|
|
|
$this->translator = $translator; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Define the current action according to the routing configuration. |
|
97
|
|
|
* |
|
98
|
|
|
* @param AdminEvent $event |
|
99
|
|
|
* |
|
100
|
|
|
* @throws Exception |
|
101
|
|
|
*/ |
|
102
|
|
|
public function handleRequest(AdminEvent $event) |
|
103
|
|
|
{ |
|
104
|
|
|
$actionName = $event->getRequest()->get('_action'); |
|
105
|
|
|
|
|
106
|
|
|
if (null === $actionName) { |
|
107
|
|
|
throw new Exception('The _action was not found in the request'); |
|
108
|
|
|
} |
|
109
|
|
|
$admin = $event->getAdmin(); |
|
110
|
|
|
$action = $this->actionFactory->create($actionName, $admin->getName(), $admin->getConfiguration()); |
|
111
|
|
|
|
|
112
|
|
|
$event->setAction($action); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Create a view using the view factory. |
|
117
|
|
|
* |
|
118
|
|
|
* @param ViewEvent $event |
|
119
|
|
|
*/ |
|
120
|
|
|
public function createView(ViewEvent $event) |
|
121
|
|
|
{ |
|
122
|
|
|
$admin = $event->getAdmin(); |
|
123
|
|
|
$action = $admin->getAction(); |
|
124
|
|
|
$menuEvent = new MenuEvent($admin->getAction()->getConfiguration()->getParameter('menus')); |
|
125
|
|
|
$this->eventDispatcher->dispatch(AdminEvents::MENU, $menuEvent); |
|
126
|
|
|
|
|
127
|
|
|
$view = $this->viewFactory->create( |
|
128
|
|
|
$event->getRequest(), |
|
129
|
|
|
$action->getName(), |
|
130
|
|
|
$admin->getName(), |
|
131
|
|
|
$admin->getConfiguration(), |
|
132
|
|
|
$action->getConfiguration(), |
|
133
|
|
|
$admin->getEntities(), |
|
134
|
|
|
$admin->getForms() |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
$event->setView($view); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Load entities into the event data to pass them to the Admin. |
|
142
|
|
|
* |
|
143
|
|
|
* @param EntityEvent $event |
|
144
|
|
|
* |
|
145
|
|
|
* @throws Exception |
|
146
|
|
|
*/ |
|
147
|
|
|
public function loadEntities(EntityEvent $event) |
|
148
|
|
|
{ |
|
149
|
|
|
$admin = $event->getAdmin(); |
|
150
|
|
|
$configuration = $admin->getConfiguration(); |
|
151
|
|
|
$actionConfiguration = $admin->getAction()->getConfiguration(); |
|
152
|
|
|
|
|
153
|
|
|
$dataProvider = $this->dataProviderFactory->get($configuration->getParameter('data_provider')); |
|
154
|
|
|
$strategy = $actionConfiguration->getParameter('load_strategy'); |
|
155
|
|
|
$class = $configuration->getParameter('entity'); |
|
156
|
|
|
|
|
157
|
|
|
if (LAGAdminBundle::LOAD_STRATEGY_NONE === $strategy) { |
|
158
|
|
|
return; |
|
159
|
|
|
} else if (LAGAdminBundle::LOAD_STRATEGY_MULTIPLE === $strategy) { |
|
160
|
|
|
$entities = $dataProvider->getCollection($admin, $event->getFilters()); |
|
161
|
|
|
$event->setEntities($entities); |
|
162
|
|
|
} else if (LAGAdminBundle::LOAD_STRATEGY_UNIQUE === $strategy) { |
|
163
|
|
|
$requirements = $actionConfiguration->getParameter('route_requirements'); |
|
164
|
|
|
$identifier = null; |
|
165
|
|
|
|
|
166
|
|
|
foreach ($requirements as $name => $requirement) { |
|
167
|
|
|
if (null !== $event->getRequest()->get($name)) { |
|
168
|
|
|
$identifier = $event->getRequest()->get($name); |
|
|
|
|
|
|
169
|
|
|
break; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if (null === $identifier) { |
|
174
|
|
|
throw new Exception('Unable to find a identifier for the class "'.$class.'"'); |
|
175
|
|
|
} |
|
176
|
|
|
$entity = $dataProvider->getItem($admin, $identifier); |
|
177
|
|
|
|
|
178
|
|
|
$event->setEntities(new ArrayCollection([ |
|
179
|
|
|
$entity, |
|
180
|
|
|
])); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Save an entity. |
|
187
|
|
|
* |
|
188
|
|
|
* @param EntityEvent $event |
|
189
|
|
|
*/ |
|
190
|
|
|
public function saveEntity(EntityEvent $event) |
|
191
|
|
|
{ |
|
192
|
|
|
$admin = $event->getAdmin(); |
|
193
|
|
|
$configuration = $admin->getConfiguration(); |
|
194
|
|
|
|
|
195
|
|
|
// Save the entity changes using the configured data provider |
|
196
|
|
|
$dataProvider = $this |
|
197
|
|
|
->dataProviderFactory |
|
198
|
|
|
->get($configuration->getParameter('data_provider')) |
|
199
|
|
|
; |
|
200
|
|
|
$dataProvider->saveItem($admin); |
|
201
|
|
|
|
|
202
|
|
|
// Inform the user that the save is successful |
|
203
|
|
|
$this->translateMessage('save_success', $configuration); |
|
204
|
|
|
|
|
205
|
|
|
$this |
|
206
|
|
|
->session |
|
207
|
|
|
->getFlashBag() |
|
208
|
|
|
->add('success', $this->translator->trans($message)) |
|
|
|
|
|
|
209
|
|
|
; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
private function translateMessage(string $message, AdminConfiguration $configuration): string |
|
213
|
|
|
{ |
|
214
|
|
|
$pattern = $configuration->get('translation_pattern'); |
|
215
|
|
|
$message = StringUtils::getTranslationKey($pattern, $admin->getName(), $message); |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.