|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Neimheadh\SonataAnnotationBundle\Admin; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Neimheadh\SonataAnnotationBundle\Annotation\AddRoute; |
|
9
|
|
|
use Neimheadh\SonataAnnotationBundle\Annotation\RemoveRoute; |
|
10
|
|
|
use Neimheadh\SonataAnnotationBundle\Reader\ActionButtonReader; |
|
11
|
|
|
use Neimheadh\SonataAnnotationBundle\Reader\DashboardActionReader; |
|
12
|
|
|
use Neimheadh\SonataAnnotationBundle\Reader\DatagridReader; |
|
13
|
|
|
use Neimheadh\SonataAnnotationBundle\Reader\DatagridValuesReader; |
|
14
|
|
|
use Neimheadh\SonataAnnotationBundle\Reader\ExportReader; |
|
15
|
|
|
use Neimheadh\SonataAnnotationBundle\Reader\FormReader; |
|
16
|
|
|
use Neimheadh\SonataAnnotationBundle\Reader\ListReader; |
|
17
|
|
|
use Neimheadh\SonataAnnotationBundle\Reader\RouteReader; |
|
18
|
|
|
use Neimheadh\SonataAnnotationBundle\Reader\ShowReader; |
|
19
|
|
|
use ReflectionClass; |
|
20
|
|
|
use ReflectionException; |
|
21
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
|
22
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
|
23
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
|
24
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
|
25
|
|
|
use Sonata\AdminBundle\Route\RouteCollectionInterface; |
|
26
|
|
|
use Sonata\AdminBundle\Show\ShowMapper; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Auto-created admin class by annotations. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Marko Kunic <[email protected]> |
|
32
|
|
|
* @author Mathieu Wambre <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
class AnnotationAdmin extends AbstractAdmin |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Action buttons annotation reader. |
|
39
|
|
|
* |
|
40
|
|
|
* @var ActionButtonReader |
|
41
|
|
|
*/ |
|
42
|
|
|
private ActionButtonReader $actionButtonReader; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Datagrid annotation reader. |
|
46
|
|
|
* |
|
47
|
|
|
* @var DatagridReader |
|
48
|
|
|
*/ |
|
49
|
|
|
private DatagridReader $datagridReader; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Datagrid values annotation reader. |
|
53
|
|
|
* |
|
54
|
|
|
* @var DatagridValuesReader |
|
55
|
|
|
* @todo Replace the previously buildDatagrid() to use datagrid values |
|
56
|
|
|
* reader. |
|
57
|
|
|
*/ |
|
58
|
|
|
private DatagridValuesReader $datagridValuesReader; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Dashboard actions annotation reader. |
|
62
|
|
|
* |
|
63
|
|
|
* @var DashboardActionReader |
|
64
|
|
|
*/ |
|
65
|
|
|
private DashboardActionReader $dashboardActionReader; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Export annotation reader. |
|
69
|
|
|
* |
|
70
|
|
|
* @var ExportReader |
|
71
|
|
|
*/ |
|
72
|
|
|
private ExportReader $exportReader; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Form page annotation reader. |
|
76
|
|
|
* |
|
77
|
|
|
* @var FormReader |
|
78
|
|
|
*/ |
|
79
|
|
|
private FormReader $formReader; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* List page annotation reader. |
|
83
|
|
|
* |
|
84
|
|
|
* @var ListReader |
|
85
|
|
|
*/ |
|
86
|
|
|
private ListReader $listReader; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Route configuration annotation reader. |
|
90
|
|
|
* |
|
91
|
|
|
* @var RouteReader |
|
92
|
|
|
*/ |
|
93
|
|
|
private RouteReader $routeReader; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Show page annotation reader. |
|
97
|
|
|
* |
|
98
|
|
|
* @var ShowReader |
|
99
|
|
|
*/ |
|
100
|
|
|
private ShowReader $showReader; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param ActionButtonReader $actionButtonReader Action buttons |
|
104
|
|
|
* annotation reader. |
|
105
|
|
|
* @param DatagridReader $datagridReader Datagrid annotation |
|
106
|
|
|
* reader. |
|
107
|
|
|
* @param DatagridValuesReader $datagridValuesReader Datagrid values |
|
108
|
|
|
* annotation reader. |
|
109
|
|
|
* @param DashboardActionReader $dashboardActionReader Dashboard actions |
|
110
|
|
|
* annotation reader. |
|
111
|
|
|
* @param ExportReader $exportReader Export annotation |
|
112
|
|
|
* reader. |
|
113
|
|
|
* @param FormReader $formReader Form page annotation |
|
114
|
|
|
* reader. |
|
115
|
|
|
* @param ListReader $listReader List page annotation |
|
116
|
|
|
* reader. |
|
117
|
|
|
* @param RouteReader $routeReader Route configuration |
|
118
|
|
|
* annotation reader. |
|
119
|
|
|
* @param ShowReader $showReader Show page annotation |
|
120
|
|
|
* reader. |
|
121
|
|
|
*/ |
|
122
|
|
|
public function __construct( |
|
123
|
|
|
ActionButtonReader $actionButtonReader, |
|
124
|
|
|
DatagridReader $datagridReader, |
|
125
|
|
|
DatagridValuesReader $datagridValuesReader, |
|
126
|
|
|
DashboardActionReader $dashboardActionReader, |
|
127
|
|
|
ExportReader $exportReader, |
|
128
|
|
|
FormReader $formReader, |
|
129
|
|
|
ListReader $listReader, |
|
130
|
|
|
RouteReader $routeReader, |
|
131
|
|
|
ShowReader $showReader |
|
132
|
|
|
) { |
|
133
|
|
|
parent::__construct(); |
|
134
|
|
|
|
|
135
|
|
|
$this->actionButtonReader = $actionButtonReader; |
|
136
|
|
|
$this->datagridReader = $datagridReader; |
|
137
|
|
|
$this->datagridValuesReader = $datagridValuesReader; |
|
138
|
|
|
$this->dashboardActionReader = $dashboardActionReader; |
|
139
|
|
|
$this->listReader = $listReader; |
|
140
|
|
|
$this->exportReader = $exportReader; |
|
141
|
|
|
$this->formReader = $formReader; |
|
142
|
|
|
$this->routeReader = $routeReader; |
|
143
|
|
|
$this->showReader = $showReader; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the list of exported formats. |
|
148
|
|
|
* |
|
149
|
|
|
* @return array<string> |
|
150
|
|
|
* @throws ReflectionException |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getExportFormats(): array |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->exportReader->getFormats( |
|
155
|
|
|
$this->getReflectionClass() |
|
156
|
|
|
) ?: parent::getExportFormats(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Configure action buttons. |
|
161
|
|
|
* |
|
162
|
|
|
* @param array $buttonList Base button list. |
|
163
|
|
|
* @param string $action Current action. |
|
164
|
|
|
* @param object|null $object Current object. |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
* @throws ReflectionException |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function configureActionButtons( |
|
170
|
|
|
array $buttonList, |
|
171
|
|
|
string $action, |
|
172
|
|
|
?object $object = null |
|
173
|
|
|
): array { |
|
174
|
|
|
return $this->actionButtonReader |
|
175
|
|
|
->getActions( |
|
176
|
|
|
$this->getReflectionClass(), |
|
177
|
|
|
parent::configureActionButtons( |
|
178
|
|
|
$buttonList, |
|
179
|
|
|
$action, |
|
180
|
|
|
$object |
|
181
|
|
|
) |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Configure dashboard actions. |
|
187
|
|
|
* |
|
188
|
|
|
* @param array<string, array<string, mixed>> $actions Base actions. |
|
189
|
|
|
* |
|
190
|
|
|
* @return array<string, array<string, mixed>> |
|
191
|
|
|
* @throws ReflectionException |
|
192
|
|
|
* @throws Exception |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function configureDashboardActions(array $actions): array |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->dashboardActionReader |
|
197
|
|
|
->getActions( |
|
198
|
|
|
$this->getReflectionClass(), |
|
199
|
|
|
parent::configureDashboardActions($actions) |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Configure datagrid filters. |
|
205
|
|
|
* |
|
206
|
|
|
* @param DatagridMapper $filter Datagrid filter mapper. |
|
207
|
|
|
* |
|
208
|
|
|
* @return void |
|
209
|
|
|
* @throws ReflectionException |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function configureDatagridFilters(DatagridMapper $filter): void |
|
212
|
|
|
{ |
|
213
|
|
|
$this->datagridReader->configureFields( |
|
214
|
|
|
$this->getReflectionClass(), |
|
215
|
|
|
$filter |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Get exported fields. |
|
221
|
|
|
* |
|
222
|
|
|
* @return array<string> |
|
223
|
|
|
* @throws ReflectionException |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function configureExportFields(): array |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->exportReader->getFields( |
|
228
|
|
|
$this->getReflectionClass() |
|
229
|
|
|
) ?: parent::getExportFields(); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Configure form page fields. |
|
234
|
|
|
* |
|
235
|
|
|
* @param FormMapper $form Form mapper. |
|
236
|
|
|
* |
|
237
|
|
|
* @return void |
|
238
|
|
|
* @throws ReflectionException |
|
239
|
|
|
*/ |
|
240
|
|
|
protected function configureFormFields(FormMapper $form): void |
|
241
|
|
|
{ |
|
242
|
|
|
if ($this->getRequest()->get($this->getIdParameter())) { |
|
243
|
|
|
$this->formReader->configureEditFields( |
|
244
|
|
|
$this->getReflectionClass(), |
|
245
|
|
|
$form |
|
246
|
|
|
); |
|
247
|
|
|
return; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
$this->formReader->configureCreateFields( |
|
251
|
|
|
$this->getReflectionClass(), |
|
252
|
|
|
$form |
|
253
|
|
|
); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Configure list page fields. |
|
258
|
|
|
* |
|
259
|
|
|
* @param ListMapper $list List mapper. |
|
260
|
|
|
* |
|
261
|
|
|
* @return void |
|
262
|
|
|
* @throws ReflectionException |
|
263
|
|
|
*/ |
|
264
|
|
|
protected function configureListFields(ListMapper $list): void |
|
265
|
|
|
{ |
|
266
|
|
|
$this->listReader |
|
267
|
|
|
->configureFields( |
|
268
|
|
|
$this->getReflectionClass(), |
|
269
|
|
|
$list |
|
270
|
|
|
); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Configure routes. |
|
275
|
|
|
* |
|
276
|
|
|
* @param RouteCollectionInterface $collection Route collection. |
|
277
|
|
|
* |
|
278
|
|
|
* @return void |
|
279
|
|
|
* @throws ReflectionException |
|
280
|
|
|
*/ |
|
281
|
|
|
protected function configureRoutes( |
|
282
|
|
|
RouteCollectionInterface $collection |
|
283
|
|
|
): void { |
|
284
|
|
|
[$addRoutes, $removeRoutes] = $this->routeReader->getRoutes( |
|
285
|
|
|
$this->getReflectionClass() |
|
286
|
|
|
); |
|
287
|
|
|
|
|
288
|
|
|
/** @var AddRoute $route */ |
|
289
|
|
|
foreach ($addRoutes as $route) { |
|
290
|
|
|
$collection->add( |
|
291
|
|
|
$route->name, |
|
292
|
|
|
$route->path ? $this->replaceIdParameterInRoutePath( |
|
293
|
|
|
$route->path |
|
294
|
|
|
) : $route->getName() |
|
295
|
|
|
); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** @var RemoveRoute $route */ |
|
299
|
|
|
foreach ($removeRoutes as $route) { |
|
300
|
|
|
$collection->remove($route->name); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Configure show page fields. |
|
306
|
|
|
* |
|
307
|
|
|
* @param ShowMapper $show Show mapper. |
|
308
|
|
|
* |
|
309
|
|
|
* @return void |
|
310
|
|
|
* @throws ReflectionException |
|
311
|
|
|
*/ |
|
312
|
|
|
protected function configureShowFields(ShowMapper $show): void |
|
313
|
|
|
{ |
|
314
|
|
|
$this->showReader->configureFields( |
|
315
|
|
|
$this->getReflectionClass(), |
|
316
|
|
|
$show |
|
317
|
|
|
); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Replace the '{id}' part in the given path with the currently managed |
|
322
|
|
|
* object id. |
|
323
|
|
|
* |
|
324
|
|
|
* @param string $path Path template. |
|
325
|
|
|
* |
|
326
|
|
|
* @return string |
|
327
|
|
|
*/ |
|
328
|
|
|
private function replaceIdParameterInRoutePath(string $path): string |
|
329
|
|
|
{ |
|
330
|
|
|
return str_replace( |
|
331
|
|
|
AddRoute::ID_PARAMETER, |
|
332
|
|
|
$this->getRouterIdParameter(), |
|
333
|
|
|
$path |
|
334
|
|
|
); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Get the reflection class of the current admin model. |
|
339
|
|
|
* |
|
340
|
|
|
* @return ReflectionClass |
|
341
|
|
|
* @throws ReflectionException |
|
342
|
|
|
*/ |
|
343
|
|
|
private function getReflectionClass(): ReflectionClass |
|
344
|
|
|
{ |
|
345
|
|
|
return new ReflectionClass($this->getClass()); |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
} |
|
349
|
|
|
|