1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the MIT license. For more information, see |
18
|
|
|
* @category |
19
|
|
|
* @package sistema/ayudantes |
20
|
|
|
* @copyright Copyright (c) 2006 - 2014 webcol.net (http://www.webcol.net/calima) |
21
|
|
|
* @license https://github.com/webcol/Calima/blob/master/LICENSE MIT |
22
|
|
|
* @version ##BETA 1.0##, ##2014 - 2015## |
23
|
|
|
* <http://www.calimaframework.com>. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Sistema\Ayudantes; |
27
|
|
|
|
28
|
|
|
class CFPHPCache { |
29
|
|
|
/** |
30
|
|
|
* Configuracion |
31
|
|
|
* |
32
|
|
|
* @access public |
33
|
|
|
*/ |
34
|
|
|
public static $configuracion = array( |
35
|
|
|
'cache_dir' => 'cache', |
36
|
|
|
// por defecto el tiempo expira en *minutos* |
37
|
|
|
'expires' => 180, |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
public static function configurar($clave, $valor = null) { |
41
|
|
|
if( is_array($clave) ) { |
42
|
|
|
foreach ($clave as $config_name => $config_value) { |
43
|
|
|
self::$configuracion[$config_name] = $config_value; |
44
|
|
|
} |
45
|
|
|
} else { |
46
|
|
|
self::$configuracion[$clave] = $valor; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
/** |
50
|
|
|
* obtiene la ruta del archivo asociado a la clave. |
51
|
|
|
* |
52
|
|
|
* @access public |
53
|
|
|
* @param string $clave |
54
|
|
|
* @return string el nombre del archivo php |
55
|
|
|
*/ |
56
|
|
|
public static function obtenerRuta($clave) { |
57
|
|
|
return static::$configuracion['cache_path'] . '/' . md5($clave) . '.php'; |
58
|
|
|
} |
59
|
|
|
/** |
60
|
|
|
* obtiene los datos asociados a la clave |
61
|
|
|
* |
62
|
|
|
* @access public |
63
|
|
|
* @param string $clave |
64
|
|
|
*/ |
65
|
|
|
public static function obtener($clave, $raw = false, $tiempo_personalizado = null) { |
66
|
|
|
if( ! self::file_expired($archivo = self::obtenerRuta($clave), $tiempo_personalizado)) { |
67
|
|
|
$contenido = file_get_contents($archivo); |
68
|
|
|
return $raw ? $contenido : unserialize($contenido); |
69
|
|
|
} |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
/** |
73
|
|
|
* Envia el contenido dentro de la cache |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
* @param string $clave |
77
|
|
|
* @param mixed $content the the content you want to store |
|
|
|
|
78
|
|
|
* @param bool $raw whether if you want to store raw data or not. If it is true, $content *must* be a string |
79
|
|
|
* It can be useful for static html caching. |
80
|
|
|
* @return bool whether if the operation was successful or not |
81
|
|
|
*/ |
82
|
|
|
public static function enviar($clave, $contenido, $raw = false) { |
83
|
|
|
return @file_put_contents(self::obtenerRuta($clave), $raw ? $contenido : serialize($contenido)) !== false; |
84
|
|
|
} |
85
|
|
|
/** |
86
|
|
|
* Delete data from cache |
87
|
|
|
* |
88
|
|
|
* @access public |
89
|
|
|
* @param string $clave |
90
|
|
|
* @return bool true if the data was removed successfully |
91
|
|
|
*/ |
92
|
|
|
public static function eliminar($clave) { |
93
|
|
|
if( ! file_exists($archivo = self::obtenerRuta($clave)) ) { |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
return @unlink($archivo); |
97
|
|
|
} |
98
|
|
|
/** |
99
|
|
|
* limpia la cache |
100
|
|
|
* |
101
|
|
|
* @access public |
102
|
|
|
* @return bool always true |
103
|
|
|
*/ |
104
|
|
|
public static function limpiar() { |
105
|
|
|
$cache_files = glob(self::$configuracion['cache_path'] . '/*.php', GLOB_NOSORT); |
106
|
|
|
foreach ($cache_files as $archivo) { |
107
|
|
|
@unlink($archivo); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
/** |
112
|
|
|
* Check if a file has expired or not. |
113
|
|
|
* |
114
|
|
|
* @access public |
115
|
|
|
* @param $archivo the rout to the file |
116
|
|
|
* @param int $fecha the number of minutes it was set to expire |
117
|
|
|
* @return bool if the file has expired or not |
118
|
|
|
*/ |
119
|
|
|
public static function file_expired($archivo, $fecha = null) { |
120
|
|
|
if( ! file_exists($archivo) ) { |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
return (time() > (filemtime($archivo) + 60 * ($fecha ? $fecha : self::$configuracion['expires']))); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.