-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bootstrap.php
111 lines (88 loc) · 3.75 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
<?php
/*
* This file is part of the 2amigos/yii2-usuario project.
*
* (c) 2amigOS! <http://2amigos.us/>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace daxslab\website;
use daxslab\thumbnailer\Thumbnailer;
use daxslab\website\models\Website;
use Yii;
use yii\base\BootstrapInterface;
use yii\base\InvalidArgumentException;
use yii\i18n\PhpMessageSource;
/**
* Bootstrap class of the yii2-usuario extension. Configures container services, initializes translations,
* builds class map, and does the other setup actions participating in the application bootstrap process.
*/
class Bootstrap implements BootstrapInterface
{
/**
* {@inheritdoc}
*
* @throws InvalidConfigException
*/
public function bootstrap($app)
{
if ($app->hasModule('website') && $app->getModule('website') instanceof Module) {
$module = $app->getModule('website');
if (!isset($app->get('i18n')->translations['website*'])) {
$app->get('i18n')->translations['website*'] = [
'class' => PhpMessageSource::className(),
'basePath' => __DIR__ . '/messages',
'sourceLanguage' => 'en-US'
];
}
if (empty($module->languages)) {
$module->languages = [explode('-', $app->language)[0]];
}
if ($app instanceof \yii\web\Application) {
if (!$app->has('thumbnailer')) {
$app->set('thumbnailer', [
'class' => Thumbnailer::class,
'thumbnailsPath' => '@webroot/assets/thumbnails',
'thumbnailsBaseUrl' => '@web/assets/thumbnails',
'enableCaching' => $module->cacheThumbnails,
]);
}
if (!isset($module->token)) {
throw new InvalidArgumentException(Yii::t('website', "The token must be set"));
}
$website = Website::find()->byToken($module->token);
if ($website == null) {
throw new InvalidArgumentException(Yii::t('website', "The token is invalid"));
}
$app->set('website', $website);
if (strstr($module->controllerNamespace, 'backend')) {
$module->viewPath = '@daxslab/website/views/backend';
WebsiteAsset::register($app->view);
} else {
WebsiteAsset::register($app->view);
$module->viewPath = $module->viewPath == Yii::getAlias('@daxslab/website/views')
? '@daxslab/website/views/frontend'
: $module->viewPath;
if (!empty($module->languages)) {
$langs = join('|', $module->languages);
$module->urlRules = [
'<_lang:(' . $langs . ')>' => 'page/home',
'<_lang:(' . $langs . ')>/<slug:[\w/-]+>.<_format>' => 'page/view',
'<_lang:(' . $langs . ')>/<slug:[\w/-]+>' => 'page/view',
];
}
$configUrlRule = [
'routePrefix' => 'website',
'rules' => $module->urlRules,
];
$configUrlRule['class'] = 'yii\web\GroupUrlRule';
$rule = Yii::createObject($configUrlRule);
$app->urlManager->addRules([$rule], false);
}
} else {
$module->controllerNamespace = 'daxslab\website\commands';
}
}
}
}