1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\themes; |
4
|
|
|
|
5
|
|
|
use Ubiquity\controllers\Startup; |
6
|
|
|
use Ubiquity\views\engine\Twig; |
7
|
|
|
use Ubiquity\exceptions\ThemesException; |
8
|
|
|
use Ubiquity\events\EventsManager; |
9
|
|
|
use Ubiquity\events\ViewEvents; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Themes manager. |
13
|
|
|
* Ubiquity\themes$ThemesManager |
14
|
|
|
* This class is part of Ubiquity |
15
|
|
|
* |
16
|
|
|
* @author jcheron <[email protected]> |
17
|
|
|
* @version 1.1.0 |
18
|
|
|
* @since Ubiquity 2.1.0 |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
class ThemesManager { |
22
|
|
|
const THEMES_FOLDER = 'themes'; |
23
|
|
|
private static $activeTheme; |
24
|
|
|
private static $refThemes = [ 'bootstrap','foundation','semantic' ]; |
25
|
|
|
|
26
|
8 |
|
public static function getActiveTheme() { |
27
|
8 |
|
return self::$activeTheme; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sets the activeTheme |
32
|
|
|
* |
33
|
|
|
* @param string $activeTheme |
34
|
|
|
* @throws ThemesException |
35
|
|
|
*/ |
36
|
1 |
|
public static function setActiveTheme($activeTheme) { |
37
|
1 |
|
self::$activeTheme = $activeTheme ?? ''; |
38
|
1 |
|
$engineInstance = Startup::$templateEngine; |
39
|
1 |
|
if ($engineInstance instanceof Twig) { |
40
|
1 |
|
$engineInstance->setTheme ( $activeTheme, self::THEMES_FOLDER ); |
41
|
|
|
} else { |
42
|
|
|
throw new ThemesException ( 'Template engine must be an instance of Twig for themes activation!' ); |
43
|
|
|
} |
44
|
1 |
|
} |
45
|
|
|
|
46
|
1 |
|
public static function saveActiveTheme($theme) { |
47
|
1 |
|
$config = Startup::getConfig (); |
48
|
1 |
|
$config ['templateEngineOptions'] ['activeTheme'] = $theme; |
49
|
1 |
|
Startup::saveConfig ( $config ); |
50
|
1 |
|
return $config; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets the activeTheme |
55
|
|
|
* |
56
|
|
|
* @param string $activeTheme |
57
|
|
|
*/ |
58
|
46 |
|
public static function setActiveThemeFromTwig($activeTheme) { |
59
|
46 |
|
self::$activeTheme = $activeTheme; |
60
|
46 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the names of available themes. |
64
|
|
|
* |
65
|
|
|
* @return string[] |
66
|
|
|
*/ |
67
|
3 |
|
public static function getAvailableThemes() { |
68
|
3 |
|
$path = \ROOT . \DS . 'views' . \DS . self::THEMES_FOLDER . \DS . '*'; |
69
|
3 |
|
$dirs = \glob ( $path, GLOB_ONLYDIR | GLOB_NOSORT ); |
70
|
3 |
|
$result = [ ]; |
71
|
3 |
|
foreach ( $dirs as $dir ) { |
72
|
3 |
|
$result [] = basename ( $dir ); |
73
|
|
|
} |
74
|
3 |
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns all referenced themes. |
79
|
|
|
* |
80
|
|
|
* @return string[] |
81
|
|
|
*/ |
82
|
1 |
|
public static function getRefThemes() { |
83
|
1 |
|
return self::$refThemes; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns if a theme is a custom Theme (not in refThemes). |
88
|
|
|
* |
89
|
|
|
* @param string $theme |
90
|
|
|
* @return boolean |
91
|
|
|
*/ |
92
|
3 |
|
public static function isCustom($theme) { |
93
|
3 |
|
return array_search ( $theme, self::$refThemes ) === false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the not installed themes |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
1 |
|
public static function getNotInstalledThemes() { |
102
|
1 |
|
$AvailableThemes = self::getAvailableThemes (); |
103
|
1 |
|
return array_diff ( self::$refThemes, $AvailableThemes ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Adds a listener before theme rendering. |
108
|
|
|
* The callback function takes the following parameters: $view (the view name), $pData (array of datas sent to the view) |
109
|
|
|
* |
110
|
|
|
* @param callable $callback |
111
|
|
|
*/ |
112
|
|
|
public static function onBeforeRender($callback) { |
113
|
|
|
EventsManager::addListener ( ViewEvents::BEFORE_RENDER, $callback ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Adds a listener after theme rendering. |
118
|
|
|
* The callback function takes the following parameters: $render (the response string), $view (the view name), $pData (array of datas sent to the view) |
119
|
|
|
* |
120
|
|
|
* @param callable $callback |
121
|
|
|
*/ |
122
|
|
|
public static function onAfterRender($callback) { |
123
|
|
|
EventsManager::addListener ( ViewEvents::AFTER_RENDER, $callback ); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|