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 | 22 | protected function hasThemeResource(&$resource): bool { |
|
75 | 22 | $resource = \str_replace('@activeTheme/', '', $resource, $count); |
|
76 | 22 | 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 | 68 | public function setTheme(string $theme, string $themeFolder = ThemesManager::THEMES_FOLDER): string { |
|
88 | 68 | $root = DDDManager::getActiveViewFolder(); |
|
89 | 68 | $path = $root . $themeFolder . \DS . $theme; |
|
90 | 68 | if ($theme == '') { |
|
91 | $path = $root; |
||
92 | } |
||
93 | 68 | if (!\file_exists($path)) { |
|
94 | throw new ThemesException (sprintf('The path `%s` does not exists!', $path)); |
||
95 | } |
||
96 | 68 | return $path; |
|
97 | } |
||
98 | |||
99 | 70 | protected function addFunctions(): void { |
|
100 | 70 | $safe = ['is_safe' => ['html']]; |
|
101 | 70 | $this->fw = new Framework(); |
|
102 | 70 | $this->addFunction('path', function ($name, $params = [], $absolute = false) { |
|
103 | 2 | return Router::path($name, $params, $absolute); |
|
104 | 70 | }); |
|
105 | |||
106 | 70 | $this->addFunction('url', function ($name, $params = []) { |
|
107 | return Router::url($name, $params); |
||
108 | 70 | }); |
|
109 | |||
110 | 70 | 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 | 70 | 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 | 70 | $this->addFunction('css', function ($resource, $parameters = [], $absolute = false) { |
|
126 | 22 | if ($this->hasThemeResource($resource)) { |
|
127 | 22 | return $this->safeString(AssetsManager::css_($resource, $parameters, $absolute)); |
|
128 | } |
||
129 | return $this->safeString(AssetsManager::css($resource, $parameters, $absolute)); |
||
130 | 70 | }, $safe); |
|
131 | |||
132 | 70 | $this->addFunction('js', function ($resource, $parameters = [], $absolute = false) { |
|
133 | 1 | if ($this->hasThemeResource($resource)) { |
|
134 | 1 | return $this->safeString(AssetsManager::js_($resource, $parameters, $absolute)); |
|
135 | } |
||
136 | return $this->safeString(AssetsManager::js($resource, $parameters, $absolute)); |
||
137 | 70 | }, $safe); |
|
138 | |||
139 | 70 | $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 | 70 | }, $safe); |
|
145 | } |
||
146 | |||
147 | 22 | protected function safeString(string $str) { |
|
148 | 22 | 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); |
||
0 ignored issues
–
show
|
|||
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.