1 | <?php |
||
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) { |
|
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) { |
||
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) { |
|
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) { |
|
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) { |
|
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) { |
||
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() { |
||
153 | |||
154 | /** |
||
155 | * Método que construye un hash para almacenar la cache |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getRequestCacheHash() { |
||
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.