Passed
Push — master ( 20b8ac...10939c )
by Jean-Christophe
06:31
created

Twig   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 76.27%

Importance

Changes 0
Metric Value
wmc 14
eloc 59
dl 0
loc 123
ccs 45
cts 59
cp 0.7627
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 51 4
A getBlockNames() 0 2 1
A setTheme() 0 9 3
A addPath() 0 2 1
A addFunction() 0 3 2
A render() 0 9 2
A getCode() 0 2 1
1
<?php
2
3
namespace Ubiquity\views\engine;
4
5
use Twig\Environment;
6
use Twig\TwigFunction;
7
use Twig\TwigTest;
8
use Twig\Loader\FilesystemLoader;
9
use Ubiquity\cache\CacheManager;
10
use Ubiquity\controllers\Router;
11
use Ubiquity\controllers\Startup;
12
use Ubiquity\core\Framework;
13
use Ubiquity\events\EventsManager;
14
use Ubiquity\events\ViewEvents;
15
use Ubiquity\exceptions\ThemesException;
16
use Ubiquity\translation\TranslatorManager;
17
use Ubiquity\utils\base\UFileSystem;
18
use Ubiquity\themes\ThemesManager;
19
use Ubiquity\assets\AssetsManager;
20
21
/**
22
 * Ubiquity Twig template engine.
23
 *
24
 * Ubiquity\views\engine$Twig
25
 * This class is part of Ubiquity
26
 *
27
 * @author jcheron <[email protected]>
28
 * @version 1.0.8
29
 *
30
 */
31
class Twig extends TemplateEngine {
32
	private $twig;
33
	private $loader;
34
35 37
	public function __construct($options = array()) {
36 37
		$loader = new FilesystemLoader ( \ROOT . \DS . "views" . \DS );
37 37
		$loader->addPath ( implode ( \DS, [ Startup::getFrameworkDir (),"..","core","views" ] ) . \DS, "framework" );
38 37
		$this->loader = $loader;
39
40 37
		$this->twig = new Environment ( $loader, $options );
41 37
		if (isset ( $options ["cache"] ) && $options ["cache"] === true) {
42
			$options ["cache"] = CacheManager::getCacheSubDirectory ( "views" );
43
		}
44 37
		if (isset ( $options ["activeTheme"] )) {
45 36
			ThemesManager::setActiveThemeFromTwig ( $options ["activeTheme"] );
46 36
			$this->setTheme ( $options ["activeTheme"], ThemesManager::THEMES_FOLDER );
47 36
			unset ( $options ["activeTheme"] );
48
		} else {
49 2
			$this->loader->setPaths ( [ \ROOT . \DS . 'views' ], "activeTheme" );
50
		}
51
52
		$this->addFunction ( 'path', function ($name, $params = [], $absolute = false) {
53
			return Router::path ( $name, $params, $absolute );
54 37
		} );
55
56
		$this->addFunction ( 'url', function ($name, $params) {
57
			return Router::url ( $name, $params );
58 37
		} );
59
60
		$this->addFunction ( 'css_', function ($resource, $parameters = [], $absolute = false) {
61 6
			return AssetsManager::css_ ( $resource, $parameters, $absolute );
62 37
		}, true );
63
64
		$this->addFunction ( 'css', function ($resource, $parameters = [], $absolute = false) {
65
			return AssetsManager::css ( $resource, $parameters, $absolute );
66 37
		}, true );
67
68
		$this->addFunction ( 'js', function ($resource, $parameters = [], $absolute = false) {
69
			return AssetsManager::js ( $resource, $parameters, $absolute );
70 37
		}, true );
71
72
		$this->addFunction ( 'js_', function ($resource, $parameters = [], $absolute = false) {
73
			return AssetsManager::js_ ( $resource, $parameters, $absolute );
74 37
		}, true );
75
76
		$this->addFunction ( 't', function ($context, $id, array $parameters = array(), $domain = null, $locale = null) {
77
			$trans = TranslatorManager::trans ( $id, $parameters, $domain, $locale );
78
			return $this->twig->createTemplate ( $trans )->render ( $context );
79 37
		}, [ 'needs_context' => true ] );
80
81
		$test = new TwigTest ( 'instanceOf', function ($var, $class) {
82
			return $var instanceof $class;
83 37
		} );
84 37
		$this->twig->addTest ( $test );
85 37
		$this->twig->addGlobal ( "app", new Framework () );
86 37
	}
87
88 37
	protected function addFunction($name, $callback, $safe = false) {
89 37
		$options = ($safe) ? [ 'is_safe' => [ 'html' ] ] : [ ];
90 37
		$this->twig->addFunction ( new TwigFunction ( $name, $callback, $options ) );
91 37
	}
92
93
	/*
94
	 * (non-PHPdoc)
95
	 * @see TemplateEngine::render()
96
	 */
97 21
	public function render($viewName, $pData, $asString) {
98 21
		$pData ["config"] = Startup::getConfig ();
99 21
		EventsManager::trigger ( ViewEvents::BEFORE_RENDER, $viewName, $pData );
100 21
		$render = $this->twig->render ( $viewName, $pData );
101 21
		EventsManager::trigger ( ViewEvents::AFTER_RENDER, $render, $viewName, $pData );
102 21
		if ($asString) {
103 1
			return $render;
104
		} else {
105 21
			echo $render;
106
		}
107 21
	}
108
109
	/**
110
	 *
111
	 * {@inheritdoc}
112
	 * @see \Ubiquity\views\engine\TemplateEngine::getBlockNames()
113
	 */
114 2
	public function getBlockNames($templateName) {
115 2
		return $this->twig->load ( $templateName )->getBlockNames ();
116
	}
117
118
	/**
119
	 *
120
	 * {@inheritdoc}
121
	 * @see \Ubiquity\views\engine\TemplateEngine::getCode()
122
	 */
123 1
	public function getCode($templateName) {
124 1
		return UFileSystem::load ( $this->twig->load ( $templateName )->getSourceContext ()->getPath () );
125
	}
126
127
	/**
128
	 * Adds a new path in a namespace
129
	 *
130
	 * @param string $path The path to add
131
	 * @param string $namespace The namespace to use
132
	 */
133
	public function addPath(string $path, string $namespace) {
134
		$this->loader->addPath ( $path, $namespace );
135
	}
136
137
	/**
138
	 * Defines the activeTheme.
139
	 * **activeTheme** namespace is @activeTheme
140
	 *
141
	 * @param string $theme
142
	 * @param string $themeFolder
143
	 * @throws ThemesException
144
	 */
145 36
	public function setTheme($theme, $themeFolder = ThemesManager::THEMES_FOLDER) {
146 36
		$path = \ROOT . \DS . 'views' . \DS . $themeFolder . \DS . $theme;
147 36
		if ($theme == '') {
148
			$path = \ROOT . \DS . 'views';
149
		}
150 36
		if (file_exists ( $path )) {
151 36
			$this->loader->setPaths ( [ $path ], "activeTheme" );
152
		} else {
153
			throw new ThemesException ( sprintf ( 'The path `%s` does not exists!', $path ) );
154
		}
155
	}
156
}