1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use AWurth\SilexUser\Entity\UserInterface; |
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\Routing\Generator\UrlGenerator; |
13
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
14
|
|
|
use Symfony\Component\Validator\Validator\RecursiveValidator; |
15
|
|
|
use Twig_Environment; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @property EventDispatcher dispatcher |
19
|
|
|
* @property RecursiveValidator validator |
20
|
|
|
* @property Session session |
21
|
|
|
* @property Twig_Environment twig |
22
|
|
|
* @property UrlGenerator url_generator |
23
|
|
|
* @property string root_dir |
24
|
|
|
*/ |
25
|
|
|
class Controller |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Application |
29
|
|
|
*/ |
30
|
|
|
protected $application; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* |
35
|
|
|
* @param Application $app |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Application $app) |
38
|
|
|
{ |
39
|
|
|
$this->application = $app; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Gets Doctrine Entity Manager. |
44
|
|
|
* |
45
|
|
|
* @return EntityManager |
46
|
|
|
*/ |
47
|
|
|
public function getEntityManager() |
48
|
|
|
{ |
49
|
|
|
return $this->application['orm.em']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets the Form Factory. |
54
|
|
|
* |
55
|
|
|
* @return FormFactory |
56
|
|
|
*/ |
57
|
|
|
public function getFormFactory() |
58
|
|
|
{ |
59
|
|
|
return $this->application['form.factory']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets the project root directory. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getRootDir() |
68
|
|
|
{ |
69
|
|
|
return $this->application['root_dir']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets the router. |
74
|
|
|
* |
75
|
|
|
* @return UrlGenerator |
76
|
|
|
*/ |
77
|
|
|
public function getRouter() |
78
|
|
|
{ |
79
|
|
|
return $this->application['url_generator']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets the session. |
84
|
|
|
* |
85
|
|
|
* @return Session |
86
|
|
|
*/ |
87
|
|
|
public function getSession() |
88
|
|
|
{ |
89
|
|
|
return $this->application['session']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Gets the Twig service. |
94
|
|
|
* |
95
|
|
|
* @return Twig_Environment |
96
|
|
|
*/ |
97
|
|
|
public function getTwig() |
98
|
|
|
{ |
99
|
|
|
return $this->application['twig']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Redirects the user to another route. |
104
|
|
|
* |
105
|
|
|
* @param string $route |
106
|
|
|
* @param array $parameters |
107
|
|
|
* @param int $status |
108
|
|
|
* |
109
|
|
|
* @return RedirectResponse |
110
|
|
|
*/ |
111
|
|
|
public function redirect($route, $parameters = [], $status = 302) |
112
|
|
|
{ |
113
|
|
|
return $this->application->redirect($this->path($route, $parameters), $status); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Redirects the user to another URL. |
118
|
|
|
* |
119
|
|
|
* @param string $url The URL to redirect to |
120
|
|
|
* @param int $status The status code (302 by default) |
121
|
|
|
* |
122
|
|
|
* @return RedirectResponse |
123
|
|
|
*/ |
124
|
|
|
public function redirectTo($url, $status = 302) |
125
|
|
|
{ |
126
|
|
|
return $this->application->redirect($url, $status); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Generates a path from the given parameters. |
131
|
|
|
* |
132
|
|
|
* @param string $route |
133
|
|
|
* @param mixed $parameters |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function path($route, $parameters = []) |
138
|
|
|
{ |
139
|
|
|
return $this->getRouter()->generate($route, $parameters, UrlGenerator::ABSOLUTE_PATH); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Generates an absolute URL from the given parameters. |
144
|
|
|
* |
145
|
|
|
* @param string $route |
146
|
|
|
* @param mixed $parameters |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function url($route, $parameters = []) |
151
|
|
|
{ |
152
|
|
|
return $this->getRouter()->generate($route, $parameters, UrlGenerator::ABSOLUTE_URL); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Renders a twig template. |
157
|
|
|
* |
158
|
|
|
* @param string $name |
159
|
|
|
* @param array $context |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function render($name, array $context = []) |
164
|
|
|
{ |
165
|
|
|
return $this->getTwig()->render($name, $context); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Adds a flash message. |
170
|
|
|
* |
171
|
|
|
* @param string $type |
172
|
|
|
* @param string $message |
173
|
|
|
*/ |
174
|
|
|
public function flash($type, $message) |
175
|
|
|
{ |
176
|
|
|
$this->getSession()->getFlashBag()->add($type, $message); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Gets the current authenticated user. |
181
|
|
|
* |
182
|
|
|
* @return UserInterface|null |
183
|
|
|
*/ |
184
|
|
|
public function getUser() |
185
|
|
|
{ |
186
|
|
|
/** @var TokenInterface $token */ |
187
|
|
|
$token = $this->application['security.token_storage']->getToken(); |
188
|
|
|
|
189
|
|
|
return null !== $token ? $token->getUser() : null; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Checks if user is granted a role. |
194
|
|
|
* |
195
|
|
|
* @param string $role |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
|
|
public function isGranted($role) |
200
|
|
|
{ |
201
|
|
|
return $this->application['security.authorization_checker']->isGranted($role); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get a service from the container |
206
|
|
|
* |
207
|
|
|
* @param string $service |
208
|
|
|
* |
209
|
|
|
* @return mixed |
210
|
|
|
*/ |
211
|
|
|
public function get($service) |
212
|
|
|
{ |
213
|
|
|
return $this->application[$service]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Gets a service from the container. |
218
|
|
|
* |
219
|
|
|
* @param string $property |
220
|
|
|
* |
221
|
|
|
* @return mixed |
222
|
|
|
*/ |
223
|
|
|
public function __get($property) |
224
|
|
|
{ |
225
|
|
|
return $this->application[$property]; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|