1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\views\engine; |
4
|
|
|
|
5
|
|
|
use Ubiquity\assets\AssetsManager; |
6
|
|
|
use Ubiquity\controllers\Router; |
7
|
|
|
use Ubiquity\core\Framework; |
8
|
|
|
use Ubiquity\domains\DDDManager; |
9
|
|
|
use Ubiquity\exceptions\ThemesException; |
10
|
|
|
use Ubiquity\themes\ThemesManager; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Abstract template engine. |
14
|
|
|
* Ubiquity\views\engine$TemplateEngine |
15
|
|
|
* This class is part of Ubiquity |
16
|
|
|
* |
17
|
|
|
* @author jcheron <[email protected]> |
18
|
|
|
* @version 1.1.0 |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
abstract class TemplateEngine { |
22
|
|
|
|
23
|
|
|
protected Framework $fw; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Renders a view. |
27
|
|
|
* |
28
|
|
|
* @param string $viewName |
29
|
|
|
* @param array|null $pData |
30
|
|
|
* @param boolean $asString |
31
|
|
|
*/ |
32
|
|
|
abstract public function render(string $viewName, ?array $pData = [], bool $asString = false); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the defined block names. |
36
|
|
|
* |
37
|
|
|
* @param string $templateName |
38
|
|
|
*/ |
39
|
|
|
abstract public function getBlockNames(string $templateName): array; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the source code of the template |
43
|
|
|
* |
44
|
|
|
* @param string $templateName |
45
|
|
|
*/ |
46
|
|
|
abstract public function getCode(string $templateName): string; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $name |
50
|
|
|
* @param $callback |
51
|
|
|
* @param array $options |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
abstract public function addFunction(string $name, $callback, array $options = []): void; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $name |
58
|
|
|
* @param $callback |
59
|
|
|
* @param array $options |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
abstract protected function addFilter(string $name, $callback, array $options = []): void; |
63
|
|
|
|
64
|
|
|
abstract protected function addExtension($extension): void; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks if we have the source code of a template, given its name. |
68
|
|
|
* |
69
|
|
|
* @param string $name |
70
|
|
|
* @return boolean |
71
|
|
|
*/ |
72
|
|
|
abstract public function exists(string $name): bool; |
73
|
|
|
|
74
|
|
|
protected function hasThemeResource(&$resource): bool { |
75
|
|
|
$resource = \str_replace('@activeTheme/', '', $resource, $count); |
76
|
|
|
return $count > 0; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Defines the activeTheme. |
81
|
|
|
* **activeTheme** namespace is @activeTheme |
82
|
|
|
* |
83
|
|
|
* @param string $theme |
84
|
|
|
* @param string $themeFolder |
85
|
|
|
* @throws ThemesException |
86
|
|
|
*/ |
87
|
|
|
public function setTheme(string $theme, string $themeFolder = ThemesManager::THEMES_FOLDER): string { |
88
|
|
|
$root = DDDManager::getActiveViewFolder(); |
89
|
|
|
$path = $root . $themeFolder . \DS . $theme; |
90
|
|
|
if ($theme == '') { |
91
|
|
|
$path = $root; |
92
|
|
|
} |
93
|
|
|
if (!\file_exists($path)) { |
94
|
|
|
throw new ThemesException (sprintf('The path `%s` does not exists!', $path)); |
95
|
|
|
} |
96
|
|
|
return $path; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function addFunctions(): void { |
100
|
|
|
$safe = ['is_safe' => ['html']]; |
101
|
|
|
$this->fw = new Framework(); |
102
|
|
|
$this->addFunction('path', function ($name, $params = [], $absolute = false) { |
103
|
|
|
return Router::path($name, $params, $absolute); |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
$this->addFunction('url', function ($name, $params = []) { |
107
|
|
|
return Router::url($name, $params); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
if (\class_exists('\\Ubiquity\\security\\csrf\\UCsrfHttp')) { |
111
|
|
|
$this->addFunction('csrfMeta', function ($name) { |
112
|
|
|
return \Ubiquity\security\csrf\UCsrfHttp::getTokenMeta($name); |
113
|
|
|
}, $safe); |
114
|
|
|
$this->addFunction('csrf', function ($name) { |
115
|
|
|
return \Ubiquity\security\csrf\UCsrfHttp::getTokenField($name); |
116
|
|
|
}, $safe); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (\class_exists('\\Ubiquity\security\\acl\\AclManager')) { |
120
|
|
|
$this->addFunction('isAllowedRoute', function ($role, $routeName) { |
121
|
|
|
return \Ubiquity\security\acl\AclManager::isAllowedRoute($role, $routeName); |
122
|
|
|
}, []); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->addFunction('css', function ($resource, $parameters = [], $absolute = false) { |
126
|
|
|
if ($this->hasThemeResource($resource)) { |
127
|
|
|
return $this->safeString(AssetsManager::css_($resource, $parameters, $absolute)); |
128
|
|
|
} |
129
|
|
|
return $this->safeString(AssetsManager::css($resource, $parameters, $absolute)); |
130
|
|
|
}, $safe); |
131
|
|
|
|
132
|
|
|
$this->addFunction('js', function ($resource, $parameters = [], $absolute = false) { |
133
|
|
|
if ($this->hasThemeResource($resource)) { |
134
|
|
|
return $this->safeString(AssetsManager::js_($resource, $parameters, $absolute)); |
135
|
|
|
} |
136
|
|
|
return $this->safeString(AssetsManager::js($resource, $parameters, $absolute)); |
137
|
|
|
}, $safe); |
138
|
|
|
|
139
|
|
|
$this->addFunction('img', function ($resource, $parameters = [], $absolute = false) { |
140
|
|
|
if ($this->hasThemeResource($resource)) { |
141
|
|
|
return $this->safeString(AssetsManager::img_($resource, $parameters, $absolute)); |
142
|
|
|
} |
143
|
|
|
return $this->safeString(AssetsManager::img($resource, $parameters, $absolute)); |
144
|
|
|
}, $safe); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
protected function safeString(string $str) { |
148
|
|
|
return $str; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
abstract public function getGenerator(): ?TemplateGenerator; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Generates the source for this engine from a twig model template. |
155
|
|
|
* @param string $templateName |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function generateTemplateSourceFromFile(string $templateName): string { |
159
|
|
|
$result = $this->getCode($templateName); |
160
|
|
|
return $this->generateTemplateSource($result); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function generateTemplateSource(string $source): string { |
164
|
|
|
$gen = $this->getGenerator(); |
165
|
|
|
if ($gen != null) { |
166
|
|
|
return $gen->parseFromTwig($source); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
return $source; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.