Passed
Push — master ( ed5274...d4fa57 )
by Jean-Christophe
10:23
created

ThemesManager::saveActiveTheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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