Passed
Push — master ( d31551...bbba5c )
by Jean-Christophe
07:02
created

Twig::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.1214

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 28
ccs 16
cts 21
cp 0.7619
rs 9.584
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3.1214
1
<?php
2
3
namespace Ubiquity\views\engine;
4
5
use Ubiquity\controllers\Startup;
6
use Ubiquity\controllers\Router;
7
use Ubiquity\cache\CacheManager;
8
use Ubiquity\core\Framework;
9
use Ubiquity\utils\base\UFileSystem;
10
use Ubiquity\translation\TranslatorManager;
11
use Twig\Loader\FilesystemLoader;
12
use Twig\Environment;
13
use Twig\TwigFunction;
14
use Twig\TwigTest;
15
16
/**
17
 * Ubiquity Twig template engine.
18
 *
19
 * Ubiquity\views\engine$Twig
20
 * This class is part of Ubiquity
21
 *
22
 * @author jcheron <[email protected]>
23
 * @version 1.0.5
24
 *
25
 */
26
class Twig extends TemplateEngine {
27
	private $twig;
28
	private $loader;
29
30 33
	public function __construct($options = array()) {
31 33
		$loader = new FilesystemLoader ( \ROOT . \DS . "views" . \DS );
32 33
		$loader->addPath ( implode ( \DS, [ Startup::getFrameworkDir (),"..","core","views" ] ) . \DS, "framework" );
33 33
		if (isset ( $options ["cache"] ) && $options ["cache"] === true)
34
			$options ["cache"] = CacheManager::getCacheSubDirectory ( "views" );
35 33
		$this->twig = new Environment ( $loader, $options );
36
37
		$function = new TwigFunction ( 'path', function ($name, $params = [], $absolute = false) {
38 2
			return Router::path ( $name, $params, $absolute );
39 33
		} );
40 33
		$this->twig->addFunction ( $function );
41
		$function = new TwigFunction ( 'url', function ($name, $params) {
42
			return Router::url ( $name, $params );
43 33
		} );
44 33
		$this->twig->addFunction ( $function );
45
46
		$function = new TwigFunction ( 't', function ($context, $id, array $parameters = array(), $domain = null, $locale = null) {
47
			$trans = TranslatorManager::trans ( $id, $parameters, $domain, $locale );
48
			return $this->twig->createTemplate ( $trans )->render ( $context );
49 33
		}, [ 'needs_context' => true ] );
50 33
		$this->twig->addFunction ( $function );
51
52
		$test = new TwigTest ( 'instanceOf', function ($var, $class) {
53
			return $var instanceof $class;
54 33
		} );
55 33
		$this->twig->addTest ( $test );
56 33
		$this->twig->addGlobal ( "app", new Framework () );
57 33
		$this->loader = $loader;
58 33
	}
59
60
	/*
61
	 * (non-PHPdoc)
62
	 * @see TemplateEngine::render()
63
	 */
64 21
	public function render($viewName, $pData, $asString) {
65 21
		$pData ["config"] = Startup::getConfig ();
66 21
		$render = $this->twig->render ( $viewName, $pData );
67 21
		if ($asString) {
68 1
			return $render;
69
		} else {
70 21
			echo $render;
71
		}
72 21
	}
73
74
	/**
75
	 *
76
	 * {@inheritdoc}
77
	 * @see \Ubiquity\views\engine\TemplateEngine::getBlockNames()
78
	 */
79 2
	public function getBlockNames($templateName) {
80 2
		return $this->twig->load ( $templateName )->getBlockNames ();
81
	}
82
83
	/**
84
	 *
85
	 * {@inheritdoc}
86
	 * @see \Ubiquity\views\engine\TemplateEngine::getCode()
87
	 */
88 1
	public function getCode($templateName) {
89 1
		return UFileSystem::load ( $this->twig->load ( $templateName )->getSourceContext ()->getPath () );
90
	}
91
92
	/**
93
	 * Adds a new path in a namespace
94
	 *
95
	 * @param string $path
96
	 *        	The path to add
97
	 * @param string $namespace
98
	 *        	The namespace to use
99
	 */
100
	public function addPath(string $path, string $namespace) {
101
		$this->loader->addPath ( $path, $namespace );
102
	}
103
}
104