Passed
Push — master ( ddd850...18dc47 )
by Jean-Christophe
12:09
created

Twig::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
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.9
29
 *
30
 */
31
class Twig extends TemplateEngine {
32
	private $twig;
33
	private $loader;
34
35 65
	public function __construct($options = array ()) {
36 65
		$loader = new FilesystemLoader ( \ROOT . \DS . "views" . \DS );
37 65
		$loader->addPath ( implode ( \DS, [ Startup::getFrameworkDir (),"..","core","views" ] ) . \DS, "framework" );
38 65
		$this->loader = $loader;
39
40 65
		if (isset ( $options ["cache"] ) && $options ["cache"] === true) {
41
			$options ["cache"] = CacheManager::getCacheSubDirectory ( "views" );
42
		}
43
44 65
		$this->twig = new Environment ( $loader, $options );
45
46 65
		if (isset ( $options ["activeTheme"] )) {
47 64
			ThemesManager::setActiveThemeFromTwig ( $options ["activeTheme"] );
48 64
			$this->setTheme ( $options ["activeTheme"], ThemesManager::THEMES_FOLDER );
49 64
			unset ( $options ["activeTheme"] );
50
		} else {
51 3
			$this->loader->setPaths ( [ \ROOT . \DS . 'views' ], "activeTheme" );
52
		}
53
54
		$this->addFunction ( 'path', function ($name, $params = [ ], $absolute = false) {
55
			return Router::path ( $name, $params, $absolute );
56 65
		} );
57
58
		$this->addFunction ( 'url', function ($name, $params) {
59
			return Router::url ( $name, $params );
60 65
		} );
61
62 65
		if (\class_exists ( '\\Ubiquity\\security\\csrf\\UCsrfHttp' )) {
63
			$this->addFunction ( 'csrfMeta', function ($name) {
64
				return \Ubiquity\security\csrf\UCsrfHttp::getTokenMeta ( $name );
65
			}, true );
66
			$this->addFunction ( 'csrf', function ($name) {
67
				return \Ubiquity\security\csrf\UCsrfHttp::getTokenField ( $name );
68
			}, true );
69
		}
70
71
		$this->addFunction ( 'css', function ($resource, $parameters = [ ], $absolute = false) {
72 9
			if ($this->hasThemeResource ( $resource )) {
73 9
				return AssetsManager::css_ ( $resource, $parameters, $absolute );
74
			}
75
			return AssetsManager::css ( $resource, $parameters, $absolute );
76 65
		}, true );
77
78
		$this->addFunction ( 'js', function ($resource, $parameters = [ ], $absolute = false) {
79 1
			if ($this->hasThemeResource ( $resource )) {
80 1
				return AssetsManager::js_ ( $resource, $parameters, $absolute );
81
			}
82
			return AssetsManager::js ( $resource, $parameters, $absolute );
83 65
		}, true );
84
85
		$t = new TwigFunction ( 't', function ($context, $id, array $parameters = array (), $domain = null, $locale = null) {
86 1
			$trans = TranslatorManager::trans ( $id, $parameters, $domain, $locale );
87 1
			return $this->twig->createTemplate ( $trans )->render ( $context );
88 65
		}, [ 'needs_context' => true ] );
89
90
		$tc = new TwigFunction ( 'tc', function ($context, $id, array $choice, array $parameters = array (), $domain = null, $locale = null) {
91 1
			$trans = TranslatorManager::transChoice ( $id, $choice, $parameters, $domain, $locale );
92 1
			return $this->twig->createTemplate ( $trans )->render ( $context );
93 65
		}, [ 'needs_context' => true ] );
94 65
		$this->twig->addFunction ( $t );
95 65
		$this->twig->addFunction ( $tc );
96
97
		$test = new TwigTest ( 'instanceOf', function ($var, $class) {
98
			return $var instanceof $class;
99 65
		} );
100 65
		$this->twig->addTest ( $test );
101 65
		$this->twig->addGlobal ( "app", new Framework () );
102 65
	}
103
104 9
	protected function hasThemeResource(&$resource) {
105 9
		$resource = str_replace ( '@activeTheme/', "", $resource, $count );
106 9
		return $count > 0;
107
	}
108
109 65
	protected function addFunction($name, $callback, $safe = false) {
110 65
		$options = ($safe) ? [ 'is_safe' => [ 'html' ] ] : [ ];
111 65
		$this->twig->addFunction ( new TwigFunction ( $name, $callback, $options ) );
112 65
	}
113
114
	/*
115
	 * (non-PHPdoc)
116
	 * @see TemplateEngine::render()
117
	 */
118 29
	public function render($viewName, $pData, $asString) {
119 29
		$pData ["config"] = Startup::getConfig ();
120 29
		EventsManager::trigger ( ViewEvents::BEFORE_RENDER, $viewName, $pData );
121 29
		$render = $this->twig->render ( $viewName, $pData );
122 29
		EventsManager::trigger ( ViewEvents::AFTER_RENDER, $render, $viewName, $pData );
123 29
		if ($asString) {
124 3
			return $render;
125
		} else {
126 29
			echo $render;
127
		}
128 29
	}
129
130
	/**
131
	 *
132
	 * {@inheritdoc}
133
	 * @see \Ubiquity\views\engine\TemplateEngine::getBlockNames()
134
	 */
135 2
	public function getBlockNames($templateName) {
136
		try {
137 2
			$result = $this->twig->load($templateName)->getBlockNames();
138 1
		} catch (\Error $e) {
139
			$result = [];
140
		}
141 2
		return $result;
142
	}
143
144
	/**
145
	 *
146
	 * {@inheritdoc}
147
	 * @see \Ubiquity\views\engine\TemplateEngine::getCode()
148
	 */
149 1
	public function getCode($templateName) {
150 1
		return UFileSystem::load ( $this->twig->load ( $templateName )->getSourceContext ()->getPath () );
151
	}
152
153
	/**
154
	 * Adds a new path in a namespace
155
	 *
156
	 * @param string $path The path to add
157
	 * @param string $namespace The namespace to use
158
	 */
159 16
	public function addPath(string $path, string $namespace) {
160 16
		$this->loader->addPath ( $path, $namespace );
161 16
	}
162
163
	/**
164
	 * Defines the activeTheme.
165
	 * **activeTheme** namespace is @activeTheme
166
	 *
167
	 * @param string $theme
168
	 * @param string $themeFolder
169
	 * @throws ThemesException
170
	 */
171 64
	public function setTheme($theme, $themeFolder = ThemesManager::THEMES_FOLDER) {
172 64
		$path = \ROOT . \DS . 'views' . \DS . $themeFolder . \DS . $theme;
173 64
		if ($theme == '') {
174
			$path = \ROOT . \DS . 'views';
175
		}
176 64
		if (file_exists ( $path )) {
177 64
			$this->loader->setPaths ( [ $path ], "activeTheme" );
178
		} else {
179
			throw new ThemesException ( sprintf ( 'The path `%s` does not exists!', $path ) );
180
		}
181 64
	}
182
183
	/**
184
	 * Checks if we have the source code of a template, given its name.
185
	 *
186
	 * @param string $name
187
	 * @return boolean
188
	 */
189 1
	public function exists($name) {
190 1
		return $this->twig->getLoader ()->exists ( $name );
191
	}
192
}