ThemeManager::setDefaultTheme()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Pluggable themes 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-2017, 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\web\AssetBundle;
18
use yii\web\Controller;
19
20
/**
21
 * Theme Manager. Provides:.
22
 *
23
 * - theme initialization
24
 * - pathMap building
25
 *
26
 * Usage, in config:
27
 *
28
 * ```php
29
 * 'components' => [
30
 *     'themeManager' => [
31
 *         'class' => \hiqdev\thememanager\ThemeManager::class,
32
 *     ],
33
 * ]
34
 * ```
35
 *
36
 * @author Andrii Vasyliev <[email protected]>
37
 */
38
class ThemeManager extends \hiqdev\yii2\collection\Manager implements \yii\base\BootstrapInterface
39
{
40
    /**
41
     * @var array basic pathMap for all themes, will be merged with theme own pathMap
42
     */
43
    public $pathMap = [];
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected $_itemClass = Theme::class;
49
50
    /**
51
     * @var string default theme name
52
     */
53
    protected $_defaultTheme;
54
55
    /**
56
     * Sets the default theme name.
57
     *
58
     * @param string $theme default theme name
59
     */
60
    public function setDefaultTheme($theme)
61
    {
62
        $this->_defaultTheme = $theme;
63
    }
64
65
    /**
66
     * Returns the default theme. Returns the first of available themes by default.
67
     *
68
     * @return string default theme name
69
     */
70
    public function getDefaultTheme()
71
    {
72
        if (!$this->_defaultTheme) {
73
            $keys = $this->keys(); /// shame to PHP it can't be done in single line :(
74
            $this->_defaultTheme = reset($keys);
75
        }
76
77
        return $this->_defaultTheme;
78
    }
79
80
    protected $_view;
81
82
    /**
83
     * @return \yii\web\View
84
     * @see setView
85
     */
86
    public function getView()
87
    {
88
        if ($this->_view === null) {
89
            $this->_view = Yii::$app->getView();
90
        }
91
92
        return $this->_view;
93
    }
94
95
    /**
96
     * You can change the View for theme manager.
97
     * @param \yii\web\View $view the view object that will be used to render views or view files
98
     */
99
    public function setView($view)
100
    {
101
        $this->_view = $view;
102
    }
103
104
    /**
105
     * @var Theme current theme object
106
     */
107
    protected $_theme;
108
109
    /**
110
     * Changes theme.
111
     * @param string theme name
112
     * @throws InvalidConfigException
113
     */
114
    public function setTheme($name)
115
    {
116
        if (!$name) {
117
            throw new InvalidConfigException('no theme to set');
118
        }
119
        $this->_theme = $name;
120
    }
121
122
    public function getTheme()
123
    {
124
        if (is_string($this->_theme)) {
125
            if (!$this->has($this->_theme)) {
126
                throw new InvalidConfigException('unknown theme: ' . $this->_theme);
127
            }
128
            $this->_theme = $this->getItem($this->_theme);
129
            $this->getView()->theme = $this->_theme;
130
        }
131
132
        return $this->_theme;
133
    }
134
135
    public function getSettings()
136
    {
137
        return $this->getTheme()->getSettings();
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public static function isHomePage()
144
    {
145
        // todo: if route is change during the request processing the result will be outdated
146
        static $result = null;
147
148
        if ($result === null) {
149
            /** @var Controller $controller */
150
            $actualRoute = Yii::$app->controller->getRoute();
151
152
            list($controller, $actionId) = Yii::$app->createController('');
153
            $actionId = !empty($actionId) ? $actionId : $controller->defaultAction;
154
            $defaultRoute = $controller->getUniqueId() . '/' . $actionId;
155
156
            $result = $actualRoute === $defaultRoute;
157
        }
158
159
        return $result;
160
    }
161
162
    /**
163
     * @var AssetBundle[] assets to be registered at bootstrap
164
     */
165
    public $assets = [];
166
167
    /**
168
     * Register all the assets.
169
     */
170
    public function registerAssets()
171
    {
172
        foreach (array_merge($this->assets, $this->getTheme()->assets) as $asset) {
173
            /** @var AssetBundle $asset */
174
            $asset::register($this->getView());
175
        }
176
    }
177
178
    /**
179
     * @var bool is already bootstrapped
180
     */
181
    protected $_isBootstrapped = false;
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function bootstrap($app)
187
    {
188
        if ($this->_isBootstrapped) {
189
            return;
190
        }
191
        $this->_isBootstrapped = true;
192
193
        Yii::trace('Bootstrap themes', get_called_class() . '::bootstrap');
0 ignored issues
show
Deprecated Code introduced by
The method yii\BaseYii::trace() has been deprecated with message: since 2.0.14. Use [[debug()]] instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
194
195
        $data = $this->getSettingsStorage()->get();
196
        $model = new Settings();
197
        $model->load($data);
198
        $theme = $this->hasItem($model->theme) ? $model->theme : null;
199
        $theme = $theme ?: $this->getDefaultTheme();
200
        $this->setTheme($theme);
201
        $this->getTheme();
202
    }
203
204
    /**
205
     * @return SettingsStorageInterface
206
     */
207
    public function getSettingsStorage()
208
    {
209
        return Yii::$app->get('themeSettingsStorage');
210
    }
211
212
    /**
213
     * @return array
214
     */
215
    public function getThemeSettings()
216
    {
217
        return $this->getSettingsStorage()->get();
218
    }
219
}
220