|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Admin\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Action\Factory\ActionFactory; |
|
6
|
|
|
use LAG\AdminBundle\Action\Registry\Registry as ActionRegistry; |
|
7
|
|
|
use LAG\AdminBundle\Admin\AdminAwareInterface; |
|
8
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
9
|
|
|
use LAG\AdminBundle\Admin\Configuration\AdminConfiguration; |
|
10
|
|
|
use LAG\AdminBundle\Admin\Event\AdminCreatedEvent; |
|
11
|
|
|
use LAG\AdminBundle\Admin\Event\AdminCreateEvent; |
|
12
|
|
|
use LAG\AdminBundle\Admin\Event\AdminInjectedEvent; |
|
13
|
|
|
use LAG\AdminBundle\Admin\Event\BeforeConfigurationEvent; |
|
14
|
|
|
use LAG\AdminBundle\Admin\Registry\Registry; |
|
15
|
|
|
use LAG\AdminBundle\Admin\Request\RequestHandler; |
|
16
|
|
|
use LAG\AdminBundle\Configuration\Factory\ConfigurationFactory; |
|
17
|
|
|
use LAG\AdminBundle\Admin\Event\AdminEvents; |
|
18
|
|
|
use LAG\AdminBundle\DataProvider\Factory\DataProviderFactory; |
|
19
|
|
|
use LAG\AdminBundle\DataProvider\Loader\EntityLoader; |
|
20
|
|
|
use LAG\AdminBundle\Message\MessageHandlerInterface; |
|
21
|
|
|
use LAG\AdminBundle\View\Factory\ViewFactory; |
|
22
|
|
|
use LogicException; |
|
23
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
25
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
26
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* AdminFactory. |
|
30
|
|
|
* |
|
31
|
|
|
* This class is responsible for the creation of Admins from configuration array. |
|
32
|
|
|
*/ |
|
33
|
|
|
class AdminFactory |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
private $isInit = false; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var EventDispatcherInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $eventDispatcher; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var ConfigurationFactory |
|
47
|
|
|
*/ |
|
48
|
|
|
private $configurationFactory; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var AdminConfiguration[] |
|
52
|
|
|
*/ |
|
53
|
|
|
private $adminConfigurations; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var ActionFactory |
|
57
|
|
|
*/ |
|
58
|
|
|
private $actionFactory; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var MessageHandlerInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
private $messageHandler; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var Registry |
|
67
|
|
|
*/ |
|
68
|
|
|
private $adminRegistry; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var DataProviderFactory |
|
72
|
|
|
*/ |
|
73
|
|
|
private $dataProviderFactory; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var RequestHandler |
|
77
|
|
|
*/ |
|
78
|
|
|
private $requestHandler; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var AuthorizationCheckerInterface |
|
82
|
|
|
*/ |
|
83
|
|
|
private $authorizationChecker; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var TokenStorageInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
private $tokenStorage; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @var ActionRegistry |
|
92
|
|
|
*/ |
|
93
|
|
|
private $actionRegistry; |
|
94
|
|
|
/** |
|
95
|
|
|
* @var ViewFactory |
|
96
|
|
|
*/ |
|
97
|
|
|
private $viewFactory; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* AdminFactory constructor. |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $adminConfigurations |
|
103
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
104
|
|
|
* @param MessageHandlerInterface $messageHandler |
|
105
|
|
|
* @param Registry $adminRegistry |
|
106
|
|
|
* @param ActionRegistry $actionRegistry |
|
107
|
|
|
* @param ActionFactory $actionFactory |
|
108
|
|
|
* @param ConfigurationFactory $configurationFactory |
|
109
|
|
|
* @param DataProviderFactory $dataProviderFactory |
|
110
|
|
|
* @param ViewFactory $viewFactory |
|
111
|
|
|
* @param RequestHandler $requestHandler |
|
112
|
|
|
* @param AuthorizationCheckerInterface $authorizationChecker |
|
113
|
|
|
* @param TokenStorageInterface $tokenStorage |
|
114
|
|
|
*/ |
|
115
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
116
|
|
|
array $adminConfigurations, |
|
117
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
118
|
|
|
MessageHandlerInterface $messageHandler, |
|
119
|
|
|
Registry $adminRegistry, |
|
120
|
|
|
ActionRegistry $actionRegistry, |
|
121
|
|
|
ActionFactory $actionFactory, |
|
122
|
|
|
ConfigurationFactory $configurationFactory, |
|
123
|
|
|
DataProviderFactory $dataProviderFactory, |
|
124
|
|
|
ViewFactory $viewFactory, |
|
125
|
|
|
RequestHandler $requestHandler, |
|
126
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
|
127
|
|
|
TokenStorageInterface $tokenStorage |
|
128
|
|
|
) { |
|
129
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
130
|
|
|
$this->configurationFactory = $configurationFactory; |
|
131
|
|
|
$this->adminConfigurations = $adminConfigurations; |
|
132
|
|
|
$this->actionFactory = $actionFactory; |
|
133
|
|
|
$this->messageHandler = $messageHandler; |
|
134
|
|
|
$this->adminRegistry = $adminRegistry; |
|
135
|
|
|
$this->dataProviderFactory = $dataProviderFactory; |
|
136
|
|
|
$this->requestHandler = $requestHandler; |
|
137
|
|
|
$this->authorizationChecker = $authorizationChecker; |
|
138
|
|
|
$this->tokenStorage = $tokenStorage; |
|
139
|
|
|
$this->actionRegistry = $actionRegistry; |
|
140
|
|
|
$this->viewFactory = $viewFactory; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Create admins from configuration and load them into the pool. Dispatch ADMIN_CREATE event. |
|
145
|
|
|
*/ |
|
146
|
|
|
public function init() |
|
147
|
|
|
{ |
|
148
|
|
|
// init only once |
|
149
|
|
|
if ($this->isInit) { |
|
150
|
|
|
return; |
|
151
|
|
|
} |
|
152
|
|
|
// dispatch an event to allow configuration modification before resolving and creating admins |
|
153
|
|
|
$event = new BeforeConfigurationEvent($this->adminConfigurations); |
|
154
|
|
|
$this |
|
155
|
|
|
->eventDispatcher |
|
156
|
|
|
->dispatch(AdminEvents::BEFORE_CONFIGURATION, $event) |
|
157
|
|
|
; |
|
158
|
|
|
|
|
159
|
|
|
// get the modified configuration |
|
160
|
|
|
$this->adminConfigurations = $event->getAdminConfigurations(); |
|
161
|
|
|
|
|
162
|
|
|
// create Admins according to the given configuration |
|
163
|
|
|
foreach ($this->adminConfigurations as $name => $configuration) { |
|
164
|
|
|
// dispatch an event to allow modification on a specific admin |
|
165
|
|
|
$event = new AdminCreateEvent($name, $configuration); |
|
166
|
|
|
$this |
|
167
|
|
|
->eventDispatcher |
|
168
|
|
|
->dispatch(AdminEvents::ADMIN_CREATE, $event) |
|
169
|
|
|
; |
|
170
|
|
|
|
|
171
|
|
|
// create Admin object and add it to the registry |
|
172
|
|
|
$admin = $this->create($name, $event->getAdminConfiguration()); |
|
173
|
|
|
$this |
|
174
|
|
|
->adminRegistry |
|
175
|
|
|
->add($admin) |
|
176
|
|
|
; |
|
177
|
|
|
|
|
178
|
|
|
// dispatch post-create event |
|
179
|
|
|
$event = new AdminCreatedEvent($admin); |
|
180
|
|
|
$this |
|
181
|
|
|
->eventDispatcher |
|
182
|
|
|
->dispatch(AdminEvents::ADMIN_CREATED, $event) |
|
183
|
|
|
; |
|
184
|
|
|
} |
|
185
|
|
|
$this->isInit = true; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Create an Admin from configuration values. It will be added to AdminFactory admin's list. |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $name |
|
192
|
|
|
* @param array $configuration |
|
193
|
|
|
* |
|
194
|
|
|
* @return AdminInterface |
|
195
|
|
|
* |
|
196
|
|
|
* @throws LogicException |
|
197
|
|
|
*/ |
|
198
|
|
|
public function create($name, array $configuration) |
|
199
|
|
|
{ |
|
200
|
|
|
// create AdminConfiguration object |
|
201
|
|
|
$adminConfiguration = $this |
|
|
|
|
|
|
202
|
|
|
->configurationFactory |
|
203
|
|
|
->createAdminConfiguration($configuration) |
|
204
|
|
|
; |
|
205
|
|
|
|
|
206
|
|
|
// retrieve a data provider and load it into the loader |
|
207
|
|
|
$dataProvider = $this |
|
208
|
|
|
->dataProviderFactory |
|
209
|
|
|
->get($adminConfiguration->getParameter('data_provider'), $adminConfiguration->getParameter('entity')) |
|
210
|
|
|
; |
|
211
|
|
|
$entityLoader = new EntityLoader($dataProvider); |
|
212
|
|
|
|
|
213
|
|
|
// retrieve Admin dynamic class |
|
214
|
|
|
$adminClass = $this |
|
|
|
|
|
|
215
|
|
|
->configurationFactory |
|
216
|
|
|
->getApplicationConfiguration() |
|
217
|
|
|
->getParameter('admin_class') |
|
218
|
|
|
; |
|
219
|
|
|
|
|
220
|
|
|
// retrieve the actions services |
|
221
|
|
|
$actions = $this |
|
222
|
|
|
->actionFactory |
|
223
|
|
|
->getActions($name, $configuration) |
|
224
|
|
|
; |
|
225
|
|
|
|
|
226
|
|
|
// create Admin object |
|
227
|
|
|
$admin = new $adminClass( |
|
228
|
|
|
$name, |
|
229
|
|
|
$entityLoader, |
|
230
|
|
|
$adminConfiguration, |
|
231
|
|
|
$this->messageHandler, |
|
232
|
|
|
$this->eventDispatcher, |
|
233
|
|
|
$this->authorizationChecker, |
|
234
|
|
|
$this->tokenStorage, |
|
235
|
|
|
$this->requestHandler, |
|
236
|
|
|
$this->viewFactory, |
|
237
|
|
|
$actions |
|
238
|
|
|
); |
|
239
|
|
|
|
|
240
|
|
|
if (!$admin instanceof AdminInterface) { |
|
241
|
|
|
throw new LogicException('Class "'.get_class($admin).'" should implements '.AdminInterface::class); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $admin; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Inject an Admin into an AdminAware controller. Add this Admin to Twig global parameters. |
|
249
|
|
|
* |
|
250
|
|
|
* @param mixed $controller |
|
251
|
|
|
* @param Request $request |
|
252
|
|
|
*/ |
|
253
|
|
|
public function injectAdmin($controller, Request $request) |
|
254
|
|
|
{ |
|
255
|
|
|
// the controller should be Admin aware to have an Admin injected |
|
256
|
|
|
if (!$controller instanceof AdminAwareInterface) { |
|
257
|
|
|
return; |
|
258
|
|
|
} |
|
259
|
|
|
// get admin from the request parameters |
|
260
|
|
|
$admin = $this |
|
261
|
|
|
->requestHandler |
|
262
|
|
|
->handle($request) |
|
263
|
|
|
; |
|
264
|
|
|
|
|
265
|
|
|
// inject the Admin in the controller |
|
266
|
|
|
$controller->setAdmin($admin); |
|
267
|
|
|
|
|
268
|
|
|
// dispatch an even to allow some custom logic |
|
269
|
|
|
$this |
|
270
|
|
|
->eventDispatcher |
|
271
|
|
|
->dispatch(AdminEvents::ADMIN_INJECTED, new AdminInjectedEvent($admin, $controller)) |
|
272
|
|
|
; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Return true if the AdminFactory is initialized. |
|
277
|
|
|
* |
|
278
|
|
|
* @return boolean |
|
279
|
|
|
*/ |
|
280
|
|
|
public function isInit() |
|
281
|
|
|
{ |
|
282
|
|
|
return $this->isInit; |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.