Completed
Push — master ( bed82d...5b1b17 )
by Dmitry
02:54
created

ThemeManager   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 13
Bugs 2 Features 1
Metric Value
wmc 30
lcom 2
cbo 7
dl 0
loc 209
rs 10
c 13
b 2
f 1
ccs 0
cts 108
cp 0

14 Methods

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