Passed
Push — master ( 523e5d...e0a7c2 )
by Jean-Christophe
06:26
created

Twig::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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