Passed
Push — master ( ed5274...d4fa57 )
by Jean-Christophe
10:23
created

Twig::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 4.1842

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 51
ccs 24
cts 31
cp 0.7742
rs 9.344
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.1842

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
			return AssetsManager::css_ ( $resource, $parameters, $absolute );
62 38
		}, true );
63
64
		$this->addFunction ( 'css', function ($resource, $parameters = [], $absolute = false) {
65 4
			return AssetsManager::css ( $resource, $parameters, $absolute );
66 38
		}, true );
67
68
		$this->addFunction ( 'js', function ($resource, $parameters = [], $absolute = false) {
69 4
			return AssetsManager::js ( $resource, $parameters, $absolute );
70 38
		}, true );
71
72
		$this->addFunction ( 'js_', function ($resource, $parameters = [], $absolute = false) {
73
			return AssetsManager::js_ ( $resource, $parameters, $absolute );
74 38
		}, 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 38
		}, [ 'needs_context' => true ] );
80
81
		$test = new TwigTest ( 'instanceOf', function ($var, $class) {
82
			return $var instanceof $class;
83 38
		} );
84 38
		$this->twig->addTest ( $test );
85 38
		$this->twig->addGlobal ( "app", new Framework () );
86 38
	}
87
88 38
	protected function addFunction($name, $callback, $safe = false) {
89 38
		$options = ($safe) ? [ 'is_safe' => [ 'html' ] ] : [ ];
90 38
		$this->twig->addFunction ( new TwigFunction ( $name, $callback, $options ) );
91 38
	}
92
93
	/*
94
	 * (non-PHPdoc)
95
	 * @see TemplateEngine::render()
96
	 */
97 22
	public function render($viewName, $pData, $asString) {
98 22
		$pData ["config"] = Startup::getConfig ();
99 22
		EventsManager::trigger ( ViewEvents::BEFORE_RENDER, $viewName, $pData );
100 22
		$render = $this->twig->render ( $viewName, $pData );
101 22
		EventsManager::trigger ( ViewEvents::AFTER_RENDER, $render, $viewName, $pData );
102 22
		if ($asString) {
103 1
			return $render;
104
		} else {
105 22
			echo $render;
106
		}
107 22
	}
108
109
	/**
110
	 *
111
	 * {@inheritdoc}
112
	 * @see \Ubiquity\views\engine\TemplateEngine::getBlockNames()
113
	 */
114 2
	public function getBlockNames($templateName) {
115 2
		return $this->twig->load ( $templateName )->getBlockNames ();
116
	}
117
118
	/**
119
	 *
120
	 * {@inheritdoc}
121
	 * @see \Ubiquity\views\engine\TemplateEngine::getCode()
122
	 */
123 1
	public function getCode($templateName) {
124 1
		return UFileSystem::load ( $this->twig->load ( $templateName )->getSourceContext ()->getPath () );
125
	}
126
127
	/**
128
	 * Adds a new path in a namespace
129
	 *
130
	 * @param string $path The path to add
131
	 * @param string $namespace The namespace to use
132
	 */
133
	public function addPath(string $path, string $namespace) {
134
		$this->loader->addPath ( $path, $namespace );
135
	}
136
137
	/**
138
	 * Defines the activeTheme.
139
	 * **activeTheme** namespace is @activeTheme
140
	 *
141
	 * @param string $theme
142
	 * @param string $themeFolder
143
	 * @throws ThemesException
144
	 */
145 37
	public function setTheme($theme, $themeFolder = ThemesManager::THEMES_FOLDER) {
146 37
		$path = \ROOT . \DS . 'views' . \DS . $themeFolder . \DS . $theme;
147 37
		if ($theme == '') {
148
			$path = \ROOT . \DS . 'views';
149
		}
150 37
		if (file_exists ( $path )) {
151 37
			$this->loader->setPaths ( [ $path ], "activeTheme" );
152
		} else {
153
			throw new ThemesException ( sprintf ( 'The path `%s` does not exists!', $path ) );
154
		}
155
	}
156
}