-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBootstrap.php
385 lines (310 loc) · 16.7 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
namespace wdmg\admin;
/**
* @author Alexsander Vyshnyvetskyy <alex.vyshnyvetskyy@gmail.com>
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*/
use wdmg\users\models\Users;
use Yii;
use wdmg\base\BaseModule;
use yii\base\BootstrapInterface;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\BaseFileHelper;
use wdmg\validators\SerialValidator;
class Bootstrap extends BaseModule implements BootstrapInterface
{
private $_module;
public function bootstrap($app)
{
// Get the module instance
$this->_module = $app->getModule('admin', true);
// Get translations module
$translations = $app->getModule('admin/translations', false);
if (YII_ENV_DEV)
$app->getUrlManager()->cache = null;
if ($this->isBackend()) {
$app->getUrlManager()->addRules(
[
'/admin' => 'admin/admin/index',
'/admin/<action:(index|modules|login|logout|restore|search|favourites|terminal|checkpoint|bugreport|info|error)>' => 'admin/admin/<action>',
'<module:\w+>/<submodule:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<submodule>/<controller>/<action>',
'<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',
'<module:\w+>/<submodule:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<submodule>/<controller>/<action>',
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
'<module:\w+>/<submodule:\w+>/<controller:\w+>' => '<module>/<submodule>/<controller>/index',
//'<module:\w+>/<controller:\w+>' => '<module>/<controller>/index',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<controller:\w+>' => '<controller>/index',
],
true
);
} else {
$app->getUrlManager()->addRules(
[
'/admin' => 'admin/admin/index',
'/admin/<action:(index|modules|login|logout|restore|search|favourites|terminal|checkpoint|bugreport|info|error)>' => 'admin/admin/<action>',
],
true
);
}
// Register language of user interface
if (!($app instanceof \yii\console\Application)) {
if (isset(Yii::$app->params['admin.useSphinxSearch']))
$this->_module->useSphinxSearch = intval(Yii::$app->params['admin.useSphinxSearch']);
if (isset(Yii::$app->params['admin.sphinxSearchConf']))
$this->_module->sphinxSearchConf = intval(Yii::$app->params['admin.sphinxSearchConf']);
// Configure Sphinx search component
if ($this->_module->useSphinxSearch && !empty($this->_module->sphinxSearchConf)) {
if (class_exists('yii\sphinx\Connection') && !isset($app->sphinx)) {
$dsn = $this->_module->sphinxSearchConf['dsn'];
$host = $this->_module->sphinxSearchConf['host'];
$port = $this->_module->sphinxSearchConf['port'];
$username = $this->_module->sphinxSearchConf['username'];
$password = $this->_module->sphinxSearchConf['password'];
$app->setComponents([
'sphinx' => [
'class' => 'yii\sphinx\Connection',
'dsn' => "$dsn:host=$host;port=$port;",
'username' => $username,
'password' => $password,
]
]);
}
}
// Set the error handler page
if (!YII_ENV_TEST) {
$errorHandler = Yii::$app->getErrorHandler();
$errorHandler->errorAction = 'admin/admin/error';
}
//var_export($app->session->get('lang', false)) && die();
//var_export($_COOKIE['lang']) && die();
//var_export(Yii::$app->request->cookies->get('lang')) && die();
$supportLocales = (isset($this->_module->supportLocales)) ? $this->_module->supportLocales : [];
$lang = Yii::$app->getRequest()->getCookies()->get('lang', false);
if (is_null($lang))
$lang = $_COOKIE['lang'];
if ($lang && in_array($lang, $supportLocales))
$app->language = $lang;
if ($lang = $app->request->get('lang', false)) {
if ($lang && in_array($lang, $supportLocales)) {
$app->language = $lang;
if (!$this->isRestAPI()) {
$app->session->set('lang', $lang);
$app->response->cookies->add(new \yii\web\Cookie([
'name' => 'lang',
'value' => $lang,
'expire' => time() + 604800
]));
}
}
}
}
// Configure languages menu for UI
if (!($app instanceof \yii\console\Application) && $this->_module) {
\yii\base\Event::on(\yii\base\Controller::class, \yii\base\Controller::EVENT_BEFORE_ACTION, function ($event) use ($translations) {
$langs = [];
// Get custom locales from params
if (isset(Yii::$app->params['admin.customLocales']))
$this->_module->customLocales = Yii::$app->params['admin.customLocales'];
// Get custom list of support modules from params
if (isset(Yii::$app->params['admin.customSupportModules']))
$this->_module->customSupportModules = Yii::$app->params['admin.customSupportModules'];
// Get custom list of sidebar menu from params
if (isset(Yii::$app->params['admin.customSidebarMenu']))
$this->_module->customSidebarMenu = Yii::$app->params['admin.customSidebarMenu'];
// Get custom list of create menu from params
if (isset(Yii::$app->params['admin.customCreateMenu']))
$this->_module->customCreateMenu = Yii::$app->params['admin.customCreateMenu'];
$locales = $this->_module->getSupportLanguages();
if ($translations) {
// Register translations for admin module
$translations->registerTranslations($this->_module, true);
$bundle = \wdmg\translations\FlagsAsset::register(Yii::$app->view);
foreach ($locales as $locale => $name) {
$locale = $translations->parseLocale($locale, Yii::$app->language);
if (!($country = $locale['domain']))
$country = '_unknown';
$flag = \yii\helpers\Html::img($bundle->baseUrl . '/flags-iso/flat/24/'.$country.'.png');
$langs[] = [
'label' => $flag . ' ' . $locale['name'],
'url' => \yii\helpers\Url::current(['lang' => $locale['locale']]),
'active'=> (Yii::$app->language == $locale['locale']) ? true : false,
'options' => [
'class' => (Yii::$app->language == $locale['locale']) ? 'active' : false
],
'linkOptions' => [
'data' => [
'label' => $name,
'pjax' => 1
]
]
];
}
} else {
// Register translations for admin module
$this->_module->registerTranslations($this->_module);
foreach ($locales as $locale => $name) {
$langs[] = [
'label' => $name,
'url' => '?lang='.$locale,
'active'=> (Yii::$app->language == $locale) ? true : false,
'options' => [
'class' => (Yii::$app->language == $locale) ? 'active' : false
],
'linkOptions' => [
'data' => [
'label' => $name,
'pjax' => 1
]
]
];
}
}
Yii::$app->view->params['langs'] = $langs;
// Dashboard favourites list
if (!Yii::$app->user->isGuest) {
$favourites = Users::getOptions('favourites', []);
if (!empty($favourites))
Yii::$app->view->params['favourites'] = array_values($favourites);
}
});
}
// Configure administrative panel
$app->setComponents([
'dashboard' => [
'class' => 'wdmg\admin\components\Dashboard'
]
]);
// Loading all child modules
if ($app->db->schema->getTableSchema(\wdmg\admin\models\Modules::tableName())) {
$migrationLookup = [];
$model = new \wdmg\admin\models\Modules();
if ($modules = $model::getModules(true)) {
if (is_array($modules)) {
foreach ($modules as $module) {
// Setup module ID
$moduleId = 'admin/' . $module['module'];
if (!class_exists($module['class'])) {
throw new InvalidConfigException('Can\'t load module `' . $module['class'] . '`');
} else {
// Check if this module has been loaded before
if (!$app->hasModule($moduleId)) {
// Get default module options
$options = [];
if (is_array($module['options'])) {
$options = $module['options'];
} elseif (is_string($module['options']) && SerialValidator::isValid($module['options'])) {
$options = unserialize($module['options']);
}
// Get default module options
$options = (is_array($module['options'])) ? $module['options'] : unserialize($module['options']);
// Prepare advanced module options from DB and rewrite defaults
if (Yii::$app->getModule('admin/options') && isset(Yii::$app->options)) {
foreach ($options as $option => $value) {
if (Yii::$app->options->get($module['module'] . '.' . $option))
$options[$option] = Yii::$app->options->get($module['module'] . '.' . $option);
}
}
// Setup the module as a child module of `admin'
if ($admin = $app->getModule('admin')) {
$admin->setModule($module['module'], ArrayHelper::merge([
'class' => $module['class']
], $options));
}
// Register and load child module
if ($installed = $app->getModule('admin/' . $module['module'], true)) {
// Register the translation for the loadable module,
// if it has not been registered before (possibly not inherited from BaseModule)
if (!($app instanceof \yii\console\Application) && $this->_module) {
if (!isset(Yii::$app->getI18n()->translations['app/modules/' . $installed->id])) {
if ($translations = Yii::$app->getModule('admin/translations')) {
$translations->registerTranslations($installed, true);
} else {
$this->_module->registerTranslations($installed);
}
}
}
// Configure dashboard layout
$installed->layout = 'dashboard';
$installed->layoutPath = '@wdmg/admin/views/layouts';
// Configure migrations lookup
$migrationLookup[] = $installed->getBaseAlias() . '/migrations';
// Configure base route prefix
if (isset($module->routePrefix))
$installed->routePrefix = $module->routePrefix;
// Check instance of BootstrapInterface
if ($installed instanceof yii\base\BootstrapInterface) {
$installed->bootstrap(Yii::$app);
} else if ($module['bootstrap']) {
Yii::$app->bootstrap[] = $module['bootstrap'];
} else {
throw new InvalidConfigException('Module `' . $module['class'] . '` must implement BootstrapInterface interface');
}
}
}
}
}
}
} else {
Yii::debug('No child modules available for loading', __METHOD__);
}
}
// Set error handler
if (!($app instanceof \yii\console\Application) && $this->isBackend()) {
if ($errorHandler = $app->getErrorHandler()) {
if (Yii::$app->getModule('admin/rbac'))
$errorHandler->errorAction = 'admin/rbac/rbac/error';
else
$errorHandler->errorAction = 'admin/error';
} else {
$app->setComponents([
'errorHandler' => [
'errorAction' => (Yii::$app->getModule('admin/rbac')) ?
'admin/rbac/rbac/error' :
'admin/error'
]
]);
}
}
// For console only
if ($app instanceof \yii\console\Application) {
$migrationLookup = [];
$support = $this->_module->getSupportModules();
// Polyfill for array_key_first() for PHP <= 7.3.0
if (!function_exists('array_key_first')) {
function array_key_first(array $arr) {
foreach($arr as $key => $unused) {
return $key;
}
return NULL;
}
}
// Configure migrations for all modules
foreach ($app->extensions as $key => $extension) {
// Limit the output of only those modules that are supported by the system.
if (in_array($extension['name'], $support)) {
$alias = array_key_first($extension['alias']);
//$migrationLookup[] = BaseFileHelper::normalizePath(Yii::getAlias($alias) . '/migrations');
$migrationLookup[] = BaseFileHelper::normalizePath($extension['alias'][$alias] . '/migrations');
}
}
$app->controllerMap['migrate'] = [
'class' => 'wdmg\admin\commands\MigrateController',
'migrationLookup' => $migrationLookup
];
// Configure urlManager
/*if ($app->getModule('admin/options') && isset(Yii::$app->options)) {
if ($hostInfo = Yii::$app->options->get('urlManager.hostInfo')) {
$app->getUrlManager()->setHostInfo($hostInfo);
$_SERVER['SERVER_NAME'] = $hostInfo;
}
if ($baseUrl = Yii::$app->options->get('urlManager.baseUrl')) {
$app->getUrlManager()->setBaseUrl($baseUrl);
$_SERVER['HTTP_HOST'] = $baseUrl;
}
}*/ // @TODO: Need review, see /yiisoft/yii2/web/UrlManager.php:640
}
}
}