1
|
|
|
<?php |
2
|
|
|
namespace PSFS\base; |
3
|
|
|
|
4
|
|
|
use PSFS\base\config\Config; |
5
|
|
|
use PSFS\base\exception\ConfigException; |
6
|
|
|
use PSFS\base\types\SingletonTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Cache |
10
|
|
|
* @package PSFS\base |
11
|
|
|
* Gestión de los ficheros de cache |
12
|
|
|
*/ |
13
|
|
|
class Cache { |
14
|
|
|
|
15
|
|
|
const JSON = 1; |
16
|
|
|
const TEXT = 2; |
17
|
|
|
const ZIP = 3; |
18
|
|
|
|
19
|
|
|
use SingletonTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Método que guarda un text en un fichero |
23
|
|
|
* @param string $data |
24
|
|
|
* @param string $path |
25
|
|
|
* @param boolean $absolute |
26
|
|
|
* @throws ConfigException |
27
|
|
|
*/ |
28
|
4 |
|
private function saveTextToFile($data, $path, $absolute = false) { |
29
|
4 |
|
$absolutePath = ($absolute) ? $path : CACHE_DIR.DIRECTORY_SEPARATOR.$path; |
30
|
4 |
|
$filename = basename($absolutePath); |
31
|
4 |
|
Config::createDir(str_replace($filename, "", $absolutePath)); |
32
|
4 |
|
if (false === file_put_contents($absolutePath, $data)) { |
33
|
|
|
throw new ConfigException(_('No se tienen los permisos suficientes para escribir en el fichero ').$absolutePath); |
|
|
|
|
34
|
|
|
} |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Método que extrae el texto de un fichero |
39
|
|
|
* @param string $path |
40
|
|
|
* @param int $transform |
41
|
|
|
* @param boolean $absolute |
42
|
|
|
* @return array|string |
43
|
|
|
*/ |
44
|
2 |
|
public function getDataFromFile($path, $transform = Cache::TEXT, $absolute = false) { |
45
|
2 |
|
$data = null; |
46
|
2 |
|
$absolutePath = ($absolute) ? $path : CACHE_DIR.DIRECTORY_SEPARATOR.$path; |
47
|
2 |
|
if (file_exists($absolutePath)) { |
48
|
1 |
|
$data = file_get_contents($absolutePath); |
49
|
1 |
|
} |
50
|
2 |
|
return Cache::extractDataWithFormat($data, $transform); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Método que verifica si un fichero tiene la cache expirada |
55
|
|
|
* @param string $path |
56
|
|
|
* @param int $expires |
57
|
|
|
* @param boolean $absolute |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
private function hasExpiredCache($path, $expires = 300, $absolute = false) { |
61
|
|
|
$absolutePath = ($absolute) ? $path : CACHE_DIR.DIRECTORY_SEPARATOR.$path; |
62
|
|
|
$lasModificationDate = filemtime($absolutePath); |
63
|
|
|
return ($lasModificationDate + $expires <= time()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Método que transforma los datos de salida |
68
|
|
|
* @param string $data |
69
|
|
|
* @param int $transform |
70
|
|
|
* @return array|string |
71
|
|
|
*/ |
72
|
2 |
|
public static function extractDataWithFormat($data, $transform = Cache::TEXT) { |
73
|
|
|
switch ($transform) { |
74
|
2 |
|
case Cache::JSON: |
75
|
2 |
|
$data = json_decode($data, true); |
76
|
2 |
|
break; |
77
|
|
|
case Cache::ZIP: |
78
|
|
|
// TODO implementar |
79
|
|
|
case Cache::TEXT: |
80
|
|
|
default: |
81
|
|
|
//do nothing |
82
|
|
|
break; |
83
|
|
|
} |
84
|
2 |
|
return $data; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Método que transforma los datos de entrada del fichero |
89
|
|
|
* @param string $data |
90
|
|
|
* @param int $transform |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
4 |
|
public static function transformData($data, $transform = Cache::TEXT) { |
94
|
|
|
switch ($transform) { |
95
|
4 |
|
case Cache::JSON: |
96
|
4 |
|
$data = json_encode($data, JSON_PRETTY_PRINT); |
97
|
4 |
|
break; |
98
|
1 |
|
case Cache::ZIP: |
99
|
|
|
// TODO implementar |
100
|
1 |
|
case Cache::TEXT: |
101
|
1 |
|
default: |
102
|
|
|
//do nothing |
103
|
1 |
|
break; |
104
|
|
|
} |
105
|
4 |
|
return $data; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Método que guarda en fichero los datos pasados |
110
|
|
|
* @param $path |
111
|
|
|
* @param $data |
112
|
|
|
* @param int $transform |
113
|
|
|
* @param boolean $absolutePath |
114
|
|
|
*/ |
115
|
4 |
|
public function storeData($path, $data, $transform = Cache::TEXT, $absolutePath = false) { |
116
|
4 |
|
$data = Cache::transformData($data, $transform); |
117
|
4 |
|
$this->saveTextToFile($data, $path, $absolutePath); |
118
|
4 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Método que verifica si tiene que leer o no un fichero de cache |
122
|
|
|
* @param string $path |
123
|
|
|
* @param int $expires |
124
|
|
|
* @param callable $function |
125
|
|
|
* @param int $transform |
126
|
|
|
* @return string|null |
127
|
|
|
*/ |
128
|
|
|
public function readFromCache($path, $expires = 300, callable $function, $transform = Cache::TEXT) { |
129
|
|
|
$data = null; |
130
|
|
|
if (file_exists(CACHE_DIR.DIRECTORY_SEPARATOR.$path)) { |
131
|
|
|
if (null !== $function && $this->hasExpiredCache($path, $expires)) { |
132
|
|
|
$data = call_user_func($function); |
133
|
|
|
$this->storeData($path, $data, $transform); |
134
|
|
|
} else { |
135
|
|
|
$data = $this->getDataFromFile($path, $transform); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
return $data; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Método estático que revisa si se necesita cachear la respuesta de un servicio o no |
143
|
|
|
* @return integer |
144
|
|
|
*/ |
145
|
|
|
public static function needCache() { |
146
|
|
|
$action = Security::getInstance()->getSessionKey("__CACHE__"); |
147
|
|
|
$needCache = false; |
148
|
|
|
if (null !== $action && array_key_exists("cache", $action) && $action["cache"] > 0) { |
149
|
|
|
$needCache = $action["cache"]; |
150
|
|
|
} |
151
|
|
|
return $needCache; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Método que construye un hash para almacenar la cache |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getRequestCacheHash() { |
159
|
|
|
$hash = ""; |
160
|
|
|
$action = Security::getInstance()->getSessionKey("__CACHE__"); |
161
|
|
|
if (null !== $action && $action["cache"] > 0) { |
162
|
|
|
$hash = $action["http"]." ".$action["slug"]; |
163
|
|
|
} |
164
|
|
|
return sha1($hash); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.