Passed
Push — master ( 9db407...b7ae74 )
by Anthony
02:58
created

Cache   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 22
c 7
b 1
f 0
lcom 1
cbo 4
dl 0
loc 170
rs 10

6 Methods

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