1
|
|
|
<?php |
2
|
|
|
namespace core; |
3
|
|
|
use core\functions\ChaineCaractere; |
4
|
|
|
|
5
|
|
|
class Cache { |
6
|
|
|
private $dossier_cache; //dossier ou sont stockés tous les caches |
7
|
|
|
|
8
|
|
|
private $chemin_page; //chemin vers la page a mettre ou aller chercher en cache |
9
|
|
|
private $page; //nom de la page a chercher ou a mettre en cache |
10
|
|
|
private $chemin_cache; //chemin du fichier dans le dossier cache |
11
|
|
|
private $no_cache; //definit dans get cache pour dire que cette page ne doit jamais etre en cache |
12
|
|
|
|
13
|
|
|
private $cache_active; //si == 1 le cache est actif sur le site |
14
|
|
|
|
15
|
|
|
private $reload_cache; //si == 1 cela veut dire que l'on doit recharger le cache de la page |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------// |
20
|
|
|
public function __construct($page) { |
21
|
|
|
$config = new Configuration(); |
22
|
|
|
$this->cache_active = 0; |
23
|
|
|
$this->dossier_cache = ROOT."cache/"; |
24
|
|
|
|
25
|
|
|
//on test si le cache est bien active |
26
|
|
|
if ($config->getCache() == 1) { |
27
|
|
|
$this->cache_active = 1; |
28
|
|
|
$this->setCreerDossier(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$page = ChaineCaractere::setUrl($page); |
32
|
|
|
$page = str_replace("/", "-", $page); |
33
|
|
|
|
34
|
|
|
$this->page = $page.".html"; |
35
|
|
|
$this->chemin_cache = $this->dossier_cache.$this->page; |
36
|
|
|
$this->chemin_page = $page.".hmtl"; |
37
|
|
|
} |
38
|
|
|
//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------// |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
43
|
|
|
/** |
44
|
|
|
* fonction qui test si on a besoin de racharger le cache d'une page |
45
|
|
|
* et si la page a le droit d'etre mise en cache |
46
|
|
|
*/ |
47
|
|
|
private function getTestCache() { |
48
|
|
|
$dbc = App::getDb(); |
49
|
|
|
|
50
|
|
|
//on regarde si il existe et un cache et si il faut ou non le remettre à jour |
51
|
|
|
$query = $dbc->select()->from("cache")->where("nom_fichier", "=", $this->page)->get(); |
52
|
|
|
|
53
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
54
|
|
|
$this->reload_cache = 0; |
55
|
|
|
foreach ($query as $obj) { |
56
|
|
|
$this->reload_cache = $obj->reload_cache; |
57
|
|
|
$this->no_cache = $obj->no_cache; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
else { |
61
|
|
|
$dbc->insert("nom_fichier", $this->page)->insert("reload_cache", 0)->into("cache")->set(); |
62
|
|
|
|
63
|
|
|
$this->reload_cache = 0; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return boolean|null |
69
|
|
|
* fonction verifie en bdd si on a déjà enregistrer le fichier en cache |
70
|
|
|
* si il ne l'est pas on le met, et si il y est et que reload cache == 0 on prend le fichier qui est en cache |
71
|
|
|
* sinon soit on update la bdd et on refait un cache soit on crée tout |
72
|
|
|
*/ |
73
|
|
|
private function getCache() { |
74
|
|
|
$this->getTestCache(); |
75
|
|
|
|
76
|
|
|
if ((file_exists($this->chemin_cache)) && ($this->reload_cache == 0) && ($this->no_cache == null)) { |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
//-------------------------- FIN GETTER ----------------------------------------------------------------------------// |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
85
|
|
|
private function setCreerDossier() { |
86
|
|
|
//on crée les dossier du cache si ils n'existent pas deja |
87
|
|
|
if (!file_exists(ROOT."cache")) { |
88
|
|
|
mkdir(ROOT."cache"); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
* fonction qui permet de démarrer l'affichage de la page |
95
|
|
|
* soit en allant la chercher dans le cache |
96
|
|
|
* sinon on lance un ob_start |
97
|
|
|
*/ |
98
|
|
|
public function setStart() { |
99
|
|
|
if ($this->cache_active == 1) { |
100
|
|
|
if ($this->getCache() == true) { |
101
|
|
|
require_once($this->chemin_cache); |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
else { |
106
|
|
|
if ($this->no_cache == null) { |
107
|
|
|
ob_start(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
else { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* fonction qui fini de récupérer le contenu et qui le met en cache |
120
|
|
|
* une fois mis en cache on affiche la page |
121
|
|
|
*/ |
122
|
|
|
public function setEnd() { |
123
|
|
|
if ($this->cache_active == 1) { |
124
|
|
|
if (($this->getCache() != true) && ($this->no_cache == null)) { |
125
|
|
|
$contenu = ob_get_clean(); |
126
|
|
|
|
127
|
|
|
$this->setCache($contenu); |
128
|
|
|
|
129
|
|
|
echo $contenu; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $contenu_fichier |
136
|
|
|
* fonction qui met en cache le contenu du fichier enregistrer dans le ob |
137
|
|
|
*/ |
138
|
|
|
private function setCache($contenu_fichier) { |
139
|
|
|
$dbc = App::getDb(); |
140
|
|
|
|
141
|
|
|
$fichier_cache = $this->chemin_cache; |
142
|
|
|
|
143
|
|
|
file_put_contents($fichier_cache, $contenu_fichier); |
144
|
|
|
|
145
|
|
|
$dbc->update("reload_cache", 0)->from("cache")->where("nom_fichier", "=", $this->page)->set(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $nom_fichier |
150
|
|
|
* fonction qui permet de dire qu'il faut recharger le cache d'un fichier spécifique |
151
|
|
|
* appeler par des controller (dans des modules ou dans l'admin...) |
152
|
|
|
*/ |
153
|
|
|
public static function setReloadCache($nom_fichier) { |
154
|
|
|
$dbc = App::getDb(); |
155
|
|
|
|
156
|
|
|
$nom_fichier = ChaineCaractere::setUrl($nom_fichier); |
157
|
|
|
$nom_fichier = str_replace("/", "-", $nom_fichier); |
158
|
|
|
|
159
|
|
|
$value = [ |
160
|
|
|
"reload" => 1, |
161
|
|
|
"nom_fichier" => $nom_fichier |
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
$dbc->prepare("UPDATE cache SET reload_cache=:reload WHERE nom_fichier LIKE :nom_fichier", $value); |
165
|
|
|
} |
166
|
|
|
//-------------------------- FIN SETTER ----------------------------------------------------------------------------// |
167
|
|
|
} |