1 | <?php |
||
32 | class Bootstrap implements BootstrapInterface |
||
33 | { |
||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public function bootstrap($app) |
||
38 | { |
||
39 | if ($app->hasModule('user') && $app->getModule('user') instanceof Module) { |
||
40 | $map = $this->buildClassMap($app->getModule('user')->classMap); |
||
41 | $this->initContainer($app, $map); |
||
42 | $this->initTranslations($app); |
||
43 | $this->initMailServiceConfiguration($app, $app->getModule('user')); |
||
44 | |||
45 | if ($app instanceof WebApplication) { |
||
46 | $this->initControllerNamespace($app); |
||
47 | $this->initUrlRoutes($app); |
||
48 | $this->initAuthCollection($app); |
||
49 | $this->initAuthManager($app); |
||
50 | } else { |
||
51 | /* @var $app ConsoleApplication */ |
||
52 | $this->initConsoleCommands($app); |
||
53 | $this->initAuthManager($app); |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Initialize container with module classes. |
||
60 | * |
||
61 | * @param \yii\base\Application $app |
||
62 | * @param array $map the previously built class map list |
||
63 | */ |
||
64 | 10 | protected function initContainer($app, $map) |
|
65 | { |
||
66 | $di = Yii::$container; |
||
67 | try { |
||
68 | // events |
||
69 | $di->set(Event\FormEvent::class); |
||
70 | $di->set(Event\ProfileEvent::class); |
||
71 | $di->set(Event\ResetPasswordEvent::class); |
||
72 | $di->set(Event\SocialNetworkAuthEvent::class); |
||
73 | $di->set(Event\SocialNetworkConnectEvent::class); |
||
74 | $di->set(Event\UserEvent::class); |
||
75 | |||
76 | // forms |
||
77 | $di->set(Form\LoginForm::class); |
||
78 | $di->set(Form\RecoveryForm::class); |
||
79 | $di->set(Form\RegistrationForm::class); |
||
80 | $di->set(Form\ResendForm::class); |
||
81 | $di->set(Form\SettingsForm::class); |
||
82 | |||
83 | // helpers |
||
84 | $di->set(Helper\AuthHelper::class); |
||
85 | $di->set(Helper\GravatarHelper::class); |
||
86 | $di->set(Helper\SecurityHelper::class); |
||
87 | $di->set(Helper\TimezoneHelper::class); |
||
88 | |||
89 | // services |
||
90 | $di->set(Service\AccountConfirmationService::class); |
||
91 | $di->set(Service\EmailChangeService::class); |
||
92 | $di->set(Service\PasswordRecoveryService::class); |
||
93 | $di->set(Service\ResendConfirmationService::class); |
||
94 | $di->set(Service\ResetPasswordService::class); |
||
95 | $di->set(Service\SocialNetworkAccountConnectService::class); |
||
96 | $di->set(Service\SocialNetworkAuthenticateService::class); |
||
97 | $di->set(Service\UserBlockService::class); |
||
98 | $di->set(Service\UserCreateService::class); |
||
99 | $di->set(Service\UserRegisterService::class); |
||
100 | $di->set(Service\UserConfirmationService::class); |
||
101 | $di->set(Service\AuthItemEditionService::class); |
||
102 | $di->set(Service\UpdateAuthAssignmentsService::class); |
||
103 | $di->set(Service\SwitchIdentityService::class); |
||
104 | |||
105 | // email change strategy |
||
106 | $di->set(Strategy\DefaultEmailChangeStrategy::class); |
||
107 | $di->set(Strategy\InsecureEmailChangeStrategy::class); |
||
108 | $di->set(Strategy\SecureEmailChangeStrategy::class); |
||
109 | |||
110 | // validators |
||
111 | $di->set(Validator\AjaxRequestModelValidator::class); |
||
112 | $di->set(TimeZoneValidator::class); |
||
113 | |||
114 | // class map models + query classes |
||
115 | $modelClassMap = []; |
||
116 | foreach ($map as $class => $definition) { |
||
117 | $di->set($class, $definition); |
||
118 | $model = is_array($definition) ? $definition['class'] : $definition; |
||
119 | $name = (substr($class, strrpos($class, '\\') + 1)); |
||
120 | $modelClassMap[$class] = $model; |
||
121 | if (in_array($name, ['User', 'Profile', 'Token', 'SocialNetworkAccount'])) { |
||
122 | $di->set( |
||
123 | "Da\\User\\Query\\{$name}Query", |
||
124 | 10 | function () use ($model) { |
|
125 | 10 | return $model::find(); |
|
126 | } |
||
127 | ); |
||
128 | } |
||
129 | } |
||
130 | $di->setSingleton(ClassMapHelper::class, ClassMapHelper::class, [$modelClassMap]); |
||
131 | |||
132 | // search classes |
||
133 | if (!$di->has(Search\UserSearch::class)) { |
||
134 | $di->set(Search\UserSearch::class, [$di->get(Query\UserQuery::class)]); |
||
135 | } |
||
136 | if (!$di->has(Search\PermissionSearch::class)) { |
||
137 | $di->set(Search\PermissionSearch::class); |
||
138 | } |
||
139 | if (!$di->has(Search\RoleSearch::class)) { |
||
140 | $di->set(Search\RoleSearch::class); |
||
141 | } |
||
142 | |||
143 | if ($app instanceof WebApplication) { |
||
144 | // override Yii |
||
145 | $di->set( |
||
146 | 'yii\web\User', |
||
147 | [ |
||
148 | 'enableAutoLogin' => true, |
||
149 | 'loginUrl' => ['/user/security/login'], |
||
150 | 'identityClass' => $di->get(ClassMapHelper::class)->get(User::class), |
||
151 | ] |
||
152 | ); |
||
153 | } |
||
154 | } catch (Exception $e) { |
||
155 | die($e); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Registers module translation messages. |
||
161 | * |
||
162 | * @param Application $app |
||
163 | */ |
||
164 | protected function initTranslations(Application $app) |
||
165 | { |
||
166 | if (!isset($app->get('i18n')->translations['usuario*'])) { |
||
167 | $app->get('i18n')->translations['usuario*'] = [ |
||
168 | 'class' => PhpMessageSource::class, |
||
169 | 'basePath' => __DIR__ . '/resources/i18n', |
||
170 | 'sourceLanguage' => 'en-US', |
||
171 | ]; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Ensures the auth manager is the one provided by the library. |
||
177 | * |
||
178 | * @param Application $app |
||
179 | */ |
||
180 | protected function initAuthManager(Application $app) |
||
181 | { |
||
182 | if (!($app->getAuthManager() instanceof AuthManagerInterface)) { |
||
183 | $app->set( |
||
184 | 'authManager', |
||
185 | [ |
||
186 | 'class' => AuthDbManagerComponent::class, |
||
187 | ] |
||
188 | ); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Initializes web url routes (rules in Yii2). |
||
194 | * |
||
195 | * @param WebApplication $app |
||
196 | */ |
||
197 | protected function initUrlRoutes(WebApplication $app) |
||
198 | { |
||
199 | /** @var $module Module */ |
||
200 | $module = $app->getModule('user'); |
||
201 | $config = [ |
||
202 | 'class' => 'yii\web\GroupUrlRule', |
||
203 | 'prefix' => $module->prefix, |
||
204 | 'rules' => $module->routes, |
||
205 | ]; |
||
206 | |||
207 | if ($module->prefix !== 'user') { |
||
208 | $config['routePrefix'] = 'user'; |
||
209 | } |
||
210 | |||
211 | $rule = Yii::createObject($config); |
||
212 | $app->getUrlManager()->addRules([$rule], false); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Ensures required mail parameters needed for the mail service. |
||
217 | * |
||
218 | * @param Application $app |
||
219 | * @param Module|\yii\base\Module $module |
||
220 | */ |
||
221 | protected function initMailServiceConfiguration(Application $app, Module $module) |
||
222 | { |
||
223 | $defaults = [ |
||
224 | 'fromEmail' => '[email protected]', |
||
225 | 'welcomeMailSubject' => Yii::t('usuario', 'Welcome to {0}', $app->name), |
||
|
|||
226 | 'confirmationMailSubject' => Yii::t('usuario', 'Confirm account on {0}', $app->name), |
||
227 | 'reconfirmationMailSubject' => Yii::t('usuario', 'Confirm email change on {0}', $app->name), |
||
228 | 'recoveryMailSubject' => Yii::t('usuario', 'Complete password reset on {0}', $app->name), |
||
229 | ]; |
||
230 | |||
231 | $module->mailParams = array_merge($defaults, $module->mailParams); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Ensures the authCollection component is configured. |
||
236 | * |
||
237 | * @param WebApplication $app |
||
238 | */ |
||
239 | protected function initAuthCollection(WebApplication $app) |
||
240 | { |
||
241 | if (!$app->has('authClientCollection')) { |
||
242 | $app->set('authClientCollection', Collection::class); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Registers console commands to main app. |
||
248 | * |
||
249 | * @param ConsoleApplication $app |
||
250 | */ |
||
251 | protected function initConsoleCommands(ConsoleApplication $app) |
||
255 | |||
256 | /** |
||
257 | * Registers controllers. |
||
258 | * |
||
259 | * @param WebApplication $app |
||
260 | */ |
||
261 | protected function initControllerNamespace(WebApplication $app) |
||
262 | { |
||
266 | |||
267 | /** |
||
268 | * Builds class map according to user configuration. |
||
269 | * |
||
270 | * @param array $userClassMap user configuration on the module |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | protected function buildClassMap(array $userClassMap) |
||
331 | |||
332 | /** |
||
333 | * Returns the parent class name route of a short class name. |
||
334 | * |
||
335 | * @param array $routes class name routes |
||
336 | * @param string $name |
||
337 | * |
||
338 | * @throws Exception |
||
339 | * @return int|string |
||
340 | * |
||
341 | */ |
||
342 | protected function getRoute(array $routes, $name) |
||
351 | } |
||
352 |
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: