1 | <?php |
||
23 | class ThemeManager extends \yii\base\Component |
||
24 | { |
||
25 | /** |
||
26 | * Name of the event before the active theme will be setup. |
||
27 | */ |
||
28 | const EVENT_BEFORE_SETUP = 'eventBeforeSetup'; |
||
29 | |||
30 | /** |
||
31 | * @var string Name of the theme which should be activated on setup. This is commonly used to defined the active theme when **not** |
||
32 | * using the CMS ThemeManager to switch between themes. |
||
33 | */ |
||
34 | public $activeThemeName; |
||
35 | |||
36 | /** |
||
37 | * @var ThemeConfig[] |
||
38 | */ |
||
39 | private $_themes = []; |
||
40 | |||
41 | /** |
||
42 | * Read the theme.json and create a new \luya\theme\ThemeConfig for the given base path. |
||
43 | * |
||
44 | * @param string $basePath Base path can either be a path to a folder with theme.json files or an absolute path to a theme.json file |
||
45 | * @return ThemeConfig |
||
46 | * @throws Exception |
||
47 | * @throws InvalidConfigException |
||
48 | */ |
||
49 | public function loadThemeConfig(string $basePath) |
||
50 | { |
||
51 | if (strpos($basePath, '@') === 0) { |
||
52 | $dir = Yii::getAlias($basePath); |
||
53 | } elseif (strpos($basePath, '/') === 0) { |
||
54 | // absolute path |
||
55 | $dir = $basePath; |
||
56 | } else { |
||
57 | // relative path |
||
58 | throw new InvalidConfigException('Theme base path have to be absolute or alias: ' . $basePath); |
||
59 | } |
||
60 | |||
61 | // $basePath is an absolute path = /VENDOR/NAME/theme.json |
||
62 | if (is_file($basePath) && file_exists($basePath)) { |
||
63 | $themeFile = $basePath; |
||
64 | // if basePath is the theme file itself and existing process: |
||
65 | $basePath = pathinfo($basePath, PATHINFO_DIRNAME); |
||
66 | } else { |
||
67 | if (!is_dir($dir) || !is_readable($dir)) { |
||
68 | throw new Exception('Theme directory not exists or readable: ' . $dir); |
||
69 | } |
||
70 | |||
71 | $themeFile = $dir . DIRECTORY_SEPARATOR . 'theme.json'; |
||
72 | if (!file_exists($themeFile)) { |
||
73 | throw new InvalidConfigException('Theme config file missing at: ' . $themeFile); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | $config = Json::decode(file_get_contents($themeFile)) ?: []; |
||
78 | |||
79 | return new ThemeConfig($basePath, $config); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Setup active theme |
||
84 | * |
||
85 | * @throws Exception |
||
86 | * @throws InvalidConfigException |
||
87 | * @throws \yii\db\Exception |
||
88 | */ |
||
89 | public function setup() |
||
105 | |||
106 | /** |
||
107 | * Trigger the {{\luya\theme\ThemeManager::EVENT_BEFORE_SETUP}} event. |
||
108 | * |
||
109 | * @param string $basePath |
||
110 | */ |
||
111 | protected function beforeSetup(string &$basePath) |
||
119 | |||
120 | /** |
||
121 | * Get base path of active theme. |
||
122 | * |
||
123 | * @return string |
||
124 | * @throws \yii\db\Exception |
||
125 | */ |
||
126 | protected function getActiveThemeBasePath() |
||
134 | |||
135 | /** |
||
136 | * Get all theme configs as array list. |
||
137 | * |
||
138 | * @param bool $throwException Whether an exception should by throw or not. By version 1.0.24 this is disabled by default. |
||
139 | * @return ThemeConfig[] |
||
140 | * @throws \yii\base\Exception |
||
141 | */ |
||
142 | public function getThemes($throwException = false) |
||
163 | |||
164 | /** |
||
165 | * Get theme definitions by search in `@app/themes` and the `Yii::$app->getPackageInstaller()` |
||
166 | * |
||
167 | * @return string[] |
||
168 | */ |
||
169 | protected function getThemeDefinitions() |
||
193 | |||
194 | /** |
||
195 | * @param string $basePath |
||
196 | * @param bool $throwException |
||
197 | * |
||
198 | * @return ThemeConfig |
||
199 | * @throws \yii\base\Exception |
||
200 | * @throws InvalidConfigException |
||
201 | */ |
||
202 | public function getThemeByBasePath(string $basePath, $throwException = false) |
||
212 | |||
213 | /** |
||
214 | * Register a theme config and set the path alias with the name of the theme. |
||
215 | * |
||
216 | * @param ThemeConfig $themeConfig Base path of the theme. |
||
217 | * |
||
218 | * @throws InvalidConfigException |
||
219 | */ |
||
220 | protected function registerTheme(ThemeConfig $themeConfig) |
||
231 | |||
232 | /** |
||
233 | * Change the active theme in the \yii\base\View component and set the `activeTheme ` alias to new theme base path. |
||
234 | * |
||
235 | * @param Theme $theme |
||
236 | */ |
||
237 | protected function activate(Theme $theme) |
||
244 | |||
245 | /** |
||
246 | * @var Theme|null |
||
247 | */ |
||
248 | private $_activeTheme; |
||
249 | |||
250 | /** |
||
251 | * Get the active theme. Null if no theme is activated. |
||
252 | * |
||
253 | * @return Theme|null |
||
254 | */ |
||
255 | public function getActiveTheme() |
||
259 | |||
260 | /** |
||
261 | * Change the active theme. |
||
262 | * |
||
263 | * @param Theme $theme |
||
264 | */ |
||
265 | protected function setActiveTheme(Theme $theme) |
||
269 | |||
270 | /** |
||
271 | * Check if a theme is activated. |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function getHasActiveTheme() |
||
279 | } |
||
280 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.