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 |
|
$filePath = ''; |
41
|
1 |
|
if (!file_exists($filePath)) { |
42
|
1 |
|
$filePath = BASE_DIR . $string; |
43
|
|
|
} |
44
|
1 |
|
$filenamePath = AssetsParser::findDomainPath($string, $filePath); |
45
|
|
|
|
46
|
1 |
|
$filePath = self::processAsset($string, $name, $return, $filenamePath); |
47
|
1 |
|
$basePath = Config::getParam('resources.cdn.url', Request::getInstance()->getRootUrl()); |
48
|
1 |
|
$returnPath = empty($name) ? $basePath . '/' . $filePath : $name; |
49
|
1 |
|
return $return ? $returnPath : ''; |
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
|
|
|
* @param array $button |
95
|
|
|
* @throws \Twig_Error_Loader |
96
|
|
|
* @throws \Twig_Error_Runtime |
97
|
|
|
* @throws \Twig_Error_Syntax |
98
|
|
|
*/ |
99
|
|
|
public static function button(array $button) |
100
|
|
|
{ |
101
|
|
|
Template::getInstance()->getTemplateEngine()->display('forms/button.html.twig', array( |
102
|
|
|
'button' => $button, |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $field |
108
|
|
|
* @param StringClass|null $label |
109
|
|
|
* @throws \Twig_Error_Loader |
110
|
|
|
* @throws \Twig_Error_Runtime |
111
|
|
|
* @throws \Twig_Error_Syntax |
112
|
|
|
*/ |
113
|
|
|
public static function widget(array $field, StringClass $label = null) |
114
|
|
|
{ |
115
|
|
|
if (null !== $label) { |
116
|
|
|
$field['label'] = $label; |
117
|
|
|
} |
118
|
|
|
//Limpiamos los campos obligatorios |
119
|
|
|
if (!isset($field['required'])) { |
120
|
|
|
$field['required'] = true; |
121
|
|
|
} |
122
|
|
|
if (isset($field['required']) && (bool)$field['required'] === false) { |
123
|
|
|
unset($field['required']); |
124
|
|
|
} |
125
|
|
|
Template::getInstance()->getTemplateEngine()->display('forms/field.html.twig', array( |
126
|
|
|
'field' => $field, |
127
|
|
|
)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Función que deveulve un formulario en html |
132
|
|
|
* @param Form $form |
133
|
|
|
*/ |
134
|
|
|
public static function form(Form $form) |
135
|
|
|
{ |
136
|
|
|
Template::getInstance()->getTemplateEngine()->display('forms/base.html.twig', array( |
137
|
|
|
'form' => $form, |
138
|
|
|
)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Función que copia un recurso directamente en el DocumentRoot |
143
|
|
|
* @param string $path |
144
|
|
|
* @param string $dest |
145
|
|
|
* @param bool|FALSE $force |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
* @throws ConfigException |
149
|
|
|
*/ |
150
|
1 |
|
public static function resource($path, $dest, $force = false) |
151
|
|
|
{ |
152
|
1 |
|
$debug = Config::getParam('debug'); |
153
|
1 |
|
$domains = Template::getDomains(true); |
154
|
1 |
|
$filenamePath = self::extractPathname($path, $domains); |
155
|
1 |
|
\PSFS\Services\GeneratorService::copyResources($dest, $force, $filenamePath, $debug); |
156
|
1 |
|
return ''; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Método que extrae el pathname para un dominio |
161
|
|
|
* @param string $path |
162
|
|
|
* @param $domains |
163
|
|
|
* |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
1 |
|
private static function extractPathname($path, $domains) |
167
|
|
|
{ |
168
|
1 |
|
$filenamePath = $path; |
169
|
1 |
|
if (!empty($domains) && !file_exists($path)) { |
170
|
1 |
|
foreach ($domains as $domain => $paths) { |
171
|
1 |
|
$domainFilename = str_replace($domain, $paths['public'], $path); |
172
|
1 |
|
if (file_exists($domainFilename)) { |
173
|
1 |
|
$filenamePath = $domainFilename; |
174
|
1 |
|
continue; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
return $filenamePath; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $filenamePath |
185
|
|
|
*/ |
186
|
|
|
private static function processCssLines($filenamePath) |
187
|
|
|
{ |
188
|
|
|
$handle = @fopen($filenamePath, 'r'); |
189
|
|
|
if ($handle) { |
|
|
|
|
190
|
|
|
while (!feof($handle)) { |
191
|
|
|
AssetsParser::extractCssLineResource($handle, $filenamePath); |
192
|
|
|
} |
193
|
|
|
fclose($handle); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Método que copia el contenido de un recurso en su destino correspondiente |
199
|
|
|
* @param string $name |
200
|
|
|
* @param string $filenamePath |
201
|
|
|
* @param string $base |
202
|
|
|
* @param string $filePath |
203
|
|
|
*/ |
204
|
1 |
|
private static function putResourceContent($name, $filenamePath, $base, $filePath) |
205
|
|
|
{ |
206
|
1 |
|
$data = file_get_contents($filenamePath); |
207
|
1 |
|
if (!empty($name)) { |
208
|
|
|
file_put_contents(WEB_DIR . DIRECTORY_SEPARATOR . $name, $data); |
209
|
|
|
}else { |
210
|
1 |
|
file_put_contents($base . $filePath, $data); |
211
|
|
|
} |
212
|
1 |
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Método que procesa un recurso para su copia en el DocumentRoot |
216
|
|
|
* @param string $string |
217
|
|
|
* @param string $name |
218
|
|
|
* @param boolean $return |
219
|
|
|
* @param string $filenamePath |
220
|
|
|
* |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
1 |
|
private static function processAsset($string, $name, $return, $filenamePath) |
224
|
|
|
{ |
225
|
1 |
|
$filePath = $filenamePath; |
226
|
1 |
|
if (file_exists($filenamePath)) { |
227
|
1 |
|
list($base, $htmlBase, $filePath) = AssetsParser::calculateAssetPath($string, $name, $return, $filenamePath); |
228
|
|
|
//Creamos el directorio si no existe |
229
|
1 |
|
GeneratorHelper::createDir($base . $htmlBase); |
230
|
|
|
//Si se ha modificado |
231
|
1 |
|
if (!file_exists($base . $filePath) || filemtime($base . $filePath) < filemtime($filenamePath)) { |
232
|
1 |
|
if ($htmlBase === 'css') { |
233
|
|
|
self::processCssLines($filenamePath); |
234
|
|
|
} |
235
|
1 |
|
self::putResourceContent($name, $filenamePath, $base, $filePath); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
return $filePath; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Template function for get a session var |
244
|
|
|
* @param string $key |
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
1 |
|
public static function session($key) |
248
|
|
|
{ |
249
|
1 |
|
return Security::getInstance()->getSessionKey($key); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Template function that check if exists any flash session var |
254
|
|
|
* @param string $key |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
1 |
|
public static function existsFlash($key = '') |
258
|
|
|
{ |
259
|
1 |
|
return null !== Security::getInstance()->getFlash($key); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Template function that get a flash session var |
264
|
|
|
* @param string $key |
265
|
|
|
* @return mixed |
266
|
|
|
*/ |
267
|
|
|
public static function getFlash($key) |
268
|
|
|
{ |
269
|
|
|
$var = Security::getInstance()->getFlash($key); |
270
|
|
|
Security::getInstance()->setFlash($key, null); |
271
|
|
|
return $var; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
} |