Completed
Push — master ( 407d4b...7356b0 )
by Andrii
02:03
created

ThemeManager::widget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Theme Manager for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-thememanager
6
 * @package   yii2-thememanager
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\thememanager;
12
13
use hiqdev\thememanager\models\Settings;
14
use hiqdev\thememanager\storage\SettingsStorageInterface;
15
use Yii;
16
use yii\base\InvalidConfigException;
17
use yii\base\Widget;
18
use yii\web\AssetBundle;
19
20
/**
21
 * Theme Manager.
22
 *
23
 * Usage, in config:
24
 *
25
 * ```php
26
 * 'components' => [
27
 *     'themeManager' => [
28
 *         'class' => \hiqdev\thememanager\ThemeManager::class,
29
 *     ],
30
 * ]
31
 * ```
32
 */
33
class ThemeManager extends \hiqdev\yii2\collection\Manager implements \yii\base\BootstrapInterface
34
{
35
    /**
36
     * @var array basic pathMap for all themes, will be merged with theme own pathMap
37
     */
38
    public $pathMap = [];
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected $_itemClass = Theme::class;
44
45
    /**
46
     * @var string default theme name
47
     */
48
    protected $_defaultTheme;
49
50
    /**
51
     * Sets the default theme name.
52
     *
53
     * @param string $theme default theme name
54
     */
55
    public function setDefaultTheme($theme)
56
    {
57
        $this->_defaultTheme = $theme;
58
    }
59
60
    /**
61
     * Returns the default theme. Returns the first of available themes by default.
62
     *
63
     * @return string default theme name
64
     */
65
    public function getDefaultTheme()
66
    {
67
        if (!$this->_defaultTheme) {
68
            $keys = $this->keys(); /// shame to PHP it can't be done in single line :(
69
            $this->_defaultTheme = reset($keys);
70
        }
71
72
        return $this->_defaultTheme;
73
    }
74
75
    protected $_view;
76
77
    /**
78
     * @return \yii\web\View
79
     * @see setView
80
     */
81
    public function getView()
82
    {
83
        if ($this->_view === null) {
84
            $this->_view = Yii::$app->getView();
85
        }
86
87
        return $this->_view;
88
    }
89
90
    /**
91
     * You can change the View for theme manager.
92
     * @param \yii\web\View $view the view object that will be used to render views or view files
93
     */
94
    public function setView($view)
95
    {
96
        $this->_view = $view;
97
    }
98
99
    /**
100
     * @var Theme current theme object
101
     */
102
    protected $_theme;
103
104
    /**
105
     * Changes theme.
106
     * @param string theme name
107
     * @throws InvalidConfigException
108
     */
109
    public function setTheme($name)
110
    {
111
        if (!$name) {
112
            throw new InvalidConfigException('no theme to set');
113
        }
114
        $this->_theme = $name;
115
    }
116
117
    public function getTheme()
118
    {
119
        if (is_string($this->_theme)) {
120
            if (!$this->has($this->_theme)) {
121
                throw new InvalidConfigException('unknown theme: ' . $this->_theme);
122
            }
123
            $this->_theme = $this->getItem($this->_theme);
124
            $this->getView()->theme = $this->_theme;
125
        }
126
127
        return $this->_theme;
128
    }
129
130
    public function getSettings()
131
    {
132
        return $this->getTheme()->getSettings();
133
    }
134
135
    /**
136
     * @return bool
137
     */
138
    public static function isHomePage()
139
    {
140
        $controller = Yii::$app->controller;
141
        $default_controller = Yii::$app->defaultRoute;
142
        return ($controller->id === $default_controller) && ($controller->action->id === $controller->defaultAction);
143
    }
144
145
    /**
146
     * @var AssetBundle[] assets to be registered at bootstrap
147
     */
148
    public $assets = [];
149
150
    /**
151
     * Register all the assets.
152
     */
153
    public function registerAssets()
154
    {
155
        foreach (array_merge($this->assets, $this->getTheme()->assets) as $asset) {
156
            /** @var AssetBundle $asset */
157
            $asset::register($this->getView());
158
        }
159
    }
160
161
    /**
162
     * @var bool is already bootstrapped
163
     */
164
    protected $_isBootstrapped = false;
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function bootstrap($app)
170
    {
171
        if ($this->_isBootstrapped) {
172
            return;
173
        }
174
        $this->_isBootstrapped = true;
175
176
        Yii::trace('Bootstrap themes', get_called_class() . '::bootstrap');
177
178
        $data = $this->getSettingsStorage()->get();
179
        $model = new Settings();
180
        $model->load($data);
181
        $theme = $this->hasItem($model->theme) ? $model->theme : null;
182
        $theme = $theme ?: $this->getDefaultTheme();
183
        $this->setTheme($theme);
184
        $this->getTheme();
185
    }
186
187
    /**
188
     * @return SettingsStorageInterface
189
     */
190
    public function getSettingsStorage()
191
    {
192
        return Yii::$app->get('themeSettingsStorage');
193
    }
194
195
    /**
196
     * @return array
197
     */
198
    public function getThemeSettings()
199
    {
200
        return $this->getSettingsStorage()->get();
201
    }
202
}
203