Completed
Push — master ( 0db593...5f0535 )
by Jean-Christophe
01:58
created

Twig   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 39
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 8 2
A __construct() 0 17 3
A getBlockNames() 0 3 1
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
10
class Twig extends TemplateEngine {
11
	private $twig;
12
13
	public function __construct($options=array()) {
14
		$loader=new \Twig_Loader_Filesystem(ROOT . DS . "views/");
15
		$loader->addPath(implode(DS,[Startup::getFrameworkDir(),"..","core","views"]).DS,"framework");
16
		if(isset($options["cache"]) && $options["cache"]===true)
17
			$options["cache"]=ROOT.DS.CacheManager::getCacheDirectory().DS."views/";
18
		$this->twig=new \Twig_Environment($loader, $options);
19
20
		$function=new \Twig_SimpleFunction('path', function ($name,$params=[],$absolute=false) {
21
			return Router::path($name,$params,$absolute);
22
		});
23
		$this->twig->addFunction($function);
24
		$function=new \Twig_SimpleFunction('url', function ($name,$params) {
25
			return Router::url($name,$params);
26
		});
27
		$this->twig->addFunction($function);
28
		$this->twig->addGlobal("app", new Framework());
29
	}
30
31
	/*
32
	 * (non-PHPdoc)
33
	 * @see TemplateEngine::render()
34
	 */
35
	public function render($viewName, $pData, $asString) {
36
		$pData["config"]=Startup::getConfig();
37
		$render=$this->twig->render($viewName, $pData);
38
		if ($asString) {
39
			return $render;
40
		} else
41
			echo $render;
42
	}
43
44
	public function getBlockNames($templateName) {
45
		return $this->twig->load($templateName)->getBlockNames();
46
	}
47
48
}
49