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, $admin = null) { |
21
|
|
|
$config = new Configuration(); |
22
|
|
|
|
23
|
|
|
//on test si le cache est bien active |
24
|
|
|
if ($config->getCache() == 1) { |
25
|
|
|
$this->cache_active = 1; |
26
|
|
|
|
27
|
|
|
//on crée les dossier du cache si ils n'existent pas deja |
28
|
|
|
if (!file_exists(ROOT."cache")) { |
29
|
|
|
mkdir(ROOT."cache"); |
30
|
|
|
mkdir(ROOT."cache/admin"); |
31
|
|
|
mkdir(ROOT."cache/app"); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
else { |
35
|
|
|
$this->cache_active = 0; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($admin == null) { |
39
|
|
|
$this->dossier_cache = ROOT."cache/app/"; |
40
|
|
|
} |
41
|
|
|
else { |
42
|
|
|
$this->dossier_cache = ROOT."cache/admin/"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$page = ChaineCaractere::setUrl($page); |
46
|
|
|
$page = str_replace("/", "-", $page); |
47
|
|
|
|
48
|
|
|
$this->page = $page.".php"; |
49
|
|
|
$this->chemin_cache = $this->dossier_cache.$this->page; |
50
|
|
|
$this->chemin_page = $page.".php"; |
51
|
|
|
} |
52
|
|
|
//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------// |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
57
|
|
|
/** |
58
|
|
|
* fonction qui test si on a besoin de racharger le cache d'une page |
59
|
|
|
* et si la page a le droit d'etre mise en cache |
60
|
|
|
*/ |
61
|
|
|
private function getTestCache() { |
62
|
|
|
$dbc = App::getDb(); |
63
|
|
|
|
64
|
|
|
//on regarde si il existe et un cache et si il faut ou non le remettre à jour |
65
|
|
|
$query = $dbc->query("SELECT * FROM cache WHERE nom_fichier like '$this->page'"); |
66
|
|
|
|
67
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
68
|
|
|
$this->reload_cache = 0; |
69
|
|
|
foreach ($query as $obj) { |
70
|
|
|
$this->reload_cache = $obj->reload_cache; |
71
|
|
|
$this->no_cache = $obj->no_cache; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
else { |
75
|
|
|
$value = [ |
76
|
|
|
"nom_fichier" => $this->page, |
77
|
|
|
"reload_cache" => 0 |
78
|
|
|
]; |
79
|
|
|
$dbc->prepare("INSERT INTO cache (nom_fichier, reload_cache) VALUES (:nom_fichier, :reload_cache)", $value); |
80
|
|
|
|
81
|
|
|
$this->reload_cache = 0; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return boolean|null |
87
|
|
|
* fonction verifie en bdd si on a déjà enregistrer le fichier en cache |
88
|
|
|
* 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 |
89
|
|
|
* sinon soit on update la bdd et on refait un cache soit on crée tout |
90
|
|
|
*/ |
91
|
|
|
private function getCache() { |
92
|
|
|
$this->getTestCache(); |
93
|
|
|
|
94
|
|
|
if ((file_exists($this->chemin_cache)) && ($this->reload_cache == 0) && ($this->no_cache == null)) { |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
//-------------------------- FIN GETTER ----------------------------------------------------------------------------// |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
|
102
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
103
|
|
|
/** |
104
|
|
|
* @return bool |
105
|
|
|
* fonction qui permet de démarrer l'affichage de la page |
106
|
|
|
* soit en allant la chercher dans le cache |
107
|
|
|
* sinon on lance un ob_start |
108
|
|
|
*/ |
109
|
|
|
public function setStart() { |
110
|
|
|
if ($this->cache_active == 1) { |
111
|
|
|
if ($this->getCache() == true) { |
112
|
|
|
require_once($this->chemin_cache); |
113
|
|
|
|
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
else { |
117
|
|
|
if ($this->no_cache == null) { |
118
|
|
|
ob_start(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
else { |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* fonction qui fini de récupérer le contenu et qui le met en cache |
131
|
|
|
* une fois mis en cache on affiche la page |
132
|
|
|
*/ |
133
|
|
|
public function setEnd() { |
134
|
|
|
if ($this->cache_active == 1) { |
135
|
|
|
if (($this->getCache() != true) && ($this->no_cache == null)) { |
136
|
|
|
$contenu = ob_get_clean(); |
137
|
|
|
|
138
|
|
|
$this->setCache($contenu); |
139
|
|
|
|
140
|
|
|
echo $contenu; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $contenu_fichier |
147
|
|
|
* fonction qui met en cache le contenu du fichier enregistrer dans le ob |
148
|
|
|
*/ |
149
|
|
|
private function setCache($contenu_fichier) { |
150
|
|
|
$dbc = App::getDb(); |
151
|
|
|
|
152
|
|
|
$fichier_cache = $this->chemin_cache; |
153
|
|
|
|
154
|
|
|
file_put_contents($fichier_cache, $contenu_fichier); |
155
|
|
|
|
156
|
|
|
$value = [ |
157
|
|
|
"nom_fichier" => $this->page, |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
$dbc->prepare("UPDATE cache SET reload_cache=0 WHERE nom_fichier=:nom_fichier", $value); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param $nom_fichier |
165
|
|
|
* fonction qui permet de dire qu'il faut recharger le cache d'un fichier spécifique |
166
|
|
|
* appeler par des controller (dans des modules ou dans l'admin...) |
167
|
|
|
*/ |
168
|
|
|
public static function setReloadCache($nom_fichier) { |
169
|
|
|
$dbc = App::getDb(); |
170
|
|
|
|
171
|
|
|
$nom_fichier = ChaineCaractere::setUrl($nom_fichier); |
172
|
|
|
$nom_fichier = str_replace("/", "-", $nom_fichier); |
173
|
|
|
|
174
|
|
|
$value = [ |
175
|
|
|
"reload" => 1, |
176
|
|
|
"nom_fichier" => $nom_fichier |
177
|
|
|
]; |
178
|
|
|
|
179
|
|
|
$dbc->prepare("UPDATE cache SET reload_cache=:reload WHERE nom_fichier LIKE :nom_fichier", $value); |
180
|
|
|
} |
181
|
|
|
//-------------------------- FIN SETTER ----------------------------------------------------------------------------// |
182
|
|
|
} |