Subversion Repositories Applications.referentiel

Rev

Rev 37 | Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 jpm 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
* Classe modèle spécifique à l'application, donc d'accés au données, elle ne devrait pas être appelée de l'extérieur.
5
* Elle est abstraite donc doit obligatoirement être étendue.
6
*
7
* @category		Php5
8
* @package 		Referentiel
9
* @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
10
* @copyright	2010 Tela-Botanica
11
* @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
12
* @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
13
* @version		SVN: $Id$
14
*/
20 jpm 15
abstract class Dao {
36 jpm 16
	const HTTP_REQUETE_SEPARATEUR = '&';
6 jpm 17
	protected $distinction = '0';
18
	protected $limite_debut = null;
19
	protected $limite_nbre = null;
20
	protected $url_jrest = null;
21
 
22
	public function __construct() {
26 jpm 23
		$this->url_jrest = Config::get('url_jrest');
6 jpm 24
	}
25
 
23 jpm 26
	//+----------------------------------------------------------------------------------------------------------------+
27
	// ACCESSEURS
28
 
6 jpm 29
	public function avoirLimitation() {
30
		$limitation = false;
31
		if (!is_null($this->limite_debut) && !is_null($this->limite_nbre)) {
32
			$limitation = true;
33
		}
34
		return $limitation;
35
	}
36
 
37
	public function setDistinction($distinct) {
38
		$this->distinction = $distinct;
39
	}
40
 
41
	public function getDistinction() {
42
		return $this->distinction;
43
	}
44
 
45
	public function setLimitation($limite_debut, $limite_nbre) {
46
		$this->limite_debut = $limite_debut;
47
		$this->limite_nbre = $limite_nbre;
48
	}
49
 
50
	public function getLimiteDebut() {
51
		return $this->limite_debut;
52
	}
53
	public function getLimiteNbre() {
54
		return $this->limite_nbre;
55
	}
23 jpm 56
 
57
	//+----------------------------------------------------------------------------------------------------------------+
58
	// MÉTHODES
24 jpm 59
 
60
	protected function envoyerRequeteConsultation($url) {
38 jpm 61
		$url = $this->traiterUrlParametres($url);
24 jpm 62
		$retour = $this->envoyerRequete($url, 'GET');
63
		return $retour;
64
	}
65
 
23 jpm 66
	protected function envoyerRequeteAjout($url, Array $donnees) {
24 jpm 67
		$retour = $this->envoyerRequete($url, 'PUT', $donnees);
23 jpm 68
		return $retour;
69
	}
70
 
26 jpm 71
	protected function envoyerRequeteModif($url, Array $donnees) {
24 jpm 72
		$retour = $this->envoyerRequete($url, 'POST', $donnees);
23 jpm 73
		return $retour;
74
	}
75
 
76
	protected function envoyerRequeteSuppression($url) {
36 jpm 77
		$retour = $this->envoyerRequete($url, 'DELETE');
23 jpm 78
		return $retour;
79
	}
80
 
24 jpm 81
	private function envoyerRequete($url, $mode, Array $donnees = array()) {
82
		$contenu = false;
83
		if ($mode != 'GET' && $mode != 'PUT' && $mode != 'POST' && $mode != 'DELETE') {
23 jpm 84
			$e = "Le mode de requête '$mode' n'est pas accepté!";
85
			trigger_error($e, E_USER_WARNING);
86
		} else {
24 jpm 87
			$contexte = stream_context_create(array(
88
				'http' => array(
89
      				'method' => $mode,
23 jpm 90
					'header' => "Content-type: application/x-www-form-urlencoded\r\n",
37 jpm 91
      				'content' => http_build_query($donnees, null, self::HTTP_REQUETE_SEPARATEUR))));
24 jpm 92
			$flux = @fopen($url, 'r', false, $contexte);
93
			if (!$flux) {
94
				$this->traiterEntete($http_response_header, $url);
95
				$e = "L'ouverture de l'url '$url' a échoué!";
96
				trigger_error($e, E_USER_WARNING);
97
			} else {
98
				// Informations sur les en-têtes et métadonnées du flux
99
				$entetes = stream_get_meta_data($flux);
100
				$this->traiterEntete($entetes, $url);
101
 
102
				// Contenu actuel de $url
103
				$contenu = stream_get_contents($flux);
104
 
105
				fclose($flux);
106
			}
23 jpm 107
		}
24 jpm 108
		return $contenu;
23 jpm 109
	}
24 jpm 110
 
38 jpm 111
	private function traiterUrlParametres($url) {
112
		$parametres = array();
113
		if (! is_null($this->getLimiteDebut())) {
114
			$parametres[] = 'start='.$this->getLimiteDebut();
115
		}
116
		if (! is_null($this->getLimiteNbre())) {
117
			$parametres[] = 'limit='.$this->getLimiteNbre();
118
		}
119
		if ($this->getDistinction() != 0) {
120
			$parametres[] = 'distinct='.$this->getDistinction();
121
		}
122
		if (count($parametres) > 0) {
123
			$url_parametres = implode('&', $parametres);
124
			$url = $url.'?'.$url_parametres;
125
		}
126
		return $url;
127
	}
128
 
24 jpm 129
	private function traiterEntete($entetes, $uri) {
130
		$infos = $this->analyserEntete($entetes, $uri);
131
		$this->traiterEnteteDebug($infos);
132
		$this->traiterEnteteMessage($infos);
133
	}
134
 
135
	private function analyserEntete($entetes, $uri) {
136
		$infos = array('date' => null, 'uri' => $uri, 'debugs' => null, 'messages' => null);
137
 
138
		if (isset($entetes['wrapper_data'])) {
139
			$entetes = $entetes['wrapper_data'];
140
		}
141
		foreach ($entetes as $entete) {
142
			if (preg_match('/^X-DebugJrest-Data: (.+)$/', $entete, $match)) {
143
				$infos['debugs'] = json_decode($match[1]);
144
			}
145
			if (preg_match('/^X-MessageJrest-Data: (.+)$/', $entete, $match)) {
146
				$infos['messages'] = json_decode($match[1]);
147
			}
148
			if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
149
				$infos['date'] = $match[1];
150
			}
151
		}
152
		return $infos;
153
	}
154
 
155
	private function traiterEnteteDebug($entetes) {
156
		if (isset($entetes['debugs'])) {
157
			$date = $entetes['date'];
158
			$uri = $entetes['uri'];
159
			$debugs = $entetes['debugs'];
160
			foreach ($debugs as $debug) {
161
				Debug::printr("DEBUG : $date - $uri :\n$debug");
162
			}
163
		}
164
	}
165
 
166
	private function traiterEnteteMessage($entetes) {
167
		if (isset($entetes['messages'])) {
168
			$date = $entetes['date'];
169
			$uri = $entetes['uri'];
170
			$messages = $entetes['messages'];
171
			foreach ($messages as $message) {
172
				Debug::printr("MESSAGE : $date - $uri :\n$message");
173
			}
174
		}
175
	}
6 jpm 176
}