|
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
|
|
|
/** |
|
70
|
|
|
* Getter for pathMap. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function init() |
|
73
|
|
|
{ |
|
74
|
|
|
parent::init(); |
|
75
|
|
|
if (!is_array($this->pathMap)) { |
|
76
|
|
|
$this->pathMap = []; |
|
77
|
|
|
} |
|
78
|
|
|
$this->pathMap = ArrayHelper::merge([ |
|
79
|
|
|
$this->getViewPath() => $this->buildViewPaths(), |
|
80
|
|
|
], $this->pathMap); |
|
81
|
|
|
$this->pathMap[$this->getViewPath()] = array_reverse(array_unique(array_values($this->pathMap[$this->getViewPath()]))); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected $_viewPath; |
|
85
|
|
|
protected $_widgetPath; |
|
86
|
|
|
|
|
87
|
|
|
public function getViewPath() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->_viewPath ?: Yii::$app->viewPath; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getWidgetPath() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->_widgetPath ?: preg_replace('/(.*)views/', '$1widgets', $this->getViewPath()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function calcParentPaths() |
|
98
|
|
|
{ |
|
99
|
|
|
$ref = $this->getReflection(); |
|
100
|
|
|
for ($depth = 0;$depth < 10;++$depth) { |
|
101
|
|
|
$dirs[] = dirname($ref->getFilename()); |
|
|
|
|
|
|
102
|
|
|
$ref = $ref->getParentClass(); |
|
103
|
|
|
if (__CLASS__ === $ref->name) { |
|
104
|
|
|
break; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $dirs; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function buildViewPaths() |
|
112
|
|
|
{ |
|
113
|
|
|
foreach ($this->calcParentPaths() as $dir) { |
|
114
|
|
|
$res[] = $dir . DIRECTORY_SEPARATOR . 'views'; |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
foreach ($this->getManager()->viewPaths as $dir) { |
|
117
|
|
|
$res[] = $dir; |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $res; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected $_baseUrl; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return string the base URL (without ending slash) for this theme. |
|
127
|
|
|
* All resources of this theme are considered to be under this base URL. |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getBaseUrl() |
|
130
|
|
|
{ |
|
131
|
|
|
if (!$this->_baseUrl) { |
|
132
|
|
|
$this->_baseUrl = '@web/themes/' . $this->name; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $this->_baseUrl; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
protected $_reflection; |
|
139
|
|
|
|
|
140
|
|
|
public function getReflection() |
|
141
|
|
|
{ |
|
142
|
|
|
if (!$this->_reflection) { |
|
143
|
|
|
$this->_reflection = new ReflectionClass($this); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $this->_reflection; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
private $_settings; |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param $settings string theme settings model class name |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setSettings($settings) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->_settings = $settings; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function getSettings() |
|
160
|
|
|
{ |
|
161
|
|
|
if (!is_object($this->_settings)) { |
|
162
|
|
|
if (!$this->_settings) { |
|
163
|
|
|
$this->_settings = static::findSettingsClass(get_called_class()); |
|
164
|
|
|
} |
|
165
|
|
|
$this->_settings = Yii::createObject($this->_settings); |
|
166
|
|
|
$this->_settings->load(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this->_settings; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public static function calcSettingsClass($class) |
|
173
|
|
|
{ |
|
174
|
|
|
return substr($class, 0, strrpos($class, '\\')) . '\\models\\Settings'; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public static function findSettingsClass($class) |
|
178
|
|
|
{ |
|
179
|
|
|
$res = static::calcSettingsClass($class); |
|
180
|
|
|
|
|
181
|
|
|
return class_exists($res) ? $res : static::findSettingsClass(get_parent_class($class)); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.