|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Admin\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Action\Factory\ActionFactory; |
|
6
|
|
|
use LAG\AdminBundle\Admin\Admin; |
|
7
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
8
|
|
|
use LAG\AdminBundle\Admin\Configuration\AdminConfiguration; |
|
9
|
|
|
use LAG\AdminBundle\Admin\Event\AdminCreatedEvent; |
|
10
|
|
|
use LAG\AdminBundle\Admin\Event\AdminCreateEvent; |
|
11
|
|
|
use LAG\AdminBundle\Admin\Event\BeforeConfigurationEvent; |
|
12
|
|
|
use LAG\AdminBundle\Admin\Registry\Registry; |
|
13
|
|
|
use LAG\AdminBundle\Configuration\Factory\ConfigurationFactory; |
|
14
|
|
|
use LAG\AdminBundle\Admin\Event\AdminEvents; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use LAG\AdminBundle\DataProvider\DataProviderInterface; |
|
17
|
|
|
use LAG\AdminBundle\DataProvider\Factory\DataProviderFactory; |
|
18
|
|
|
use LAG\AdminBundle\Filter\Factory\RequestFilterFactory; |
|
19
|
|
|
use LAG\AdminBundle\Message\MessageHandlerInterface; |
|
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* AdminFactory. |
|
24
|
|
|
* |
|
25
|
|
|
* Create admin from configuration |
|
26
|
|
|
*/ |
|
27
|
|
|
class AdminFactory |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var bool |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $isInit = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var EventDispatcherInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $eventDispatcher; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ConfigurationFactory |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $configurationFactory; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var AdminConfiguration[] |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $adminConfigurations; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var ActionFactory |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $actionFactory; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var MessageHandlerInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $messageHandler; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Registry |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $adminRegistry; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var RequestFilterFactory |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $requestFilterFactory; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var DataProviderFactory |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $dataProviderFactory; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* AdminFactory constructor. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $adminConfigurations |
|
78
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
79
|
|
|
* @param ConfigurationFactory $configurationFactory |
|
80
|
|
|
* @param ActionFactory $actionFactory |
|
81
|
|
|
* @param MessageHandlerInterface $messageHandler |
|
82
|
|
|
* @param Registry $adminRegistry |
|
83
|
|
|
* @param RequestFilterFactory $requestFilterFactory |
|
84
|
|
|
* @param DataProviderFactory $dataProviderFactory |
|
85
|
|
|
*/ |
|
86
|
|
|
public function __construct( |
|
87
|
|
|
array $adminConfigurations, |
|
88
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
89
|
4 |
|
ConfigurationFactory $configurationFactory, |
|
90
|
|
|
ActionFactory $actionFactory, |
|
91
|
|
|
MessageHandlerInterface $messageHandler, |
|
92
|
|
|
Registry $adminRegistry, |
|
93
|
|
|
RequestFilterFactory $requestFilterFactory, |
|
94
|
|
|
DataProviderFactory $dataProviderFactory |
|
95
|
|
|
) { |
|
96
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
97
|
|
|
$this->configurationFactory = $configurationFactory; |
|
98
|
4 |
|
$this->adminConfigurations = $adminConfigurations; |
|
99
|
4 |
|
$this->actionFactory = $actionFactory; |
|
100
|
4 |
|
$this->messageHandler = $messageHandler; |
|
101
|
4 |
|
$this->adminRegistry = $adminRegistry; |
|
102
|
4 |
|
$this->requestFilterFactory = $requestFilterFactory; |
|
103
|
4 |
|
$this->dataProviderFactory = $dataProviderFactory; |
|
104
|
4 |
|
} |
|
105
|
4 |
|
|
|
106
|
4 |
|
/** |
|
107
|
|
|
* Create admins from configuration and load them into the pool. Dispatch ADMIN_CREATE event. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function init() |
|
110
|
|
|
{ |
|
111
|
2 |
|
// init only once |
|
112
|
|
|
if ($this->isInit) { |
|
113
|
|
|
return; |
|
114
|
2 |
|
} |
|
115
|
1 |
|
// dispatch an event to allow configuration modification before resolving and creating admins |
|
116
|
|
|
$event = new BeforeConfigurationEvent($this->adminConfigurations); |
|
117
|
|
|
$this |
|
118
|
2 |
|
->eventDispatcher |
|
119
|
2 |
|
->dispatch(AdminEvents::BEFORE_CONFIGURATION, $event); |
|
120
|
|
|
|
|
121
|
2 |
|
// get the modified configuration |
|
122
|
|
|
$this->adminConfigurations = $event->getAdminConfigurations(); |
|
123
|
|
|
|
|
124
|
2 |
|
// create Admins according to the given configuration |
|
125
|
|
|
foreach ($this->adminConfigurations as $name => $configuration) { |
|
126
|
|
|
|
|
127
|
2 |
|
// dispatch an event to allow modification on a specific admin |
|
128
|
|
|
$event = new AdminCreateEvent($name, $configuration); |
|
129
|
|
|
$this |
|
130
|
1 |
|
->eventDispatcher |
|
131
|
1 |
|
->dispatch(AdminEvents::ADMIN_CREATE, $event); |
|
132
|
|
|
|
|
133
|
1 |
|
// create Admin object and add it to the registry |
|
134
|
|
|
$admin = $this->create($name, $event->getAdminConfiguration()); |
|
135
|
|
|
$this |
|
136
|
1 |
|
->adminRegistry |
|
137
|
1 |
|
->add($admin); |
|
138
|
|
|
|
|
139
|
1 |
|
// dispatch post-create event |
|
140
|
|
|
$event = new AdminCreatedEvent($admin); |
|
141
|
|
|
$this |
|
142
|
1 |
|
->eventDispatcher |
|
143
|
1 |
|
->dispatch(AdminEvents::ADMIN_CREATED, $event); |
|
144
|
|
|
} |
|
145
|
1 |
|
$this->isInit = true; |
|
146
|
2 |
|
} |
|
147
|
2 |
|
|
|
148
|
2 |
|
/** |
|
149
|
|
|
* Create an Admin from configuration values. It will be added to AdminFactory admin's list. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $name |
|
152
|
|
|
* @param array $configuration |
|
153
|
|
|
* @return Admin |
|
154
|
|
|
* @throws Exception |
|
155
|
|
|
*/ |
|
156
|
|
|
public function create($name, array $configuration) |
|
157
|
|
|
{ |
|
158
|
2 |
|
// create AdminConfiguration object |
|
159
|
|
|
$adminConfiguration = $this |
|
160
|
|
|
->configurationFactory |
|
161
|
2 |
|
->createAdminConfiguration($configuration); |
|
162
|
|
|
|
|
163
|
2 |
|
// retrieve a data provider |
|
164
|
|
|
$dataProvider = $this->getDataProvider( |
|
165
|
|
|
$adminConfiguration->getParameter('entity'), |
|
166
|
2 |
|
$adminConfiguration->getParameter('data_provider') |
|
167
|
2 |
|
); |
|
168
|
2 |
|
|
|
169
|
2 |
|
// retrieve a request filter |
|
170
|
|
|
$requestFilter = $this |
|
171
|
|
|
->requestFilterFactory |
|
172
|
2 |
|
->create($adminConfiguration); |
|
173
|
2 |
|
|
|
174
|
2 |
|
// create Admin object |
|
175
|
2 |
|
$admin = new Admin( |
|
176
|
2 |
|
$name, |
|
177
|
2 |
|
$dataProvider, |
|
178
|
2 |
|
$adminConfiguration, |
|
179
|
|
|
$this->messageHandler, |
|
180
|
|
|
$this->eventDispatcher, |
|
181
|
2 |
|
$requestFilter |
|
182
|
|
|
); |
|
183
|
2 |
|
|
|
184
|
2 |
|
// adding actions |
|
185
|
|
|
foreach ($adminConfiguration->getParameter('actions') as $actionName => $actionConfiguration) { |
|
186
|
2 |
|
// create action and add it to the admin instance |
|
187
|
|
|
$this->createAction($admin, $actionName, $actionConfiguration); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $admin; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return boolean |
|
195
|
1 |
|
*/ |
|
196
|
|
|
public function isInit() |
|
197
|
1 |
|
{ |
|
198
|
|
|
return $this->isInit; |
|
199
|
1 |
|
} |
|
200
|
1 |
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return Registry |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getRegistry() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->adminRegistry; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Create an Action from the configuration, and add it to the Admin. |
|
211
|
|
|
* |
|
212
|
|
|
* @param AdminInterface $admin |
|
213
|
3 |
|
* @param $actionName |
|
214
|
|
|
* @param array $actionConfiguration |
|
215
|
3 |
|
*/ |
|
216
|
|
|
protected function createAction(AdminInterface $admin, $actionName, array $actionConfiguration) |
|
217
|
|
|
{ |
|
218
|
|
|
// creating action from configuration |
|
219
|
|
|
$action = $this |
|
220
|
|
|
->actionFactory |
|
221
|
|
|
->create($actionName, $actionConfiguration, $admin); |
|
222
|
|
|
|
|
223
|
|
|
// adding action to admin |
|
224
|
|
|
$admin->addAction($action); |
|
225
|
2 |
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
2 |
|
* Return a configured data provider or create an new instance of the default one. |
|
229
|
|
|
* |
|
230
|
2 |
|
* @param string $entityClass |
|
231
|
|
|
* @param string|null $name |
|
232
|
|
|
* @return DataProviderInterface |
|
233
|
2 |
|
* @throws Exception |
|
234
|
2 |
|
*/ |
|
235
|
|
|
protected function getDataProvider($entityClass, $name = null) |
|
236
|
|
|
{ |
|
237
|
|
|
return $this |
|
238
|
|
|
->dataProviderFactory |
|
239
|
|
|
->get($name, $entityClass); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|