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