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 ReflectionClass; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Theme class. |
20
|
|
|
*/ |
21
|
|
|
class Theme extends \yii\base\Theme implements \hiqdev\yii2\collection\ItemWithNameInterface |
22
|
|
|
{ |
23
|
|
|
use GetManagerTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string theme name |
27
|
|
|
*/ |
28
|
|
|
public $name; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string theme label |
32
|
|
|
*/ |
33
|
|
|
public $label; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array assets to be registered for this theme |
37
|
|
|
*/ |
38
|
|
|
public $assets = []; |
39
|
|
|
|
40
|
|
|
private $_view; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the view object that can be used to render views or view files. |
44
|
|
|
* The [[render()]] and [[renderFile()]] methods will use |
45
|
|
|
* this view object to implement the actual view rendering. |
46
|
|
|
* If not set, it will default to the "view" application component. |
47
|
|
|
* |
48
|
|
|
* @return \yii\web\View the view object that can be used to render views or view files |
49
|
|
|
*/ |
50
|
|
|
public function getView() |
51
|
|
|
{ |
52
|
|
|
if ($this->_view === null) { |
53
|
|
|
$this->_view = $this->getManager()->getView(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->_view; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Sets the view object to be used. |
61
|
|
|
* |
62
|
|
|
* @param View $view the view object that can be used to render views or view files |
63
|
|
|
*/ |
64
|
|
|
public function setView($view) |
65
|
|
|
{ |
66
|
|
|
$this->_view = $view; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public $pathMap = [ |
70
|
|
|
__DIR__ . '/widgets/views' => '$themedWidgetPaths', |
71
|
|
|
'$themedWidgetPaths' => '$themedViewPaths/widgets', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Getter for pathMap. |
76
|
|
|
*/ |
77
|
|
|
public function init() |
78
|
|
|
{ |
79
|
|
|
parent::init(); |
80
|
|
|
if (!is_array($this->pathMap)) { |
81
|
|
|
$this->pathMap = []; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->pathMap = $this->compilePathMap(ArrayHelper::merge( |
85
|
|
|
['$themedViewPaths' => $this->buildThemedViewPaths()], |
86
|
|
|
$this->getManager()->pathMap, $this->pathMap |
87
|
|
|
)); |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function compilePathMap($map) |
92
|
|
|
{ |
93
|
|
|
$map = $this->substituteVars($map); |
94
|
|
|
|
95
|
|
|
foreach ($map as $from => &$tos) { |
96
|
|
|
$tos = array_reverse(array_unique(array_values($tos))); |
97
|
|
|
foreach ($tos as &$to) { |
98
|
|
|
$to = Yii::getAlias($to); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $map; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function substituteVars($vars) |
106
|
|
|
{ |
107
|
|
|
$proceed = true; |
108
|
|
|
while ($proceed) { |
109
|
|
|
$proceed = false; |
110
|
|
|
foreach ($vars as $key => $exp) { |
111
|
|
|
if ($this->isVar($exp)) { |
112
|
|
|
$value = $this->calcExp($exp, $vars); |
113
|
|
|
if (isset($value)) { |
114
|
|
|
$vars[$key] = $value; |
115
|
|
|
$proceed = true; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
foreach (array_keys($vars) as $key) { |
122
|
|
|
if ($this->isVar($key)) { |
123
|
|
|
unset($vars[$key]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $vars; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function isVar($name) |
131
|
|
|
{ |
132
|
|
|
return is_string($name) && (strncmp($name, '$', 1) === 0); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function calcExp($exp, $vars) |
136
|
|
|
{ |
137
|
|
|
$pos = strpos($exp, '/'); |
138
|
|
|
if ($pos === FALSE) { |
139
|
|
|
return $vars[$exp]; |
140
|
|
|
} |
141
|
|
|
list($name, $suffix) = explode('/', $exp, 2); |
142
|
|
|
|
143
|
|
|
return array_map(function ($a) use ($suffix) { return "$a/$suffix"; }, $vars[$name]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function buildThemedViewPaths() |
147
|
|
|
{ |
148
|
|
|
return array_map(function ($a) { return "$a/views"; }, $this->findParentPaths()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function findParentPaths() |
152
|
|
|
{ |
153
|
|
|
$ref = $this->getReflection(); |
154
|
|
|
for ($depth = 0; $depth < 10; ++$depth) { |
155
|
|
|
$dirs[] = dirname($ref->getFilename()); |
|
|
|
|
156
|
|
|
$ref = $ref->getParentClass(); |
157
|
|
|
if (__CLASS__ === $ref->name) { |
158
|
|
|
break; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $dirs; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
protected $_reflection; |
166
|
|
|
|
167
|
|
|
public function getReflection() |
168
|
|
|
{ |
169
|
|
|
if (!$this->_reflection) { |
170
|
|
|
$this->_reflection = new ReflectionClass($this); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->_reflection; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
private $_settings; |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $settings theme settings model class name or config. |
180
|
|
|
*/ |
181
|
|
|
public function setSettings($settings) |
182
|
|
|
{ |
183
|
|
|
$this->_settings = $settings; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function getSettings() |
187
|
|
|
{ |
188
|
|
|
if (!is_object($this->_settings)) { |
189
|
|
|
if (!$this->_settings) { |
190
|
|
|
$this->_settings = static::findSettingsClass(get_called_class()); |
191
|
|
|
} |
192
|
|
|
$this->_settings = Yii::createObject($this->_settings); |
193
|
|
|
$this->_settings->load(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->_settings; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public static function calcSettingsClass($class) |
200
|
|
|
{ |
201
|
|
|
return substr($class, 0, strrpos($class, '\\')) . '\\models\\Settings'; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public static function findSettingsClass($class) |
205
|
|
|
{ |
206
|
|
|
$res = static::calcSettingsClass($class); |
207
|
|
|
|
208
|
|
|
return class_exists($res) ? $res : static::findSettingsClass(get_parent_class($class)); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.