Test Failed
Push — master ( ac669c...c918ec )
by Jean-Christophe
08:37
created

Twig::setTheme()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 12
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;
0 ignored issues
show
Bug introduced by
The type Ubiquity\assets\AssetsManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 33
 */
31 33
class Twig extends TemplateEngine {
32 33
	private $twig;
33 33
	private $loader;
34
35 33
	public function __construct($options = array()) {
36
		$loader = new FilesystemLoader ( \ROOT . \DS . "views" . \DS );
37
		$loader->addPath ( implode ( \DS, [ Startup::getFrameworkDir (),"..","core","views" ] ) . \DS, "framework" );
38 2
		$this->loader = $loader;
39 33
40 33
		$this->twig = new Environment ( $loader, $options );
41
		if (isset ( $options ["cache"] ) && $options ["cache"] === true) {
42
			$options ["cache"] = CacheManager::getCacheSubDirectory ( "views" );
43 33
		}
44 33
		if (isset ( $options ["activeTheme"] )) {
45
			ThemesManager::setActiveThemeFromTwig ( $options ["activeTheme"] );
46
			self::setTheme ( $options ["activeTheme"], ThemesManager::THEMES_FOLDER );
0 ignored issues
show
Bug Best Practice introduced by
The method Ubiquity\views\engine\Twig::setTheme() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

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