Passed
Push — master ( 5bd0b7...fe2b7e )
by Jean-Christophe
10:05 queued 48s
created

Twig::__construct()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 67
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 7.7109

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 47
c 2
b 0
f 1
dl 0
loc 67
ccs 31
cts 41
cp 0.7561
rs 8.223
cc 7
nc 8
nop 1
crap 7.7109

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