Passed
Push — master ( 25ab92...d5a1bb )
by Anthony
03:06
created

Cache::getTestCache()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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