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