|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Admin\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use LAG\AdminBundle\Action\Factory\ActionFactory; |
|
7
|
|
|
use LAG\AdminBundle\Admin\Admin; |
|
8
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
9
|
|
|
use LAG\AdminBundle\Configuration\Factory\ConfigurationFactory; |
|
10
|
|
|
use LAG\AdminBundle\DataProvider\DataProvider; |
|
11
|
|
|
use LAG\AdminBundle\DataProvider\DataProviderInterface; |
|
12
|
|
|
use LAG\AdminBundle\Event\AdminEvent; |
|
13
|
|
|
use LAG\AdminBundle\Event\AdminFactoryEvent; |
|
14
|
|
|
use Exception; |
|
15
|
|
|
use LAG\AdminBundle\Message\MessageHandlerInterface; |
|
16
|
|
|
use LAG\DoctrineRepositoryBundle\Repository\RepositoryInterface; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
|
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* AdminFactory. |
|
24
|
|
|
* |
|
25
|
|
|
* Create admin from configuration |
|
26
|
|
|
*/ |
|
27
|
|
|
class AdminFactory |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $admins = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $isInit = false; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var EventDispatcherInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $eventDispatcher; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var EntityManager |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $entityManager; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var ConfigurationFactory |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $configurationFactory; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* User custom data provider, indexed by service id |
|
56
|
|
|
* |
|
57
|
|
|
* @var ParameterBagInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $dataProviders; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $adminConfigurations; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var ActionFactory |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $actionFactory; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var MessageHandlerInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $messageHandler; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* AdminFactory constructor. |
|
78
|
|
|
* |
|
79
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
80
|
|
|
* @param EntityManager $entityManager |
|
81
|
|
|
* @param ConfigurationFactory $configurationFactory |
|
82
|
|
|
* @param array $adminConfigurations |
|
83
|
|
|
* @param ActionFactory $actionFactory |
|
84
|
|
|
* @param MessageHandlerInterface $messageHandler |
|
85
|
|
|
*/ |
|
86
|
6 |
|
public function __construct( |
|
87
|
|
|
array $adminConfigurations, |
|
88
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
89
|
|
|
EntityManager $entityManager, |
|
90
|
|
|
ConfigurationFactory $configurationFactory, |
|
91
|
|
|
ActionFactory $actionFactory, |
|
92
|
|
|
MessageHandlerInterface $messageHandler |
|
93
|
|
|
) { |
|
94
|
6 |
|
$this->eventDispatcher = $eventDispatcher; |
|
95
|
6 |
|
$this->entityManager = $entityManager; |
|
96
|
6 |
|
$this->configurationFactory = $configurationFactory; |
|
97
|
6 |
|
$this->adminConfigurations = $adminConfigurations; |
|
98
|
6 |
|
$this->actionFactory = $actionFactory; |
|
99
|
6 |
|
$this->messageHandler = $messageHandler; |
|
100
|
6 |
|
$this->dataProviders = new ParameterBag(); |
|
101
|
6 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Create admins from configuration and load them into the pool. Dispatch ADMIN_CREATE event. |
|
105
|
|
|
*/ |
|
106
|
4 |
|
public function init() |
|
107
|
|
|
{ |
|
108
|
|
|
// init only once |
|
109
|
4 |
|
if ($this->isInit) { |
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
4 |
|
$event = new AdminFactoryEvent(); |
|
113
|
4 |
|
$event->setAdminsConfiguration($this->adminConfigurations); |
|
114
|
|
|
|
|
115
|
|
|
// dispatch an event to allow configuration modification before resolving and creating admins |
|
116
|
4 |
|
$this |
|
117
|
|
|
->eventDispatcher |
|
118
|
4 |
|
->dispatch(AdminFactoryEvent::ADMIN_CREATION, $event); |
|
119
|
|
|
// set modified configuration |
|
120
|
4 |
|
$this->adminConfigurations = $event->getAdminsConfiguration(); |
|
121
|
|
|
|
|
122
|
4 |
|
foreach ($this->adminConfigurations as $name => $configuration) { |
|
123
|
|
|
|
|
124
|
|
|
// dispatch an event to allow modification on this specific admin |
|
125
|
4 |
|
$event = new AdminEvent(); |
|
126
|
|
|
$event |
|
127
|
4 |
|
->setConfiguration($configuration) |
|
128
|
4 |
|
->setAdminName($name) |
|
129
|
|
|
; |
|
130
|
4 |
|
$this |
|
131
|
|
|
->eventDispatcher |
|
132
|
4 |
|
->dispatch(AdminEvent::ADMIN_CREATE, $event); |
|
133
|
|
|
|
|
134
|
|
|
// create Admin object |
|
135
|
4 |
|
$this->admins[$name] = $this->create($name, $event->getConfiguration()); |
|
136
|
4 |
|
} |
|
137
|
4 |
|
$this->isInit = true; |
|
138
|
4 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Create an Admin from configuration values. It will be added to AdminFactory admin's list. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $name |
|
144
|
|
|
* @param array $configuration |
|
145
|
|
|
* @return Admin |
|
146
|
|
|
* @throws Exception |
|
147
|
|
|
*/ |
|
148
|
5 |
|
public function create($name, array $configuration) |
|
149
|
|
|
{ |
|
150
|
|
|
// create AdminConfiguration object |
|
151
|
5 |
|
$adminConfiguration = $this |
|
152
|
|
|
->configurationFactory |
|
153
|
5 |
|
->createAdminConfiguration($configuration); |
|
154
|
|
|
|
|
155
|
|
|
// retrieve a data provider |
|
156
|
5 |
|
$dataProvider = $this->getDataProvider( |
|
157
|
5 |
|
$adminConfiguration->getParameter('entity'), |
|
158
|
5 |
|
$adminConfiguration->getParameter('data_provider') |
|
159
|
5 |
|
); |
|
160
|
|
|
|
|
161
|
|
|
// create Admin object |
|
162
|
5 |
|
$admin = new Admin( |
|
163
|
5 |
|
$name, |
|
164
|
5 |
|
$dataProvider, |
|
165
|
5 |
|
$adminConfiguration, |
|
166
|
5 |
|
$this->messageHandler |
|
167
|
5 |
|
); |
|
168
|
|
|
|
|
169
|
|
|
// adding actions |
|
170
|
5 |
|
foreach ($adminConfiguration->getParameter('actions') as $actionName => $actionConfiguration) { |
|
171
|
|
|
// dispatching action create event for dynamic action creation |
|
172
|
5 |
|
$event = new AdminEvent(); |
|
173
|
5 |
|
$event->setConfiguration($actionConfiguration); |
|
174
|
5 |
|
$event->setAdmin($admin); |
|
175
|
5 |
|
$event->setActionName($actionName); |
|
176
|
5 |
|
$this |
|
177
|
|
|
->eventDispatcher |
|
178
|
5 |
|
->dispatch(AdminEvent::ACTION_CREATE, $event); |
|
179
|
|
|
|
|
180
|
|
|
// creating action from configuration |
|
181
|
5 |
|
$action = $this |
|
182
|
|
|
->actionFactory |
|
183
|
5 |
|
->create($actionName, $event->getConfiguration(), $admin); |
|
184
|
|
|
|
|
185
|
|
|
// adding action to admin |
|
186
|
5 |
|
$admin->addAction($action); |
|
187
|
5 |
|
} |
|
188
|
5 |
|
return $admin; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Return an admin from a Symfony request. |
|
193
|
|
|
* |
|
194
|
|
|
* @param Request $request |
|
195
|
|
|
* @return AdminInterface |
|
196
|
|
|
* @throws Exception |
|
197
|
|
|
*/ |
|
198
|
1 |
|
public function getAdminFromRequest(Request $request) |
|
199
|
|
|
{ |
|
200
|
1 |
|
$routeParameters = $request->get('_route_params'); |
|
201
|
|
|
|
|
202
|
1 |
|
if (!$routeParameters) { |
|
203
|
|
|
throw new Exception('Cannot find admin from request. _route_params parameters for request not found'); |
|
204
|
|
|
} |
|
205
|
1 |
|
if (!array_key_exists('_admin', $routeParameters)) { |
|
206
|
|
|
throw new Exception('Cannot find admin from request. "_admin" route parameter is missing'); |
|
207
|
|
|
} |
|
208
|
1 |
|
if (!array_key_exists('_action', $routeParameters)) { |
|
209
|
|
|
throw new Exception('Cannot find admin action from request. "_action" route parameter is missing'); |
|
210
|
|
|
} |
|
211
|
1 |
|
$admin = $this->getAdmin($routeParameters['_admin']); |
|
212
|
|
|
|
|
213
|
1 |
|
return $admin; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Return a admin by its name. |
|
218
|
|
|
* |
|
219
|
|
|
* @param $name |
|
220
|
|
|
* @return Admin |
|
221
|
|
|
* @throws Exception |
|
222
|
|
|
*/ |
|
223
|
5 |
|
public function getAdmin($name) |
|
224
|
|
|
{ |
|
225
|
5 |
|
if (!array_key_exists($name, $this->admins)) { |
|
226
|
3 |
|
throw new Exception(sprintf('Admin with name "%s" not found. Check your admin configuration', $name)); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
3 |
|
return $this->admins[$name]; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Return all admins. |
|
234
|
|
|
* |
|
235
|
|
|
* @return AdminInterface[] |
|
236
|
|
|
*/ |
|
237
|
1 |
|
public function getAdmins() |
|
238
|
|
|
{ |
|
239
|
1 |
|
return $this->admins; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Add user custom repositories (called in the repository compiler pass), to avoid injecting the service container |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $name |
|
246
|
|
|
* @param DataProviderInterface $dataProvider |
|
247
|
|
|
*/ |
|
248
|
1 |
|
public function addDataProvider($name, DataProviderInterface $dataProvider) |
|
249
|
|
|
{ |
|
250
|
1 |
|
$this |
|
251
|
|
|
->dataProviders |
|
252
|
1 |
|
->set($name, $dataProvider); |
|
253
|
1 |
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Return a configured data provider or create an new instance of the default one. |
|
257
|
|
|
* |
|
258
|
|
|
* @param string $entityClass |
|
259
|
|
|
* @param string|null $name |
|
260
|
|
|
* @return DataProvider|mixed |
|
261
|
|
|
* @throws Exception |
|
262
|
|
|
*/ |
|
263
|
5 |
|
protected function getDataProvider($entityClass, $name = null) |
|
264
|
|
|
{ |
|
265
|
|
|
// create or get repository according to the configuration |
|
266
|
5 |
|
if ($name) { |
|
267
|
|
|
// custom data provider class must be loaded by the compiler pass |
|
268
|
|
|
if (!$this->dataProviders->has($name)) { |
|
269
|
|
|
throw new Exception(sprintf( |
|
270
|
|
|
'Data provider %s not found. Did you add the @data_provider tag in your service ?', |
|
271
|
|
|
$name |
|
272
|
|
|
)); |
|
273
|
|
|
} |
|
274
|
|
|
$dataProvider = $this |
|
275
|
|
|
->dataProviders |
|
276
|
|
|
->get($name); |
|
277
|
|
|
} else { |
|
278
|
|
|
// no data provider is configured, we create a new one |
|
279
|
5 |
|
$repository = $this |
|
280
|
|
|
->entityManager |
|
281
|
5 |
|
->getRepository($entityClass); |
|
282
|
|
|
|
|
283
|
5 |
|
if (!($repository instanceof RepositoryInterface)) { |
|
284
|
|
|
$repositoryClass = get_class($repository); |
|
285
|
|
|
throw new Exception("Repository {$repositoryClass} should implements ".RepositoryInterface::class); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
5 |
|
$dataProvider = new DataProvider($repository); |
|
289
|
|
|
} |
|
290
|
5 |
|
return $dataProvider; |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|