|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2018 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
15
|
|
|
use Psr\Container\ContainerInterface; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BaseController; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
20
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
21
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
22
|
|
|
use Symfony\Contracts\EventDispatcher\Event; |
|
23
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
24
|
|
|
use Throwable; |
|
25
|
|
|
use Twig\Environment; |
|
26
|
|
|
use WBW\Bundle\CoreBundle\Event\NotificationEvent; |
|
27
|
|
|
use WBW\Bundle\CoreBundle\Event\ToastEvent; |
|
28
|
|
|
use WBW\Bundle\CoreBundle\EventDispatcher\EventDispatcherHelper; |
|
29
|
|
|
use WBW\Bundle\CoreBundle\EventListener\KernelEventListener; |
|
30
|
|
|
use WBW\Bundle\CoreBundle\Exception\BadUserRoleException; |
|
31
|
|
|
use WBW\Bundle\CoreBundle\Model\User; |
|
32
|
|
|
use WBW\Bundle\CoreBundle\Security\Core\User\UserHelper; |
|
33
|
|
|
use WBW\Bundle\CoreBundle\Service\CompatibilityService; |
|
34
|
|
|
use WBW\Library\Symfony\Assets\NotificationInterface; |
|
35
|
|
|
use WBW\Library\Symfony\Assets\ToastInterface; |
|
36
|
|
|
use WBW\Library\Symfony\Response\DefaultJsonResponseData; |
|
37
|
|
|
use WBW\Library\Symfony\Response\DefaultJsonResponseDataInterface; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Abstract controller. |
|
41
|
|
|
* |
|
42
|
|
|
* @author webeweb <https://github.com/webeweb> |
|
43
|
|
|
* @package WBW\Bundle\CoreBundle\Controller |
|
44
|
|
|
* @abstract |
|
45
|
|
|
*/ |
|
46
|
|
|
abstract class AbstractController extends BaseController { |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Dispatch an event. |
|
50
|
40 |
|
* |
|
51
|
40 |
|
* @param string $eventName The event name. |
|
52
|
40 |
|
* @param Event $event The event. |
|
53
|
|
|
* @return Event|null Returns the event. |
|
54
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function dispatchEvent(string $eventName, Event $event): Event { |
|
57
|
|
|
|
|
58
|
|
|
$this->getLogger()->debug(sprintf('A controller dispatch an event with name "%s"', $eventName), [ |
|
59
|
|
|
"_controller" => get_class($this), |
|
60
|
10 |
|
"_event" => get_class($event), |
|
61
|
10 |
|
]); |
|
62
|
|
|
|
|
63
|
|
|
return EventDispatcherHelper::dispatch($this->getEventDispatcher(), $eventName, $event); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the container. |
|
68
|
|
|
* |
|
69
|
50 |
|
* @return ContainerInterface|null Returns the container. |
|
70
|
50 |
|
*/ |
|
71
|
|
|
protected function getContainer(): ?ContainerInterface { |
|
72
|
|
|
return $this->container; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the entity manager. |
|
77
|
|
|
* |
|
78
|
10 |
|
* @return EntityManagerInterface|null Returns the entity manager. |
|
79
|
10 |
|
* @throws Throwable Throws an exception if an error occurs. |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getEntityManager(): ?EntityManagerInterface { |
|
82
|
|
|
return $this->container->get("doctrine.orm.manager"); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the event dispatcher. |
|
87
|
30 |
|
* |
|
88
|
30 |
|
* @return EventDispatcherInterface|null Returns the event dispatcher. |
|
89
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getEventDispatcher(): ?EventDispatcherInterface { |
|
92
|
|
|
return $this->container->get("event_dispatcher"); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
50 |
|
* Get the kernel event listener. |
|
97
|
50 |
|
* |
|
98
|
|
|
* @return KernelEventListener|null Returns the kernel event listener. |
|
99
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function getKernelEventListener(): ?KernelEventListener { |
|
102
|
|
|
return $this->container->get(KernelEventListener::SERVICE_NAME); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
10 |
|
/** |
|
106
|
10 |
|
* Get the logger. |
|
107
|
|
|
* |
|
108
|
|
|
* @return LoggerInterface|null Returns the logger. |
|
109
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getLogger(): ?LoggerInterface { |
|
112
|
|
|
return $this->container->get("logger"); |
|
113
|
|
|
} |
|
114
|
10 |
|
|
|
115
|
10 |
|
/** |
|
116
|
|
|
* Get the mailer. |
|
117
|
|
|
* |
|
118
|
|
|
* @return MailerInterface|null Returns the mailer. |
|
119
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function getMailer(): ?MailerInterface { |
|
122
|
|
|
return $this->container->get("mailer"); |
|
123
|
10 |
|
} |
|
124
|
10 |
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get the router. |
|
127
|
|
|
* |
|
128
|
|
|
* @return RouterInterface|null Returns the router. |
|
129
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getRouter(): ?RouterInterface { |
|
132
|
20 |
|
return $this->container->get("router"); |
|
133
|
20 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get the session. |
|
137
|
|
|
* |
|
138
|
|
|
* @return SessionInterface|null Returns the session. |
|
139
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function getSession(): ?SessionInterface { |
|
142
|
|
|
|
|
143
|
|
|
/** @var CompatibilityService $service */ |
|
144
|
|
|
$service = $this->container->get(CompatibilityService::SERVICE_NAME); |
|
145
|
|
|
|
|
146
|
20 |
|
return null === $service ? null : $service->getSession(); |
|
147
|
|
|
} |
|
148
|
20 |
|
|
|
149
|
20 |
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
10 |
|
public static function getSubscribedServices(): array { |
|
153
|
10 |
|
|
|
154
|
|
|
return array_merge([ |
|
155
|
|
|
"doctrine.orm.manager" => "?" . EntityManagerInterface::class, |
|
156
|
10 |
|
"event_dispatcher" => "?" . EventDispatcherInterface::class, |
|
157
|
|
|
"logger" => "?" . LoggerInterface::class, |
|
158
|
|
|
"mailer" => "?" . MailerInterface::class, |
|
159
|
|
|
"translator" => "?" . TranslatorInterface::class, |
|
160
|
|
|
KernelEventListener::SERVICE_NAME => "?" . KernelEventListener::class, |
|
161
|
|
|
CompatibilityService::SERVICE_NAME => "?" . CompatibilityService::class, |
|
162
|
|
|
], parent::getSubscribedServices()); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
20 |
|
* Get the translator. |
|
167
|
20 |
|
* |
|
168
|
|
|
* @return TranslatorInterface|null Returns the translator. |
|
169
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function getTranslator(): TranslatorInterface { |
|
172
|
|
|
return $this->container->get("translator"); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Get Twig. |
|
177
|
20 |
|
* |
|
178
|
20 |
|
* @return Environment|null Returns Twig. |
|
179
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function getTwig(): ?Environment { |
|
182
|
|
|
return $this->container->get("twig"); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Determines if the connected user has roles or redirect. |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $roles The roles. |
|
189
|
|
|
* @param bool $or OR ? |
|
190
|
10 |
|
* @param string $redirectUrl The redirect URL. |
|
191
|
10 |
|
* @param string $originUrl The origin URL. |
|
192
|
|
|
* @return void |
|
193
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
194
|
|
|
* @throws BadUserRoleException Throws a bad user role exception. |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function hasRolesOrRedirect(array $roles, bool $or, string $redirectUrl, string $originUrl = ""): void { |
|
197
|
|
|
|
|
198
|
|
|
$user = $this->getKernelEventListener()->getUser(); |
|
199
|
|
|
if (false === UserHelper::hasRoles($user, $roles, $or)) { |
|
200
|
|
|
|
|
201
|
|
|
// Throw a bad user role exception with an anonymous user if user is null. |
|
202
|
|
|
$user = null === $user ? new User("anonymous", "") : $user; |
|
203
|
|
|
throw new BadUserRoleException($user, $roles, $redirectUrl, $originUrl); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Creates a default JSON response data. |
|
209
|
|
|
* |
|
210
|
|
|
* @param bool $success The success. |
|
211
|
|
|
* @param array $data The data. |
|
212
|
|
|
* @param string|null $message The message. |
|
213
|
|
|
* @return DefaultJsonResponseDataInterface Returns the default JSON response data. |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function newDefaultJsonResponseData(bool $success, array $data, string $message = null): DefaultJsonResponseDataInterface { |
|
216
|
|
|
|
|
217
|
|
|
$model = new DefaultJsonResponseData(); |
|
218
|
|
|
$model->setSuccess($success); |
|
219
|
|
|
$model->setData($data); |
|
220
|
|
|
$model->setMessage($message); |
|
221
|
|
|
|
|
222
|
|
|
return $model; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Notify. |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $eventName The event name. |
|
229
|
|
|
* @param NotificationInterface $notification The notification. |
|
230
|
|
|
* @return NotificationEvent|null Returns the event. |
|
231
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function notify(string $eventName, NotificationInterface $notification): ?NotificationEvent { |
|
234
|
|
|
return $this->dispatchEvent($eventName, new NotificationEvent($eventName, $notification)); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Toast. |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $eventName The event name. |
|
241
|
|
|
* @param ToastInterface $toast The toast. |
|
242
|
|
|
* @return ToastEvent|null Returns the event. |
|
243
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
244
|
|
|
*/ |
|
245
|
|
|
protected function toast(string $eventName, ToastInterface $toast): ?ToastEvent { |
|
246
|
|
|
return $this->dispatchEvent($eventName, new ToastEvent($eventName, $toast)); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Translate. |
|
251
|
|
|
* |
|
252
|
|
|
* @param string $id The id. |
|
253
|
|
|
* @param array $parameters The parameters. |
|
254
|
|
|
* @param string|null $domain The domain. |
|
255
|
|
|
* @param string|null $locale The locale. |
|
256
|
|
|
* @return string Returns the translation in case of success, $id otherwise. |
|
257
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
|
258
|
|
|
*/ |
|
259
|
|
|
protected function translate(string $id, array $parameters = [], string $domain = null, string $locale = null): string { |
|
260
|
|
|
return $this->getTranslator()->trans($id, $parameters, $domain, $locale); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|