1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Core\Controller; |
4
|
|
|
|
5
|
|
|
use App\Security\Entity\User; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Silex\Application; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
9
|
|
|
use Symfony\Component\Form\FormFactory; |
10
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
11
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
12
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
13
|
|
|
use Symfony\Component\Routing\Generator\UrlGenerator; |
14
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
15
|
|
|
use Symfony\Component\Validator\Validator\RecursiveValidator; |
16
|
|
|
use Twig\Environment; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @property EventDispatcher dispatcher |
20
|
|
|
* @property RecursiveValidator validator |
21
|
|
|
* @property Session session |
22
|
|
|
* @property Environment twig |
23
|
|
|
* @property UrlGenerator url_generator |
24
|
|
|
* @property string environment |
25
|
|
|
* @property string root_dir |
26
|
|
|
*/ |
27
|
|
|
class Controller |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Application |
31
|
|
|
*/ |
32
|
|
|
protected $application; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param Application $app |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Application $app) |
40
|
|
|
{ |
41
|
|
|
$this->application = $app; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns a new AccessDeniedException. |
46
|
|
|
* |
47
|
|
|
* @param string $message |
48
|
|
|
* |
49
|
|
|
* @return AccessDeniedException |
50
|
|
|
*/ |
51
|
|
|
protected function createAccessDeniedException($message = 'Access Denied.') |
52
|
|
|
{ |
53
|
|
|
return new AccessDeniedException($message); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns a new NotFoundHttpException. |
58
|
|
|
* |
59
|
|
|
* @param string $message |
60
|
|
|
* |
61
|
|
|
* @return NotFoundHttpException |
62
|
|
|
*/ |
63
|
|
|
protected function createNotFoundHttpException($message = 'Not Found') |
64
|
|
|
{ |
65
|
|
|
return new NotFoundHttpException($message); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Throws an exception unless the attributes are granted against the current authentication token. |
70
|
|
|
* |
71
|
|
|
* @param mixed $roles |
72
|
|
|
* @param string $message |
73
|
|
|
* |
74
|
|
|
* @throws AccessDeniedException |
75
|
|
|
*/ |
76
|
|
|
protected function denyAccessUnlessGranted($roles, $message = 'Access Denied.') |
77
|
|
|
{ |
78
|
|
|
if (!$this->isGranted($roles)) { |
79
|
|
|
$exception = $this->createAccessDeniedException($message); |
80
|
|
|
$exception->setAttributes($roles); |
81
|
|
|
|
82
|
|
|
throw $exception; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets Doctrine Entity Manager. |
88
|
|
|
* |
89
|
|
|
* @return EntityManager |
90
|
|
|
*/ |
91
|
|
|
public function getEntityManager() |
92
|
|
|
{ |
93
|
|
|
return $this->application['orm.em']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets the Form Factory. |
98
|
|
|
* |
99
|
|
|
* @return FormFactory |
100
|
|
|
*/ |
101
|
|
|
public function getFormFactory() |
102
|
|
|
{ |
103
|
|
|
return $this->application['form.factory']; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Gets the project root directory. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getRootDir() |
112
|
|
|
{ |
113
|
|
|
return $this->application['root_dir']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Gets the router. |
118
|
|
|
* |
119
|
|
|
* @return UrlGenerator |
120
|
|
|
*/ |
121
|
|
|
public function getRouter() |
122
|
|
|
{ |
123
|
|
|
return $this->application['url_generator']; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Gets the session. |
128
|
|
|
* |
129
|
|
|
* @return Session |
130
|
|
|
*/ |
131
|
|
|
public function getSession() |
132
|
|
|
{ |
133
|
|
|
return $this->application['session']; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Gets the Twig service. |
138
|
|
|
* |
139
|
|
|
* @return Environment |
140
|
|
|
*/ |
141
|
|
|
public function getTwig() |
142
|
|
|
{ |
143
|
|
|
return $this->application['twig']; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Gets the current authenticated user or null if not logged in. |
148
|
|
|
* |
149
|
|
|
* @return User|null |
150
|
|
|
*/ |
151
|
|
|
public function getUser() |
152
|
|
|
{ |
153
|
|
|
$user = $this->application['security.token_storage']->getToken()->getUser(); |
154
|
|
|
if ($user instanceof User) { |
155
|
|
|
return $user; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Redirects the user to another route. |
163
|
|
|
* |
164
|
|
|
* @param string $route |
165
|
|
|
* @param array $parameters |
166
|
|
|
* @param int $status |
167
|
|
|
* |
168
|
|
|
* @return RedirectResponse |
169
|
|
|
*/ |
170
|
|
|
public function redirect($route, $parameters = [], $status = 302) |
171
|
|
|
{ |
172
|
|
|
return $this->application->redirect($this->path($route, $parameters), $status); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Redirects the user to another URL. |
177
|
|
|
* |
178
|
|
|
* @param string $url The URL to redirect to |
179
|
|
|
* @param int $status The status code (302 by default) |
180
|
|
|
* |
181
|
|
|
* @return RedirectResponse |
182
|
|
|
*/ |
183
|
|
|
public function redirectTo($url, $status = 302) |
184
|
|
|
{ |
185
|
|
|
return $this->application->redirect($url, $status); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Generates a path from the given parameters. |
190
|
|
|
* |
191
|
|
|
* @param string $route |
192
|
|
|
* @param mixed $parameters |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function path($route, $parameters = []) |
197
|
|
|
{ |
198
|
|
|
return $this->getRouter()->generate($route, $parameters, UrlGenerator::ABSOLUTE_PATH); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Generates an absolute URL from the given parameters. |
203
|
|
|
* |
204
|
|
|
* @param string $route |
205
|
|
|
* @param mixed $parameters |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function url($route, $parameters = []) |
210
|
|
|
{ |
211
|
|
|
return $this->getRouter()->generate($route, $parameters, UrlGenerator::ABSOLUTE_URL); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Renders a twig template. |
216
|
|
|
* |
217
|
|
|
* @param string $name |
218
|
|
|
* @param array $context |
219
|
|
|
* |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
public function render($name, array $context = []) |
223
|
|
|
{ |
224
|
|
|
return $this->getTwig()->render($name, $context); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Adds a flash message. |
229
|
|
|
* |
230
|
|
|
* @param string $type |
231
|
|
|
* @param string $message |
232
|
|
|
*/ |
233
|
|
|
public function flash($type, $message) |
234
|
|
|
{ |
235
|
|
|
$this->getSession()->getFlashBag()->add($type, $message); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Checks if user is granted a role. |
240
|
|
|
* |
241
|
|
|
* @param string $role |
242
|
|
|
* |
243
|
|
|
* @return bool |
244
|
|
|
*/ |
245
|
|
|
public function isGranted($role) |
246
|
|
|
{ |
247
|
|
|
return $this->application['security.authorization_checker']->isGranted($role); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get a service from the container |
252
|
|
|
* |
253
|
|
|
* @param string $service |
254
|
|
|
* |
255
|
|
|
* @return mixed |
256
|
|
|
*/ |
257
|
|
|
public function get($service) |
258
|
|
|
{ |
259
|
|
|
return $this->application[$service]; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Gets a service from the container. |
264
|
|
|
* |
265
|
|
|
* @param string $property |
266
|
|
|
* |
267
|
|
|
* @return mixed |
268
|
|
|
*/ |
269
|
|
|
public function __get($property) |
270
|
|
|
{ |
271
|
|
|
return $this->application[$property]; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|