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