Passed
Push — master ( a02cde...eb9e44 )
by Jean-Christophe
09:52
created

Twig::addFunction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 3
cts 3
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 46
	public function __construct($options = array()) {
36 46
		$loader = new FilesystemLoader ( \ROOT . \DS . "views" . \DS );
37 46
		$loader->addPath ( implode ( \DS, [ Startup::getFrameworkDir (),"..","core","views" ] ) . \DS, "framework" );
38 46
		$this->loader = $loader;
39
40 46
		if (isset ( $options ["cache"] ) && $options ["cache"] === true) {
41
			$options ["cache"] = CacheManager::getCacheSubDirectory ( "views" );
42
		}
43
44 46
		$this->twig = new Environment ( $loader, $options );
45
46 46
		if (isset ( $options ["activeTheme"] )) {
47 45
			ThemesManager::setActiveThemeFromTwig ( $options ["activeTheme"] );
48 45
			$this->setTheme ( $options ["activeTheme"], ThemesManager::THEMES_FOLDER );
49 45
			unset ( $options ["activeTheme"] );
50
		} else {
51 2
			$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 46
		} );
57
58
		$this->addFunction ( 'url', function ($name, $params) {
59
			return Router::url ( $name, $params );
60 46
		} );
61
62
		$this->addFunction ( 'css', function ($resource, $parameters = [], $absolute = false) {
63 8
			if ($this->hasThemeResource ( $resource )) {
64 8
				return AssetsManager::css_ ( $resource, $parameters, $absolute );
65
			}
66 1
			return AssetsManager::css ( $resource, $parameters, $absolute );
67 46
		}, true );
68
69
		$this->addFunction ( 'js', function ($resource, $parameters = [], $absolute = false) {
70 1
			if ($this->hasThemeResource ( $resource )) {
71
				return AssetsManager::js_ ( $resource, $parameters, $absolute );
72
			}
73 1
			return AssetsManager::js ( $resource, $parameters, $absolute );
74 46
		}, 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 46
		}, [ 'needs_context' => true ] );
80
81
		$this->addFunction ( 'tc', function ($context, $id, array $choice, array $parameters = array(), $domain = null, $locale = null) {
82
			$trans = TranslatorManager::transChoice ( $id, $choice, $parameters, $domain, $locale );
83
			return $this->twig->createTemplate ( $trans )->render ( $context );
84 46
		}, [ 'needs_context' => true ] );
85
86
		$test = new TwigTest ( 'instanceOf', function ($var, $class) {
87
			return $var instanceof $class;
88 46
		} );
89 46
		$this->twig->addTest ( $test );
90 46
		$this->twig->addGlobal ( "app", new Framework () );
91 46
	}
92
93 8
	protected function hasThemeResource(&$resource) {
94 8
		$resource = str_replace ( '@activeTheme/', "", $resource, $count );
95 8
		return $count > 0;
96
	}
97
98 46
	protected function addFunction($name, $callback, $safe = false) {
99 46
		$options = ($safe) ? [ 'is_safe' => [ 'html' ] ] : [ ];
100 46
		$this->twig->addFunction ( new TwigFunction ( $name, $callback, $options ) );
101 46
	}
102
103
	/*
104
	 * (non-PHPdoc)
105
	 * @see TemplateEngine::render()
106
	 */
107 23
	public function render($viewName, $pData, $asString) {
108 23
		$pData ["config"] = Startup::getConfig ();
109 23
		EventsManager::trigger ( ViewEvents::BEFORE_RENDER, $viewName, $pData );
110 23
		$render = $this->twig->render ( $viewName, $pData );
111 23
		EventsManager::trigger ( ViewEvents::AFTER_RENDER, $render, $viewName, $pData );
112 23
		if ($asString) {
113 2
			return $render;
114
		} else {
115 23
			echo $render;
116
		}
117 23
	}
118
119
	/**
120
	 *
121
	 * {@inheritdoc}
122
	 * @see \Ubiquity\views\engine\TemplateEngine::getBlockNames()
123
	 */
124 2
	public function getBlockNames($templateName) {
125 2
		return $this->twig->load ( $templateName )->getBlockNames ();
126
	}
127
128
	/**
129
	 *
130
	 * {@inheritdoc}
131
	 * @see \Ubiquity\views\engine\TemplateEngine::getCode()
132
	 */
133 1
	public function getCode($templateName) {
134 1
		return UFileSystem::load ( $this->twig->load ( $templateName )->getSourceContext ()->getPath () );
135
	}
136
137
	/**
138
	 * Adds a new path in a namespace
139
	 *
140
	 * @param string $path The path to add
141
	 * @param string $namespace The namespace to use
142
	 */
143
	public function addPath(string $path, string $namespace) {
144
		$this->loader->addPath ( $path, $namespace );
145
	}
146
147
	/**
148
	 * Defines the activeTheme.
149
	 * **activeTheme** namespace is @activeTheme
150
	 *
151
	 * @param string $theme
152
	 * @param string $themeFolder
153
	 * @throws ThemesException
154
	 */
155 45
	public function setTheme($theme, $themeFolder = ThemesManager::THEMES_FOLDER) {
156 45
		$path = \ROOT . \DS . 'views' . \DS . $themeFolder . \DS . $theme;
157 45
		if ($theme == '') {
158
			$path = \ROOT . \DS . 'views';
159
		}
160 45
		if (file_exists ( $path )) {
161 45
			$this->loader->setPaths ( [ $path ], "activeTheme" );
162
		} else {
163
			throw new ThemesException ( sprintf ( 'The path `%s` does not exists!', $path ) );
164
		}
165
	}
166
}