Test Failed
Push — master ( 545d2f...b75c4f )
by Jean-Christophe
10:08
created

ThemesManager::getRefThemes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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.0.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 2
27 2
	public static function getActiveTheme() {
28
		return self::$activeTheme;
29
	}
30
31
	/**
32
	 * Sets the activeTheme
33
	 *
34
	 * @param string $activeTheme
35
	 * @throws ThemesException
36
	 */
37
	public static function setActiveTheme($activeTheme) {
38
		self::$activeTheme = $activeTheme ?? '';
39
		$engineInstance = Startup::$templateEngine;
40
		if ($engineInstance instanceof Twig) {
41
			$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
	}
46
47
	public static function saveActiveTheme($theme) {
48
		$config = Startup::getConfig ();
49
		$config ['templateEngineOptions'] ['activeTheme'] = $theme;
50
		$content = "<?php\nreturn " . UArray::asPhpArray ( $config, "array", 1, true ) . ";";
51
		Startup::saveConfig ( $content );
52
		return $config;
53
	}
54
55
	/**
56
	 * Sets the activeTheme
57
	 *
58
	 * @param string $activeTheme
59 36
	 */
60 36
	public static function setActiveThemeFromTwig($activeTheme) {
61 36
		self::$activeTheme = $activeTheme;
62
	}
63
64
	/**
65
	 * Returns the names of available themes.
66
	 *
67
	 * @return string[]
68 2
	 */
69 2
	public static function getAvailableThemes() {
70 2
		$path = \ROOT . \DS . 'views' . \DS . self::THEMES_FOLDER . \DS . '*';
71 2
		$dirs = \glob ( $path, GLOB_ONLYDIR | GLOB_NOSORT );
72 2
		$result = [ ];
73 2
		foreach ( $dirs as $dir ) {
74
			$result [] = basename ( $dir );
75 2
		}
76
		return $result;
77
	}
78
79
	/**
80
	 * Returns all referenced themes
81
	 *
82
	 * @return string[]
83
	 */
84
	public static function getRefThemes() {
85
		return self::$refThemes;
86
	}
87
88
	/**
89
	 * Returns the not installed themes
90
	 *
91
	 * @return array
92
	 */
93
	public static function getNotInstalledThemes() {
94
		$AvailableThemes = self::getAvailableThemes ();
95
		return array_diff ( self::$refThemes, $AvailableThemes );
96
	}
97
98
	/**
99
	 * Adds a listener before theme rendering.
100
	 * The callback function takes the following parameters: $view (the view name), $pData (array of datas sent to the view)
101
	 *
102
	 * @param callable $callback
103
	 */
104
	public static function onBeforeRender($callback) {
105
		EventsManager::addListener ( ViewEvents::BEFORE_RENDER, $callback );
106
	}
107
108
	/**
109
	 * Adds a listener after theme rendering.
110
	 * The callback function takes the following parameters: $render (the response string), $view (the view name), $pData (array of datas sent to the view)
111
	 *
112
	 * @param callable $callback
113
	 */
114
	public static function onAfterRender($callback) {
115
		EventsManager::addListener ( ViewEvents::AFTER_RENDER, $callback );
116
	}
117
}
118