|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the awurth/silex-user package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexis Wurth <[email protected]> |
|
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 AWurth\Silex\User\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use AWurth\Silex\User\Model\UserInterface; |
|
15
|
|
|
use AWurth\Silex\User\Model\UserManagerInterface; |
|
16
|
|
|
use Silex\Application; |
|
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
18
|
|
|
use Symfony\Component\Form\FormInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
21
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
22
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Silex User base Controller. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Alexis Wurth <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class Controller |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Application |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $application; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Application $app |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(Application $app) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->application = $app; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Creates a new form. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $type |
|
50
|
|
|
* @param mixed $data |
|
51
|
|
|
* @param array $options |
|
52
|
|
|
* |
|
53
|
|
|
* @return FormInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
public function createForm($type, $data = null, array $options = []) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->application['form.factory']->create($type, $data, $options); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Gets the event dispatcher. |
|
62
|
|
|
* |
|
63
|
|
|
* @return EventDispatcherInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getDispatcher() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->application['dispatcher']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Gets the session. |
|
72
|
|
|
* |
|
73
|
|
|
* @return SessionInterface |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getSession() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->application['session']; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Gets the user manager. |
|
82
|
|
|
* |
|
83
|
|
|
* @return UserManagerInterface |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getUserManager() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->application['silex_user.user_manager']; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Gets a user from the Security Token Storage. |
|
92
|
|
|
* |
|
93
|
|
|
* @return UserInterface|null |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getUser() |
|
96
|
|
|
{ |
|
97
|
|
|
/** @var TokenInterface $token */ |
|
98
|
|
|
$token = $this->application['security.token_storage']->getToken(); |
|
99
|
|
|
if (null === $token) { |
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$user = $token->getUser(); |
|
104
|
|
|
if (!is_object($user)) { |
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $user; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Generates a path from the given parameters. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $route The name of the route |
|
115
|
|
|
* @param mixed $parameters An array of parameters |
|
116
|
|
|
* |
|
117
|
|
|
* @return string The generated path |
|
118
|
|
|
*/ |
|
119
|
|
|
public function path($route, $parameters = []) |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->application['url_generator']->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Redirects the user to another route. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $route The route to redirect to |
|
128
|
|
|
* @param array $parameters An array of parameters |
|
129
|
|
|
* @param int $status The status code (302 by default) |
|
130
|
|
|
* |
|
131
|
|
|
* @return RedirectResponse |
|
132
|
|
|
*/ |
|
133
|
|
|
public function redirect($route, $parameters = [], $status = 302) |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->application->redirect($this->path($route, $parameters), $status); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Renders a template. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $name The template name |
|
142
|
|
|
* @param array $context An array of parameters to pass to the template |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function render($name, array $context = []) |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->application['twig']->render($name, $context); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|