1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.writesdown.com/ |
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
5
|
|
|
* @license http://www.writesdown.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace common\components; |
9
|
|
|
|
10
|
|
|
use common\models\Module; |
11
|
|
|
use common\models\Option; |
12
|
|
|
use Yii; |
13
|
|
|
use yii\base\Application; |
14
|
|
|
use yii\base\BootstrapInterface; |
15
|
|
|
use yii\helpers\ArrayHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class BackendBootstrap |
19
|
|
|
* |
20
|
|
|
* @author Agiel K. Saputra <[email protected]> |
21
|
|
|
* @since 0.1.0 |
22
|
|
|
*/ |
23
|
|
|
class BackendBootstrap implements BootstrapInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Bootstrap method to be called during application bootstrap stage. |
27
|
|
|
* |
28
|
|
|
* @param Application $app the application currently running |
29
|
|
|
*/ |
30
|
|
|
public function bootstrap($app) |
31
|
|
|
{ |
32
|
|
|
$this->setTime($app); |
33
|
|
|
$this->setTheme($app); |
34
|
|
|
$this->setModule($app); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set time base on Option. |
39
|
|
|
* |
40
|
|
|
* @param Application $app the application currently running |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
protected function setTime($app) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
/* TIME ZONE */ |
45
|
|
|
$app->timeZone = Option::get('time_zone'); |
46
|
|
|
/* DATE TIME */ |
47
|
|
|
$app->formatter->dateFormat = 'php:' . Option::get('date_format'); |
48
|
|
|
$app->formatter->timeFormat = 'php:' . Option::get('time_format'); |
49
|
|
|
$app->formatter->datetimeFormat = 'php:' . Option::get('date_format') . ' ' . Option::get('time_format'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set theme params |
54
|
|
|
* |
55
|
|
|
* @param Application $app the application currently running |
56
|
|
|
*/ |
57
|
|
|
protected function setTheme($app) |
58
|
|
|
{ |
59
|
|
|
/* THEME CONFIG */ |
60
|
|
|
$paramsPath = Yii::getAlias('@themes/') . Option::get('theme') . '/config/params.php'; |
61
|
|
|
|
62
|
|
|
if (is_file($paramsPath)) { |
63
|
|
|
$params = require($paramsPath); |
64
|
|
|
if ($backendParams = ArrayHelper::getValue($params, 'backend')) { |
65
|
|
|
$app->params = ArrayHelper::merge($app->params, $backendParams); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set modules. |
72
|
|
|
* |
73
|
|
|
* @param Application $app the application currently running |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
protected function setModule($app) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
foreach (Module::getActiveModules() as $module) { |
78
|
|
|
// Get module backend config. |
79
|
|
|
if ($config = $module->getBackendConfig()) { |
80
|
|
|
// Set module. |
81
|
|
|
$app->setModules([$module->name => $config]); |
82
|
|
|
// Merge application params with exist module params. |
83
|
|
|
if (is_file($module->getParamPath())) { |
84
|
|
|
$params = require($module->getParamPath()); |
85
|
|
|
if ($backendParams = ArrayHelper::getValue($params, 'backend')) { |
86
|
|
|
$app->params = ArrayHelper::merge($app->params, $backendParams); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
// Bootstrap injection. |
90
|
|
|
if ($module->backend_bootstrap) { |
91
|
|
|
$component = $app->getModule($module->name); |
92
|
|
|
if ($component instanceof BootstrapInterface) { |
93
|
|
|
$component->bootstrap($app); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.