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->eventDispatcher->dispatch(AdminEvent::ACTION_CREATE, $event); |
177
|
|
|
// creating action from configuration |
178
|
5 |
|
$action = $this |
179
|
|
|
->actionFactory |
180
|
5 |
|
->create($actionName, $event->getConfiguration(), $admin); |
181
|
|
|
// adding action to admin |
182
|
5 |
|
$admin->addAction($action); |
183
|
5 |
|
} |
184
|
5 |
|
return $admin; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Return an admin from a Symfony request. |
189
|
|
|
* |
190
|
|
|
* @param Request $request |
191
|
|
|
* @return AdminInterface |
192
|
|
|
* @throws Exception |
193
|
|
|
*/ |
194
|
1 |
|
public function getAdminFromRequest(Request $request) |
195
|
|
|
{ |
196
|
1 |
|
$routeParameters = $request->get('_route_params'); |
197
|
|
|
|
198
|
1 |
|
if (!$routeParameters) { |
199
|
|
|
throw new Exception('Cannot find admin from request. _route_params parameters for request not found'); |
200
|
|
|
} |
201
|
1 |
|
if (!array_key_exists('_admin', $routeParameters)) { |
202
|
|
|
throw new Exception('Cannot find admin from request. "_admin" route parameter is missing'); |
203
|
|
|
} |
204
|
1 |
|
if (!array_key_exists('_action', $routeParameters)) { |
205
|
|
|
throw new Exception('Cannot find admin action from request. "_action" route parameter is missing'); |
206
|
|
|
} |
207
|
1 |
|
$admin = $this->getAdmin($routeParameters['_admin']); |
208
|
|
|
|
209
|
1 |
|
return $admin; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return a admin by its name. |
214
|
|
|
* |
215
|
|
|
* @param $name |
216
|
|
|
* @return Admin |
217
|
|
|
* @throws Exception |
218
|
|
|
*/ |
219
|
5 |
|
public function getAdmin($name) |
220
|
|
|
{ |
221
|
5 |
|
if (!array_key_exists($name, $this->admins)) { |
222
|
3 |
|
throw new Exception(sprintf('Admin with name "%s" not found. Check your admin configuration', $name)); |
223
|
|
|
} |
224
|
|
|
|
225
|
3 |
|
return $this->admins[$name]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Return all admins. |
230
|
|
|
* |
231
|
|
|
* @return Admin[] |
232
|
|
|
*/ |
233
|
1 |
|
public function getAdmins() |
234
|
|
|
{ |
235
|
1 |
|
return $this->admins; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Add user custom repositories (called in the repository compiler pass), to avoid injecting the service container |
240
|
|
|
* |
241
|
|
|
* @param string $name |
242
|
|
|
* @param DataProviderInterface $dataProvider |
243
|
|
|
*/ |
244
|
1 |
|
public function addDataProvider($name, DataProviderInterface $dataProvider) |
245
|
|
|
{ |
246
|
1 |
|
$this |
247
|
|
|
->dataProviders |
248
|
1 |
|
->set($name, $dataProvider); |
249
|
1 |
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Return a configured data provider or create an new instance of the default one. |
253
|
|
|
* |
254
|
|
|
* @param string $entityClass |
255
|
|
|
* @param string|null $name |
256
|
|
|
* @return DataProvider|mixed |
257
|
|
|
* @throws Exception |
258
|
|
|
*/ |
259
|
5 |
|
protected function getDataProvider($entityClass, $name = null) |
260
|
|
|
{ |
261
|
|
|
// create or get repository according to the configuration |
262
|
5 |
|
if ($name) { |
263
|
|
|
// custom data provider class must be loaded by the compiler pass |
264
|
|
|
if (!$this->dataProviders->has($name)) { |
265
|
|
|
throw new Exception(sprintf( |
266
|
|
|
'Data provider %s not found. Did you add the @data_provider tag in your service ?', |
267
|
|
|
$name |
268
|
|
|
)); |
269
|
|
|
} |
270
|
|
|
$dataProvider = $this |
271
|
|
|
->dataProviders |
272
|
|
|
->get($name); |
273
|
|
|
} else { |
274
|
|
|
// no data provider is configured, we create a new one |
275
|
5 |
|
$repository = $this |
276
|
|
|
->entityManager |
277
|
5 |
|
->getRepository($entityClass); |
278
|
|
|
|
279
|
5 |
|
if (!($repository instanceof RepositoryInterface)) { |
280
|
|
|
$repositoryClass = get_class($repository); |
281
|
|
|
throw new Exception("Repository {$repositoryClass} should implements " . RepositoryInterface::class); |
282
|
|
|
} |
283
|
|
|
|
284
|
5 |
|
$dataProvider = new DataProvider($repository); |
285
|
|
|
} |
286
|
5 |
|
return $dataProvider; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|