Test Failed
Push — master ( ac669c...c918ec )
by Jean-Christophe
08:37
created

ThemesManager::getActiveTheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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