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 Yii; |
16
|
|
|
use yii\base\Application; |
17
|
|
|
use yii\base\InvalidConfigException; |
18
|
|
|
use yii\base\Widget; |
19
|
|
|
use yii\web\AssetBundle; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Theme Manager. |
23
|
|
|
* |
24
|
|
|
* Usage, in config: |
25
|
|
|
* ``` |
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
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
protected $_itemClass = Theme::class; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array additional dirs to look for views. |
42
|
|
|
*/ |
43
|
|
|
public $pathDirs = []; |
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
|
|
|
* Returns the view object that can be used to render views or view files. |
79
|
|
|
* The [[render()]] and [[renderFile()]] methods will use |
80
|
|
|
* this view object to implement the actual view rendering. |
81
|
|
|
* If not set, it will default to the "view" application component. |
82
|
|
|
* |
83
|
|
|
* @return \yii\web\View the view object that can be used to render views or view files. |
84
|
|
|
*/ |
85
|
|
|
public function getView() |
86
|
|
|
{ |
87
|
|
|
if ($this->_view === null) { |
88
|
|
|
$this->_view = Yii::$app->getView(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->_view; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets the view object to be used. |
96
|
|
|
* |
97
|
|
|
* @param View $view the view object that can 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->view->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
|
|
|
$controller = Yii::$app->controller; |
146
|
|
|
$default_controller = Yii::$app->defaultRoute; |
147
|
|
|
return (($controller->id === $default_controller) && ($controller->action->id === $controller->defaultAction)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @var array assets of the application |
152
|
|
|
*/ |
153
|
|
|
public $assets = []; |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Register all the assets. |
157
|
|
|
*/ |
158
|
|
|
public function registerAssets() |
159
|
|
|
{ |
160
|
|
|
foreach (array_merge($this->assets, $this->getTheme()->assets) as $asset) { |
161
|
|
|
/** |
162
|
|
|
* @var AssetBundle |
163
|
|
|
*/ |
164
|
|
|
$asset::register($this->getView()); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @var bool is already bootstrapped. |
170
|
|
|
*/ |
171
|
|
|
protected $_isBootstrapped = false; |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
|
|
public function bootstrap($app) |
177
|
|
|
{ |
178
|
|
|
if ($this->_isBootstrapped) { |
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
$this->_isBootstrapped = true; |
182
|
|
|
|
183
|
|
|
Yii::trace('Bootstrap themes', get_called_class() . '::bootstrap'); |
184
|
|
|
|
185
|
|
|
$model = new Settings(); |
186
|
|
|
$model->load(); |
187
|
|
|
$theme = $this->hasItem($model->theme) ? $model->theme : null; |
188
|
|
|
$theme = $theme ?: $this->getDefaultTheme(); |
189
|
|
|
$this->setTheme($theme); |
190
|
|
|
$this->getTheme(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/* public function init() |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
parent::init(); |
196
|
|
|
} */ |
197
|
|
|
|
198
|
|
|
public $widgets = []; |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Draws widget. |
202
|
|
|
* @param mixed $config |
203
|
|
|
* @throws InvalidConfigException |
204
|
|
|
* @return void |
205
|
|
|
*/ |
206
|
|
|
public function widget($config) |
207
|
|
|
{ |
208
|
|
|
if (!is_array($config)) { |
209
|
|
|
$config = ['class' => $config]; |
210
|
|
|
} |
211
|
|
|
if (!isset($config['class'])) { |
212
|
|
|
throw new InvalidConfigException('no class given'); |
213
|
|
|
} |
214
|
|
|
if (isset($this->widgets[$config['class']])) { |
215
|
|
|
$config['class'] = $this->widgets[$config['class']]; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
ob_start(); |
219
|
|
|
ob_implicit_flush(false); |
220
|
|
|
try { |
221
|
|
|
/* @var $widget Widget */ |
222
|
|
|
$widget = Yii::createObject($config); |
223
|
|
|
$out = $widget->run(); |
224
|
|
|
} catch (\Exception $e) { |
225
|
|
|
// close the output buffer opened above if it has not been closed already |
226
|
|
|
if (ob_get_level() > 0) { |
227
|
|
|
ob_end_clean(); |
228
|
|
|
} |
229
|
|
|
throw $e; |
230
|
|
|
} |
231
|
|
|
return ob_get_clean() . $out; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Checks if widget with given name is defined. |
236
|
|
|
* @param mixed $name name or class |
237
|
|
|
* @return boolean |
238
|
|
|
*/ |
239
|
|
|
public function hasWidget($name) |
240
|
|
|
{ |
241
|
|
|
return (isset($this->widgets[$name]) && $this->widgets[$name]) || class_exists($name); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.