|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use LAG\AdminBundle\Configuration\AdminConfiguration; |
|
7
|
|
|
use LAG\AdminBundle\Event\Events; |
|
8
|
|
|
use LAG\AdminBundle\Event\Events\AdminEvent; |
|
9
|
|
|
use LAG\AdminBundle\Event\Events\EntityEvent; |
|
10
|
|
|
use LAG\AdminBundle\Event\Events\FilterEvent; |
|
11
|
|
|
use LAG\AdminBundle\Event\Events\FormEvent; |
|
12
|
|
|
use LAG\AdminBundle\Event\Events\ViewEvent; |
|
13
|
|
|
use LAG\AdminBundle\Exception\Exception; |
|
14
|
|
|
use LAG\AdminBundle\Resource\AdminResource; |
|
15
|
|
|
use LAG\AdminBundle\View\ViewInterface; |
|
16
|
|
|
use Symfony\Component\Form\FormInterface; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
|
19
|
|
|
|
|
20
|
|
|
class Admin implements AdminInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $name; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var AdminConfiguration |
|
29
|
|
|
*/ |
|
30
|
|
|
private $configuration; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var AdminResource |
|
34
|
|
|
*/ |
|
35
|
|
|
private $resource; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var EventDispatcherInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $eventDispatcher; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var ActionInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $action; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var ArrayCollection |
|
49
|
|
|
*/ |
|
50
|
|
|
private $entities; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var FormInterface[] |
|
54
|
|
|
*/ |
|
55
|
|
|
private $forms = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var Request |
|
59
|
|
|
*/ |
|
60
|
|
|
private $request; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Admin constructor. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct( |
|
66
|
|
|
AdminResource $resource, |
|
67
|
|
|
AdminConfiguration $configuration, |
|
68
|
|
|
EventDispatcherInterface $eventDispatcher |
|
69
|
|
|
) { |
|
70
|
|
|
$this->name = $resource->getName(); |
|
71
|
|
|
$this->configuration = $configuration; |
|
72
|
|
|
$this->resource = $resource; |
|
73
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
74
|
|
|
$this->entities = new ArrayCollection(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function handleRequest(Request $request) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->request = $request; |
|
80
|
|
|
$event = new AdminEvent($this, $request); |
|
81
|
|
|
$this->eventDispatcher->dispatch(Events::ADMIN_HANDLE_REQUEST, $event); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
if (!$event->hasAction()) { |
|
84
|
|
|
throw new Exception('The current action was not set during the dispatch of the event'); |
|
85
|
|
|
} |
|
86
|
|
|
$this->action = $event->getAction(); |
|
87
|
|
|
|
|
88
|
|
|
// Dispatch an event to allow entities to be filtered |
|
89
|
|
|
$filterEvent = new FilterEvent($this, $request); |
|
90
|
|
|
$this->eventDispatcher->dispatch(Events::ADMIN_FILTER, $filterEvent); |
|
91
|
|
|
|
|
92
|
|
|
$event = new EntityEvent($this, $request); |
|
93
|
|
|
$event->setFilters($filterEvent->getFilters()); |
|
94
|
|
|
$this->eventDispatcher->dispatch(Events::ENTITY_LOAD, $event); |
|
95
|
|
|
|
|
96
|
|
|
if (null !== $event->getEntities()) { |
|
97
|
|
|
$this->entities = $event->getEntities(); |
|
98
|
|
|
} |
|
99
|
|
|
$event = new FormEvent($this, $request); |
|
100
|
|
|
$this->eventDispatcher->dispatch(Events::ADMIN_CREATE_FORM, $event); |
|
101
|
|
|
|
|
102
|
|
|
// Merge the regular forms and the filter forms |
|
103
|
|
|
$this->forms = array_merge($filterEvent->getForms(), $event->getForms()); |
|
104
|
|
|
|
|
105
|
|
|
$this->handleEntityForm($request); |
|
106
|
|
|
$this->eventDispatcher->dispatch(Events::ADMIN_HANDLE_FORM, new FormEvent($this, $request)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getRequest(): Request |
|
110
|
|
|
{ |
|
111
|
|
|
if (null === $this->request) { |
|
112
|
|
|
throw new Exception('The handleRequest() method should be called before calling getRequest()'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this->request; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function getName(): string |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->name; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getEntityClass(): string |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->configuration->get('entity'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function getResource(): AdminResource |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->resource; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function getEventDispatcher(): EventDispatcherInterface |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->eventDispatcher; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function getConfiguration(): AdminConfiguration |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->configuration; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function createView(): ViewInterface |
|
144
|
|
|
{ |
|
145
|
|
|
if (null === $this->request) { |
|
146
|
|
|
throw new Exception('The handleRequest() method should be called before creating a view'); |
|
147
|
|
|
} |
|
148
|
|
|
$event = new ViewEvent($this, $this->request); |
|
149
|
|
|
$this->eventDispatcher->dispatch(Events::ADMIN_VIEW, $event); |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
return $event->getView(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getAction(): ActionInterface |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->action; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function hasAction(): bool |
|
160
|
|
|
{ |
|
161
|
|
|
return null !== $this->action; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function getEntities() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->entities; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function getForms(): array |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->forms; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function hasForm(string $name): bool |
|
175
|
|
|
{ |
|
176
|
|
|
return key_exists($name, $this->forms); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function getForm(string $name): FormInterface |
|
180
|
|
|
{ |
|
181
|
|
|
if (!$this->hasForm($name)) { |
|
182
|
|
|
throw new Exception('Form "'.$name.'" does not exists in Admin "'.$this->name.'"'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $this->forms[$name]; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
private function handleEntityForm(Request $request) |
|
189
|
|
|
{ |
|
190
|
|
|
if (!key_exists('entity', $this->forms)) { |
|
191
|
|
|
return; |
|
192
|
|
|
} |
|
193
|
|
|
$form = $this->forms['entity']; |
|
194
|
|
|
$form->handleRequest($request); |
|
195
|
|
|
|
|
196
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
197
|
|
|
if ($this->entities->isEmpty()) { |
|
198
|
|
|
$this->entities->add($form->getData()); |
|
199
|
|
|
} |
|
200
|
|
|
$event = new EntityEvent($this, $request); |
|
201
|
|
|
$this->eventDispatcher->dispatch(Events::ENTITY_SAVE, $event); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.