1 | <?php |
||
28 | class AdminFactory |
||
29 | { |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $admins = []; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $isInit = false; |
||
39 | |||
40 | /** |
||
41 | * @var EventDispatcherInterface |
||
42 | */ |
||
43 | protected $eventDispatcher; |
||
44 | |||
45 | /** |
||
46 | * @var EntityManager |
||
47 | */ |
||
48 | protected $entityManager; |
||
49 | |||
50 | /** |
||
51 | * @var ApplicationConfiguration |
||
52 | */ |
||
53 | protected $application; |
||
54 | |||
55 | /** |
||
56 | * User custom data provider, indexed by service id |
||
57 | * |
||
58 | * @var ParameterBagInterface |
||
59 | */ |
||
60 | protected $dataProviders; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $adminConfiguration; |
||
66 | |||
67 | /** |
||
68 | * @var ActionFactory |
||
69 | */ |
||
70 | protected $actionFactory; |
||
71 | |||
72 | /** |
||
73 | * @var MessageHandlerInterface |
||
74 | */ |
||
75 | protected $messageHandler; |
||
76 | |||
77 | /** |
||
78 | * AdminFactory constructor. |
||
79 | * |
||
80 | * @param EventDispatcherInterface $eventDispatcher |
||
81 | * @param EntityManager $entityManager |
||
82 | * @param ApplicationConfiguration $application |
||
83 | * @param array $adminConfiguration |
||
84 | * @param ActionFactory $actionFactory |
||
85 | * @param MessageHandlerInterface $messageHandler |
||
86 | */ |
||
87 | 6 | public function __construct( |
|
103 | |||
104 | /** |
||
105 | * Create admins from configuration and load them into the pool. Dispatch ADMIN_CREATE event. |
||
106 | */ |
||
107 | 4 | public function init() |
|
108 | { |
||
109 | 4 | if ($this->isInit) { |
|
110 | return; |
||
111 | } |
||
112 | 4 | $event = new AdminFactoryEvent(); |
|
113 | 4 | $event->setAdminsConfiguration($this->adminConfiguration); |
|
114 | 4 | $this->eventDispatcher->dispatch(AdminFactoryEvent::ADMIN_CREATION, $event); |
|
115 | 4 | $this->adminConfiguration = $event->getAdminsConfiguration(); |
|
116 | |||
117 | 4 | foreach ($this->adminConfiguration as $name => $configuration) { |
|
118 | 4 | $event = new AdminEvent(); |
|
119 | 4 | $event->setConfiguration($configuration); |
|
120 | 4 | $this->eventDispatcher->dispatch(AdminEvent::ADMIN_CREATE, $event); |
|
121 | 4 | $this->admins[$name] = $this->create($name, $event->getConfiguration()); |
|
122 | } |
||
123 | 4 | $this->isInit = true; |
|
124 | 4 | } |
|
125 | |||
126 | /** |
||
127 | * Create an Admin from configuration values. It will be added to AdminFactory admin's list. |
||
128 | * |
||
129 | * @param string $name |
||
130 | * @param array $configuration |
||
131 | * @return Admin |
||
132 | * @throws Exception |
||
133 | */ |
||
134 | 5 | public function create($name, array $configuration) |
|
135 | { |
||
136 | // resolve admin configuration |
||
137 | 5 | $configuration = $this->resolveConfiguration($configuration); |
|
138 | // retrieve metadata from entity manager |
||
139 | 5 | $configuration['metadata'] = $this |
|
140 | 5 | ->entityManager |
|
141 | 5 | ->getClassMetadata($configuration['entity']); |
|
142 | // create AdminConfiguration object |
||
143 | 5 | $adminConfiguration = new AdminConfiguration($configuration); |
|
144 | |||
145 | // retrieve a data provider |
||
146 | 5 | $dataProvider = $this->getDataProvider( |
|
147 | 5 | $adminConfiguration->getEntityName(), |
|
148 | 5 | $adminConfiguration->getDataProvider() |
|
149 | ); |
||
150 | |||
151 | // create Admin object |
||
152 | 5 | $admin = new Admin( |
|
153 | $name, |
||
154 | $dataProvider, |
||
155 | $adminConfiguration, |
||
156 | 5 | $this->messageHandler |
|
157 | ); |
||
158 | // adding actions |
||
159 | 5 | foreach ($adminConfiguration->getActions() as $actionName => $actionConfiguration) { |
|
160 | // dispatching action create event for dynamic action creation |
||
161 | 5 | $event = new AdminEvent(); |
|
162 | 5 | $event->setConfiguration($actionConfiguration); |
|
163 | 5 | $event->setAdmin($admin); |
|
164 | 5 | $event->setActionName($actionName); |
|
165 | 5 | $this->eventDispatcher->dispatch(AdminEvent::ACTION_CREATE, $event); |
|
166 | // creating action from configuration |
||
167 | $action = $this |
||
168 | 5 | ->actionFactory |
|
169 | 5 | ->create($actionName, $event->getConfiguration(), $admin); |
|
170 | // adding action to admin |
||
171 | 5 | $admin->addAction($action); |
|
172 | } |
||
173 | 5 | return $admin; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Return an admin from a Symfony request. |
||
178 | * |
||
179 | * @param Request $request |
||
180 | * @return AdminInterface |
||
181 | * @throws Exception |
||
182 | */ |
||
183 | 1 | public function getAdminFromRequest(Request $request) |
|
200 | |||
201 | /** |
||
202 | * Return a admin by its name. |
||
203 | * |
||
204 | * @param $name |
||
205 | * @return Admin |
||
206 | * @throws Exception |
||
207 | */ |
||
208 | 5 | public function getAdmin($name) |
|
216 | |||
217 | /** |
||
218 | * Return all admins. |
||
219 | * |
||
220 | * @return Admin[] |
||
221 | */ |
||
222 | 1 | public function getAdmins() |
|
226 | |||
227 | /** |
||
228 | * Add user custom repositories (called in the repository compiler pass), to avoid injecting the service container |
||
229 | * |
||
230 | * @param string $name |
||
231 | * @param DataProviderInterface $dataProvider |
||
232 | */ |
||
233 | 1 | public function addDataProvider($name, DataProviderInterface $dataProvider) |
|
239 | |||
240 | /** |
||
241 | * Return a configured data provider or create an new instance of the default one. |
||
242 | * |
||
243 | * @param string $entityClass |
||
244 | * @param string|null $name |
||
245 | * @return DataProvider|mixed |
||
246 | * @throws Exception |
||
247 | */ |
||
248 | 5 | protected function getDataProvider($entityClass, $name = null) |
|
277 | |||
278 | /** |
||
279 | * Resolve admin configuration. |
||
280 | * |
||
281 | * @param array $configuration |
||
282 | * @return array |
||
283 | */ |
||
284 | 5 | protected function resolveConfiguration(array $configuration) |
|
311 | } |
||
312 |