|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PSFS\base\extension; |
|
4
|
|
|
|
|
5
|
|
|
use PSFS\base\Cache; |
|
6
|
|
|
use PSFS\base\config\Config; |
|
7
|
|
|
use PSFS\base\exception\GeneratorException; |
|
8
|
|
|
use PSFS\base\Logger; |
|
9
|
|
|
use PSFS\base\Request; |
|
10
|
|
|
use PSFS\base\Router; |
|
11
|
|
|
use PSFS\base\Security; |
|
12
|
|
|
use PSFS\base\Template; |
|
13
|
|
|
use PSFS\base\types\Form; |
|
14
|
|
|
use PSFS\base\types\helpers\AssetsHelper; |
|
15
|
|
|
use PSFS\base\types\helpers\GeneratorHelper; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class TemplateFunctions |
|
19
|
|
|
* @package PSFS\base\extension |
|
20
|
|
|
*/ |
|
21
|
|
|
class TemplateFunctions |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
const ASSETS_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::asset'; |
|
25
|
|
|
const ROUTE_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::route'; |
|
26
|
|
|
const CONFIG_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::config'; |
|
27
|
|
|
const BUTTON_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::button'; |
|
28
|
|
|
const WIDGET_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::widget'; |
|
29
|
|
|
const FORM_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::form'; |
|
30
|
|
|
const RESOURCE_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::resource'; |
|
31
|
|
|
const SESSION_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::session'; |
|
32
|
|
|
const EXISTS_FLASH_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::existsFlash'; |
|
33
|
|
|
const GET_FLASH_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::getFlash'; |
|
34
|
|
|
const GET_QUERY_FUNCTION = '\\PSFS\\base\\extension\\TemplateFunctions::query'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Función que copia los recursos de las carpetas Public al DocumentRoot |
|
38
|
|
|
* @param string $string |
|
39
|
|
|
* @param string|null $name |
|
40
|
|
|
* @param bool $return |
|
41
|
|
|
* @return string|null |
|
42
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public static function asset(string $string, string $name = null, bool $return = true): ?string |
|
45
|
|
|
{ |
|
46
|
|
|
|
|
47
|
1 |
|
$filePath = ''; |
|
48
|
1 |
|
if (!file_exists($filePath)) { |
|
49
|
1 |
|
$filePath = BASE_DIR . $string; |
|
50
|
|
|
} |
|
51
|
1 |
|
$filenamePath = AssetsHelper::findDomainPath($string, $filePath); |
|
52
|
|
|
|
|
53
|
1 |
|
$filePath = self::processAsset($string, $name, $return, $filenamePath); |
|
|
|
|
|
|
54
|
1 |
|
$basePath = Config::getParam('resources.cdn.url', Request::getInstance()->getRootUrl()); |
|
55
|
1 |
|
$returnPath = empty($name) ? $basePath . '/' . $filePath : $name; |
|
56
|
1 |
|
return $return ? $returnPath : ''; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Función que devuelve una url correspondiente a una ruta |
|
61
|
|
|
* @param string $path |
|
62
|
|
|
* @param bool $absolute |
|
63
|
|
|
* @param array $params |
|
64
|
|
|
* |
|
65
|
|
|
* @return string|null |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public static function route(string $path = '', bool $absolute = false, array $params = []): ?string |
|
68
|
|
|
{ |
|
69
|
1 |
|
$router = Router::getInstance(); |
|
70
|
|
|
try { |
|
71
|
1 |
|
return $router->getRoute($path, $absolute, $params); |
|
72
|
|
|
} catch (\Exception $e) { |
|
73
|
|
|
Logger::log($e->getMessage()); |
|
74
|
|
|
return $router->getRoute('', $absolute, $params); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Función que devuelve un parámetro de la configuración |
|
80
|
|
|
* @param string $param |
|
81
|
|
|
* @param string $default |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed|null |
|
84
|
|
|
*/ |
|
85
|
3 |
|
public static function config(string $param, string $default = ''): mixed |
|
86
|
|
|
{ |
|
87
|
3 |
|
return Config::getInstance()->get($param) ?: $default; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Función que devuelve un query string |
|
92
|
|
|
* @param string $query |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function query(string $query): string |
|
97
|
|
|
{ |
|
98
|
|
|
return Request::getInstance()->getQuery($query); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param array $button |
|
103
|
|
|
* @throws \Twig\Error\LoaderError |
|
104
|
|
|
* @throws \Twig\Error\RuntimeError |
|
105
|
|
|
* @throws \Twig\Error\SyntaxError |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function button(array $button): void |
|
108
|
|
|
{ |
|
109
|
|
|
Template::getInstance()->getTemplateEngine()->display('forms/button.html.twig', array( |
|
110
|
|
|
'button' => $button, |
|
111
|
|
|
)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param array $field |
|
116
|
|
|
* @param string|null $label |
|
117
|
|
|
* @throws \Twig\Error\LoaderError |
|
118
|
|
|
* @throws \Twig\Error\RuntimeError |
|
119
|
|
|
* @throws \Twig\Error\SyntaxError |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function widget(array $field, string $label = null): void |
|
122
|
|
|
{ |
|
123
|
|
|
if (null !== $label) { |
|
124
|
|
|
$field['label'] = $label; |
|
125
|
|
|
} |
|
126
|
|
|
//Limpiamos los campos obligatorios |
|
127
|
|
|
if (!isset($field['required'])) { |
|
128
|
|
|
$field['required'] = true; |
|
129
|
|
|
} |
|
130
|
|
|
if (isset($field['required']) && (bool)$field['required'] === false) { |
|
131
|
|
|
unset($field['required']); |
|
132
|
|
|
} |
|
133
|
|
|
Template::getInstance()->getTemplateEngine()->display('forms/field.html.twig', array( |
|
134
|
|
|
'field' => $field, |
|
135
|
|
|
)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Función que deveulve un formulario en html |
|
140
|
|
|
* @param Form $form |
|
141
|
|
|
* @throws \Twig\Error\LoaderError |
|
142
|
|
|
* @throws \Twig\Error\RuntimeError |
|
143
|
|
|
* @throws \Twig\Error\SyntaxError |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function form(Form $form): void |
|
146
|
|
|
{ |
|
147
|
|
|
Template::getInstance()->getTemplateEngine()->display('forms/base.html.twig', array( |
|
148
|
|
|
'form' => $form, |
|
149
|
|
|
)); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Función que copia un recurso directamente en el DocumentRoot |
|
154
|
|
|
* @param string $path |
|
155
|
|
|
* @param string $dest |
|
156
|
|
|
* @param bool|bool $force |
|
157
|
|
|
* |
|
158
|
|
|
* @return string |
|
159
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public static function resource(string $path, string $dest, bool $force = false): string |
|
162
|
|
|
{ |
|
163
|
1 |
|
$debug = Config::getParam('debug'); |
|
164
|
1 |
|
$domains = Template::getDomains(true); |
|
165
|
1 |
|
$filenamePath = self::extractPathname($path, $domains); |
|
166
|
|
|
// Check if resources has been copied to public folders |
|
167
|
1 |
|
if (!$debug) { |
|
168
|
1 |
|
$cacheFilename = Config::getParam('cache.var', '__initial__') . '.file.cache'; |
|
169
|
1 |
|
$cachedFiles = Cache::getInstance()->readFromCache($cacheFilename, |
|
170
|
1 |
|
1, fn() => [], Cache::JSON, true) ?: []; |
|
171
|
|
|
// Force the resource copy |
|
172
|
1 |
|
if (!in_array($filenamePath, $cachedFiles) || $force) { |
|
173
|
1 |
|
$force = true; |
|
174
|
1 |
|
$cachedFiles[] = $filenamePath; |
|
175
|
1 |
|
Cache::getInstance()->storeData($cacheFilename, $cachedFiles, Cache::JSON); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
1 |
|
GeneratorHelper::copyResources($dest, $force, $filenamePath, $debug); |
|
|
|
|
|
|
179
|
1 |
|
return ''; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Método que extrae el pathname para un dominio |
|
184
|
|
|
* @param string $path |
|
185
|
|
|
* @param $domains |
|
186
|
|
|
* |
|
187
|
|
|
* @return string|array |
|
188
|
|
|
*/ |
|
189
|
1 |
|
private static function extractPathname(string $path, $domains): string|array |
|
190
|
|
|
{ |
|
191
|
1 |
|
$filenamePath = $path; |
|
192
|
1 |
|
if (!empty($domains) && !file_exists($path)) { |
|
193
|
1 |
|
foreach ($domains as $domain => $paths) { |
|
194
|
1 |
|
$domainFilename = str_replace($domain, $paths['public'], $path); |
|
195
|
1 |
|
if (file_exists($domainFilename)) { |
|
196
|
1 |
|
$filenamePath = $domainFilename; |
|
197
|
1 |
|
break; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
return $filenamePath; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param $filenamePath |
|
208
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
|
209
|
|
|
*/ |
|
210
|
|
|
private static function processCssLines($filenamePath): void |
|
211
|
|
|
{ |
|
212
|
|
|
$handle = @fopen($filenamePath, 'r'); |
|
213
|
|
|
if ($handle) { |
|
|
|
|
|
|
214
|
|
|
while (!feof($handle)) { |
|
215
|
|
|
AssetsParser::extractCssLineResource($handle, $filenamePath); |
|
216
|
|
|
} |
|
217
|
|
|
fclose($handle); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Método que copia el contenido de un recurso en su destino correspondiente |
|
223
|
|
|
* @param string|null $name |
|
224
|
|
|
* @param string $filenamePath |
|
225
|
|
|
* @param string $base |
|
226
|
|
|
* @param string $filePath |
|
227
|
|
|
*/ |
|
228
|
1 |
|
private static function putResourceContent(string|null $name, string $filenamePath, string $base, string $filePath): void |
|
229
|
|
|
{ |
|
230
|
1 |
|
$data = file_get_contents($filenamePath); |
|
231
|
1 |
|
if (!empty($name)) { |
|
232
|
|
|
file_put_contents(WEB_DIR . DIRECTORY_SEPARATOR . $name, $data); |
|
233
|
|
|
} else { |
|
234
|
1 |
|
file_put_contents($base . $filePath, $data); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Método que procesa un recurso para su copia en el DocumentRoot |
|
240
|
|
|
* @param string $string |
|
241
|
|
|
* @param string|null $name |
|
242
|
|
|
* @param boolean $return |
|
243
|
|
|
* @param string $filenamePath |
|
244
|
|
|
* @return string |
|
245
|
|
|
* @throws GeneratorException |
|
246
|
|
|
*/ |
|
247
|
1 |
|
private static function processAsset(string $string, string|null $name = null, bool $return = true, string $filenamePath = ''): string |
|
248
|
|
|
{ |
|
249
|
1 |
|
$filePath = $filenamePath; |
|
250
|
1 |
|
if (file_exists($filenamePath)) { |
|
251
|
1 |
|
list($base, $htmlBase, $filePath) = AssetsHelper::calculateAssetPath($string, $name, $return, $filenamePath); |
|
252
|
|
|
//Creamos el directorio si no existe |
|
253
|
1 |
|
GeneratorHelper::createDir($base . $htmlBase); |
|
254
|
|
|
//Si se ha modificado |
|
255
|
1 |
|
if (!file_exists($base . $filePath) || filemtime($base . $filePath) < filemtime($filenamePath)) { |
|
256
|
1 |
|
if ($htmlBase === 'css') { |
|
257
|
|
|
self::processCssLines($filenamePath); |
|
258
|
|
|
} |
|
259
|
1 |
|
self::putResourceContent($name, $filenamePath, $base, $filePath); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
1 |
|
return $filePath; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Template function for get a session var |
|
268
|
|
|
* @param string $key |
|
269
|
|
|
* @return mixed |
|
270
|
|
|
*/ |
|
271
|
1 |
|
public static function session(string $key): mixed |
|
272
|
|
|
{ |
|
273
|
1 |
|
return Security::getInstance()->getSessionKey($key); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Template function that check if exists any flash session var |
|
278
|
|
|
* @param string $key |
|
279
|
|
|
* @return bool |
|
280
|
|
|
*/ |
|
281
|
1 |
|
public static function existsFlash(string $key = ''): bool |
|
282
|
|
|
{ |
|
283
|
1 |
|
return null !== Security::getInstance()->getFlash($key); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Template function that get a flash session var |
|
288
|
|
|
* @param string $key |
|
289
|
|
|
* @return mixed |
|
290
|
|
|
*/ |
|
291
|
|
|
public static function getFlash(string $key): mixed |
|
292
|
|
|
{ |
|
293
|
|
|
$var = Security::getInstance()->getFlash($key); |
|
294
|
|
|
Security::getInstance()->setFlash($key, null); |
|
295
|
|
|
return $var; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
} |
|
299
|
|
|
|