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