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