forked from dektrium/yii2-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bootstrap.php
130 lines (113 loc) · 4.71 KB
/
Bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace dektrium\user;
use Yii;
use yii\authclient\Collection;
use yii\base\BootstrapInterface;
use yii\console\Application as ConsoleApplication;
use yii\i18n\PhpMessageSource;
/**
* Bootstrap class registers module and user application component. It also creates some url rules which will be applied
* when UrlManager.enablePrettyUrl is enabled.
*
* @author Dmitry Erofeev <[email protected]>
*/
class Bootstrap implements BootstrapInterface
{
/** @var array Model's map */
private $_modelMap = [
'User' => 'dektrium\user\models\User',
'Account' => 'dektrium\user\models\Account',
'Profile' => 'dektrium\user\models\Profile',
'Token' => 'dektrium\user\models\Token',
'RegistrationForm' => 'dektrium\user\models\RegistrationForm',
'ResendForm' => 'dektrium\user\models\ResendForm',
'LoginForm' => 'dektrium\user\models\LoginForm',
'SettingsForm' => 'dektrium\user\models\SettingsForm',
'RecoveryForm' => 'dektrium\user\models\RecoveryForm',
'UserSearch' => 'dektrium\user\models\UserSearch',
];
/** @inheritdoc */
public function bootstrap($app)
{
/** @var Module $module */
/** @var \yii\db\ActiveRecord $modelName */
if ($app->hasModule('user') && ($module = $app->getModule('user')) instanceof Module) {
$this->_modelMap = array_merge($this->_modelMap, $module->modelMap);
foreach ($this->_modelMap as $name => $definition) {
$class = "dektrium\\user\\models\\" . $name;
Yii::$container->set($class, $definition);
$modelName = is_array($definition) ? $definition['class'] : $definition;
$module->modelMap[$name] = $modelName;
if (in_array($name, ['User', 'Profile', 'Token', 'Account'])) {
Yii::$container->set($name . 'Query', function () use ($modelName) {
return $modelName::find();
});
}
}
Yii::$container->setSingleton(Finder::className(), [
'userQuery' => Yii::$container->get('UserQuery'),
'profileQuery' => Yii::$container->get('ProfileQuery'),
'tokenQuery' => Yii::$container->get('TokenQuery'),
'accountQuery' => Yii::$container->get('AccountQuery'),
]);
if ($app instanceof ConsoleApplication) {
$module->controllerNamespace = 'dektrium\user\commands';
} else {
Yii::$container->set('yii\web\User', [
'enableAutoLogin' => true,
'loginUrl' => ['/user/security/login'],
'identityClass' => $module->modelMap['User'],
]);
$configUrlRule = [
'prefix' => $module->urlPrefix,
'rules' => $module->urlRules,
];
if ($module->urlPrefix != 'user') {
$configUrlRule['routePrefix'] = 'user';
}
$configUrlRule['class'] = 'yii\web\GroupUrlRule';
$rule = Yii::createObject($configUrlRule);
$app->urlManager->addRules([$rule], false);
if (!$app->has('authClientCollection')) {
$app->set('authClientCollection', [
'class' => Collection::className(),
]);
}
}
if (!isset($app->get('i18n')->translations['user*'])) {
$app->get('i18n')->translations['user*'] = [
'class' => PhpMessageSource::className(),
'basePath' => __DIR__ . '/messages',
'sourceLanguage' => 'en-US'
];
}
Yii::$container->set('dektrium\user\Mailer', $module->mailer);
$module->debug = $this->ensureCorrectDebugSetting();
}
}
/** Ensure the module is not in DEBUG mode on production environments */
public function ensureCorrectDebugSetting()
{
if (!defined('YII_DEBUG')) {
return false;
}
if (!defined('YII_ENV')) {
return false;
}
if (defined('YII_ENV') && YII_ENV !== 'dev') {
return false;
}
if (defined('YII_DEBUG') && YII_DEBUG !== true) {
return false;
}
return Yii::$app->getModule('user')->debug;
}
}