-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
146 lines (133 loc) · 4.15 KB
/
Module.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
<?php
/**
* _ __ __ _____ _____ ___ ____ _____
* | | / // // ___//_ _// || __||_ _|
* | |/ // /(__ ) / / / /| || | | |
* |___//_//____/ /_/ /_/ |_||_| |_|
* @link https://vistart.me/
* @copyright Copyright (c) 2016 - 2017 vistart
* @license https://vistart.me/license/
*/
namespace rhoone;
use Yii;
use yii\base\Application;
use yii\base\BootstrapInterface;
/**
* Description of Module
*
* @author vistart <[email protected]>
*/
class Module extends \yii\base\Module implements BootstrapInterface
{
public $controllerNamespace = 'rhoone\controllers';
public function init()
{
parent::init();
$this->registerTranslations();
}
public function registerTranslations()
{
Yii::$app->i18n->translations['rhoone*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '@rhoone/messages',
];
}
/**
* @inheritdoc
*/
public function bootstrap($app)
{
if ($app instanceof \yii\web\Application) {
$this->addRules($app);
}
$count = $this->registerComponents($app);
Yii::info("$count component(s) registered.", __METHOD__);
$count = $this->bootstrapExtensions($app);
Yii::info("$count extension(s) bootstrapped.", __METHOD__);
}
/**
*
* @param Application $app
*/
public function registerComponents($app)
{
$count = 0;
foreach ($this->coreComponents() as $id => $component) {
try {
$app->set($id, $component);
} catch (\Exception $ex) {
Yii::error("`$id` failed to register: " . $ex->getMessage(), __METHOD__);
continue;
}
Yii::info("`$id` registered.", __METHOD__);
$count++;
}
return $count;
}
public function coreComponents()
{
return [
'rhoone' => ['class' => 'rhoone\base\Rhoone'], // Required.
];
}
public function addRules($app)
{
Yii::trace('Adding URL Rules.', __METHOD__);
$rules = [
's' => $this->id . '/search/index',
'search' => $this->id . '/search/index',
'subordinate/register' => $this->id . '/subordinate/register',
'register-subordinate' => $this->id . '/subordinate/register-subordinate',
'subordinate/deregister' => $this->id . '/subordinate/deregister',
'deregister-subordinate' => $this->id . '/subordinate/deregister-subordinate',
];
$app->getUrlManager()->addRules($rules);
}
/**
* Bootstrap with Extensions.
* @param \yii\base\Application $app
* @return int
*/
protected function bootstrapExtensions($app)
{
$rhoone = Yii::$app->rhoone;
/* @var $rhoone \rhoone\base\Rhoone */
$extensions = $rhoone->extensions;
if (empty($extensions)) {
return 0;
}
foreach ($extensions as $id => $extension) {
if ($extension instanceof BootstrapInterface) {
try {
$extension->bootstrap($app);
} catch (\Exception $ex) {
Yii::error($ex->getMessage(), __METHOD__);
continue;
}
}
}
$count = 0;
foreach ($extensions as $ext) {
$moduleConfig = [];
try {
$moduleConfig = $ext->getModule();
} catch (\Exception $ex) {
continue;
}
if (empty($moduleConfig)) {
continue;
}
Yii::info("`" . $moduleConfig['id'] . '` enabled.', __METHOD__);
$app->setModule($moduleConfig['id'], $moduleConfig);
$module = $app->getModule($moduleConfig['id']);
if ($module instanceof BootstrapInterface) {
$app->bootstrap[] = $module->id;
Yii::trace('Bootstrap with ' . $module->className() . '::' . 'bootstrap()', __METHOD__);
$module->bootstrap($app);
$count++;
}
}
return $count;
}
}