aboutsummaryrefslogtreecommitdiff
path: root/lib/minz/Cache.php
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2013-06-14 20:53:12 +0200
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2013-06-14 20:53:12 +0200
commitfb5e5594bea149ca730bc6424dc547dab3347747 (patch)
tree62a8d919e2f209bc74f3f8afc0f058e6801fe3db /lib/minz/Cache.php
parent1d459af0464eb2cdbdaaaf1122e1681210fdffea (diff)
Fix issue #82 : ajout direct de Minz sans devoir faire appel au script ./build.sh
Diffstat (limited to 'lib/minz/Cache.php')
-rw-r--r--lib/minz/Cache.php116
1 files changed, 116 insertions, 0 deletions
diff --git a/lib/minz/Cache.php b/lib/minz/Cache.php
new file mode 100644
index 000000000..69143a70c
--- /dev/null
+++ b/lib/minz/Cache.php
@@ -0,0 +1,116 @@
+<?php
+/**
+ * MINZ - Copyright 2011 Marien Fressinaud
+ * Sous licence AGPL3 <http://www.gnu.org/licenses/>
+*/
+
+/**
+ * La classe Cache permet de gérer facilement les pages en cache
+ */
+class Cache {
+ /**
+ * $expire timestamp auquel expire le cache de $url
+ */
+ private $expire = 0;
+
+ /**
+ * $file est le nom du fichier de cache
+ */
+ private $file = '';
+
+ /**
+ * $enabled permet de déterminer si le cache est activé
+ */
+ private static $enabled = true;
+
+ /**
+ * Constructeur
+ */
+ public function __construct () {
+ $this->_fileName ();
+ $this->_expire ();
+ }
+
+ /**
+ * Setteurs
+ */
+ public function _fileName () {
+ $file = md5 (Request::getURI ());
+
+ $this->file = CACHE_PATH . '/'.$file;
+ }
+
+ public function _expire () {
+ if ($this->exist ()) {
+ $this->expire = filemtime ($this->file)
+ + Configuration::delayCache ();
+ }
+ }
+
+ /**
+ * Permet de savoir si le cache est activé
+ * @return true si activé, false sinon
+ */
+ public static function isEnabled () {
+ return Configuration::cacheEnabled () && self::$enabled;
+ }
+
+ /**
+ * Active / désactive le cache
+ */
+ public static function switchOn () {
+ self::$enabled = true;
+ }
+ public static function switchOff () {
+ self::$enabled = false;
+ }
+
+ /**
+ * Détermine si le cache de $url a expiré ou non
+ * @return true si il a expiré, false sinon
+ */
+ public function expired () {
+ return time () > $this->expire;
+ }
+
+ /**
+ * Affiche le contenu du cache
+ * @print le code html du cache
+ */
+ public function render () {
+ if ($this->exist ()) {
+ include ($this->file);
+ }
+ }
+
+ /**
+ * Enregistre $html en cache
+ * @param $html le html à mettre en cache
+ */
+ public function cache ($html) {
+ file_put_contents ($this->file, $html);
+ }
+
+ /**
+ * Permet de savoir si le cache existe
+ * @return true si il existe, false sinon
+ */
+ public function exist () {
+ return file_exists ($this->file);
+ }
+
+ /**
+ * Nettoie le cache en supprimant tous les fichiers
+ */
+ public static function clean () {
+ $files = opendir (CACHE_PATH);
+
+ while ($fic = readdir ($files)) {
+ if ($fic != '.' && $fic != '..') {
+ unlink (CACHE_PATH.'/'.$fic);
+ }
+ }
+
+ closedir ($files);
+ }
+}