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