1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AWurth\SilexUser\Provider; |
4
|
|
|
|
5
|
|
|
use AWurth\SilexUser\Controller\AuthController; |
6
|
|
|
use AWurth\SilexUser\Controller\RegistrationController; |
7
|
|
|
use AWurth\SilexUser\Entity\UserManager; |
8
|
|
|
use AWurth\SilexUser\EventListener\AuthenticationListener; |
9
|
|
|
use AWurth\SilexUser\EventListener\EmailConfirmationListener; |
10
|
|
|
use AWurth\SilexUser\EventListener\FlashListener; |
11
|
|
|
use AWurth\SilexUser\Mailer\TwigSwiftMailer; |
12
|
|
|
use AWurth\SilexUser\Security\LoginManager; |
13
|
|
|
use LogicException; |
14
|
|
|
use Pimple\Container; |
15
|
|
|
use Pimple\ServiceProviderInterface; |
16
|
|
|
use Silex\Api\BootableProviderInterface; |
17
|
|
|
use Silex\Api\ControllerProviderInterface; |
18
|
|
|
use Silex\Api\EventListenerProviderInterface; |
19
|
|
|
use Silex\Application; |
20
|
|
|
use Silex\ControllerCollection; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
use Symfony\Component\Translation\Loader\PhpFileLoader; |
23
|
|
|
use Symfony\Component\Translation\Translator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Silex User Service Provider. |
27
|
|
|
* |
28
|
|
|
* @author Alexis Wurth <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class SilexUserServiceProvider implements ServiceProviderInterface, BootableProviderInterface, ControllerProviderInterface, EventListenerProviderInterface |
31
|
|
|
{ |
32
|
|
|
protected static $defaultOptions = [ |
33
|
|
|
'use_templates' => true, |
34
|
|
|
'use_translations' => true, |
35
|
|
|
'use_flash_notifications' => true, |
36
|
|
|
'use_authentication_listener' => false, |
37
|
|
|
'registration.confirmation.enabled' => false, |
38
|
|
|
'registration.confirmation.from_email' => '' |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function register(Container $app) |
45
|
|
|
{ |
46
|
|
|
if (!isset($app['twig'])) { |
47
|
|
|
throw new LogicException('You must register the TwigServiceProvider to use the SilexUserServiceProvider'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$app['silex_user.options'] = []; |
51
|
|
|
|
52
|
|
|
// Services |
53
|
|
|
$app['silex_user.user_manager'] = function ($app) { |
54
|
|
|
return new UserManager($app); |
55
|
|
|
}; |
56
|
|
|
|
57
|
|
|
$app['silex_user.login_manager'] = function ($app) { |
58
|
|
|
return new LoginManager( |
59
|
|
|
$app['security.token_storage'], |
60
|
|
|
$app['security.user_checker'], |
61
|
|
|
$app['security.session_strategy'], |
62
|
|
|
$app['request_stack'] |
63
|
|
|
); |
64
|
|
|
}; |
65
|
|
|
|
66
|
|
|
$app['silex_user.user_provider.username'] = function ($app) { |
67
|
|
|
return new UserProvider($app['silex_user.user_manager']); |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
$app['silex_user.user_provider.username_email'] = function ($app) { |
71
|
|
|
return new EmailUserProvider($app['silex_user.user_manager']); |
72
|
|
|
}; |
73
|
|
|
|
74
|
|
|
$app['silex_user.mailer'] = function ($app) { |
75
|
|
|
if (isset($app['mailer']) && get_class($app['mailer']) === 'Swift_Mailer') { |
76
|
|
|
$parameters = [ |
77
|
|
|
'from_email' => [ |
78
|
|
|
'confirmation' => $this->getOption($app, 'registration.confirmation.from_email') |
79
|
|
|
] |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
return new TwigSwiftMailer($app['mailer'], $app['twig'], $app['url_generator'], $parameters); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return null; |
86
|
|
|
}; |
87
|
|
|
|
88
|
|
|
// Controllers |
89
|
|
|
$app['auth.controller'] = function ($app) { |
90
|
|
|
return new AuthController($app); |
91
|
|
|
}; |
92
|
|
|
|
93
|
|
|
$app['registration.controller'] = function ($app) { |
94
|
|
|
return new RegistrationController($app); |
95
|
|
|
}; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function boot(Application $app) |
102
|
|
|
{ |
103
|
|
|
$app['silex_user.options'] = array_replace(self::$defaultOptions, $app['silex_user.options']); |
104
|
|
|
|
105
|
|
|
if (empty($app['silex_user.options']['user_class'])) { |
106
|
|
|
throw new LogicException('The "user_class" option must be set'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (empty($app['silex_user.options']['firewall_name'])) { |
110
|
|
|
throw new LogicException('The "firewall_name" option must be set'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (true === $this->getOption($app, 'registration.confirmation.enabled')) { |
114
|
|
|
if (null === $app['silex_user.mailer']) { |
115
|
|
|
throw new LogicException('You must configure a mailer to enable email notifications'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (empty($this->getOption($app, 'registration.confirmation.from_email'))) { |
119
|
|
|
throw new LogicException('The "registration.confirmation.from_email" option must be set'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (true === $this->getOption($app, 'use_templates')) { |
124
|
|
|
$app['twig.loader.filesystem']->addPath(__DIR__ . '/../Resources/views/'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (true === $this->getOption($app, 'use_translations')) { |
128
|
|
|
/** @var Translator $translator */ |
129
|
|
|
$translator = $app['translator']; |
130
|
|
|
|
131
|
|
|
$translator->addLoader('php', new PhpFileLoader()); |
132
|
|
|
|
133
|
|
|
$translator->addResource('php', __DIR__ . '/../Resources/translations/en.php', 'en'); |
134
|
|
|
$translator->addResource('php', __DIR__ . '/../Resources/translations/fr.php', 'fr'); |
135
|
|
|
$translator->addResource('php', __DIR__ . '/../Resources/translations/validators.en.php', 'en', 'validators'); |
136
|
|
|
$translator->addResource('php', __DIR__ . '/../Resources/translations/validators.fr.php', 'fr', 'validators'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function connect(Application $app) |
144
|
|
|
{ |
145
|
|
|
/** @var ControllerCollection $controllers */ |
146
|
|
|
$controllers = $app['controllers_factory']; |
147
|
|
|
|
148
|
|
|
$controllers->get('/login', 'auth.controller:loginAction') |
149
|
|
|
->bind('silex_user.login'); |
150
|
|
|
|
151
|
|
|
$controllers->method('GET|POST') |
152
|
|
|
->match('/login_check') |
153
|
|
|
->bind('silex_user.login_check'); |
154
|
|
|
|
155
|
|
|
$controllers->method('GET|POST') |
156
|
|
|
->match('/logout') |
157
|
|
|
->bind('silex_user.logout'); |
158
|
|
|
|
159
|
|
|
$controllers->method('GET|POST') |
160
|
|
|
->match('/register', 'registration.controller:registerAction') |
161
|
|
|
->bind('silex_user.register'); |
162
|
|
|
|
163
|
|
|
$controllers->get('/register/confirmed', 'registration.controller:confirmedAction') |
164
|
|
|
->bind('silex_user.registration_confirmed'); |
165
|
|
|
|
166
|
|
|
if (true === $this->getOption($app, 'registration.confirmation.enabled')) { |
167
|
|
|
$controllers->get('/register/check-email', 'registration.controller:checkEmailAction') |
168
|
|
|
->bind('silex_user.registration_check_email'); |
169
|
|
|
|
170
|
|
|
$controllers->get('/register/confirm/{token}', 'registration.controller:confirmAction') |
171
|
|
|
->bind('silex_user.registration_confirm'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $controllers; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function subscribe(Container $app, EventDispatcherInterface $dispatcher) |
181
|
|
|
{ |
182
|
|
|
if (true === $this->getOption($app, 'use_authentication_listener')) { |
183
|
|
|
$dispatcher->addSubscriber(new AuthenticationListener($app['silex_user.login_manager'], $this->getOption($app, 'firewall_name'))); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (true === $this->getOption($app, 'use_flash_notifications')) { |
187
|
|
|
$dispatcher->addSubscriber(new FlashListener($app['session'], $app['translator'])); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (true === $this->getOption($app, 'registration.confirmation.enabled')) { |
191
|
|
|
$dispatcher->addSubscriber(new EmailConfirmationListener($app['silex_user.mailer'], $app['url_generator'], $app['session'])); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Gets an option or its default value if it is not set. |
197
|
|
|
* |
198
|
|
|
* @param Container $app |
199
|
|
|
* @param string $name |
200
|
|
|
* |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
|
|
protected function getOption(Container $app, $name) |
204
|
|
|
{ |
205
|
|
|
return isset($app['silex_user.options'][$name]) ? $app['silex_user.options'][$name] : self::$defaultOptions[$name]; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: