Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 26 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 delphine 1
<?php
2
 
3
 
26 alex 4
class Projets extends RestService {
20 delphine 5
 
6
 
7
	private $parametres = array();
8
	private $ressources = array();
9
 
10
	private $squeletteDossier = '';
11
	private $cache;
12
 
13
 
14
	public function __construct() {
26 alex 15
		$this->squeletteDossier = dirname(__FILE__).DIRECTORY_SEPARATOR;
20 delphine 16
	}
17
 
18
 
19
	public function consulter($ressources, $parametres) {
20
		$resultat = '';
21
		$reponseHttp = new ReponseHttp();
22
		try {
23
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
24
			$resultat = $this->traiterRessources();
25
			$reponseHttp->setResultatService($resultat);
26
		} catch (Exception $e) {
27
			$reponseHttp->ajouterErreur($e);
28
		}
29
		$reponseHttp->emettreLesEntetes();
30
		$corps = $reponseHttp->getCorps();
31
		return $corps;
32
	}
33
 
34
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
35
		$this->ressources = $ressources;
36
		$this->parametres = $parametres;
37
	}
38
 
39
	private function traiterRessources() {
40
		$retour = '';
41
		if ($this->avoirRessources()) {
42
			if ($this->avoirRessourceService()) {
26 alex 43
				$retour = $this->initialiserService();
20 delphine 44
			}
45
		}
46
		return $retour;
47
	}
48
 
49
	private function avoirRessources() {
50
		$presenceDeRessources = false;
51
		if (isset($this->ressources) && count($this->ressources) > 0) {
52
			$presenceDeRessources = true;
53
		} else {
54
			$message = "Accès au service refusé : aucune ressource indiquée.\n"
55
				. "Veuillez indiquer au moins une source de données à interroger"
26 alex 56
				. " et le type d'informations a renvoyer.";
20 delphine 57
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
58
			throw new Exception($message, $code);
59
		}
60
		return $presenceDeRessources;
61
	}
62
 
26 alex 63
	private function chargerConfiguration($nomParametre) {
20 delphine 64
		$tableau = array();
26 alex 65
		$tableauPartiel = explode(',', Config::get($nomParametre));
20 delphine 66
		$tableauPartiel = array_map('trim', $tableauPartiel);
67
		foreach ($tableauPartiel as $champ) {
68
			if (strpos($champ, '=') === false) {
69
				$tableau[] = $champ;
70
			} else {
71
				list($cle, $val) = explode('=', $champ);
72
				$clePropre = trim($cle);
73
				$valeurPropre = trim($val);
74
				$tableau[$clePropre] = $valeurPropre;
75
			}
76
		}
77
		return $tableau;
78
	}
79
 
26 alex 80
	public static function chargerConfigurationSource($nomSource) {
81
		$chemin = Config::get('chemin_configurations')."config_$nomSource.ini";
20 delphine 82
		Config::charger($chemin);
83
	}
84
 
26 alex 85
	private function chargerClasse($classe) {
20 delphine 86
		if (class_exists($classe)) {
87
			return null;
88
		}
26 alex 89
 
20 delphine 90
		$cheminBiblio = Config::get('chemin_bibliotheque');
91
		$chemins = array();
26 alex 92
		$chemins[] = $this->squeletteDossier;
93
		$chemins[] = $this->squeletteDossier.'commun' .DIRECTORY_SEPARATOR;
94
		$chemins[] = $this->squeletteDossier.'sources'.DIRECTORY_SEPARATOR;
20 delphine 95
		$chemins[] = $cheminBiblio;
26 alex 96
		$chemins[] = $cheminBiblio.'robots'.DIRECTORY_SEPARATOR;
20 delphine 97
 
98
		foreach ($chemins as $chemin) {
26 alex 99
			$chemin = $chemin.$classe.'.php';
20 delphine 100
			if (file_exists($chemin)) {
101
				require_once $chemin;
102
				break;
103
			}
104
		}
105
	}
106
 
107
	private function avoirRessourceService() {
108
		$presenceRessourceService = false;
26 alex 109
		$servicesDispo = $this->chargerConfiguration('services_dispo');
110
		if (isset($this->ressources[0])) {
111
			$service = $this->ressources[0];
20 delphine 112
			if (in_array($service, $servicesDispo)) {
113
				$presenceRessourceService = true;
114
			} else {
26 alex 115
				$message = "La service demandé '$service' n'est pas disponible !\n"
116
					. "Les services disponibles sont : ".implode(', ', $servicesDispo);
20 delphine 117
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
118
				throw new Exception($message, $code);
119
			}
120
		} else {
26 alex 121
			$message = "Vous n'avez pas indiqué de service !\n"
122
				. "Les services disponibles sont : ".implode(', ', $servicesDispo);
20 delphine 123
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
124
			throw new Exception($message, $code);
125
		}
126
		return $presenceRessourceService;
127
	}
128
 
129
	private function initialiserService() {
26 alex 130
		$nomService = 'commun';
131
		$classe = $this->obtenirNomClasseService($nomService);
20 delphine 132
		$chemins = array();
26 alex 133
		$chemins[] = $this->squeletteDossier.'commun'.DIRECTORY_SEPARATOR.'Commun.php';
134
 
135
		// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
136
		if (phpversion() < 5.3) {
137
			spl_autoload_register(array($this, 'chargerClasse'));
138
		} else {
139
			spl_autoload_register(array($this, 'chargerClasse'), true , true);
140
		}
20 delphine 141
 
142
		$retour = '';
143
		$service = null;
144
		foreach ($chemins as $chemin) {
145
			if (file_exists($chemin)) {
146
				$service = new $classe($this->getBdd());
147
				$ressourcesPourService = $this->filtrerRessourcesPourService();
26 alex 148
				$this->cache = new CacheMoissonnage($service, $nomService,
20 delphine 149
					Config::get('cache'));
150
				$retour = $this->cache->consulter($ressourcesPourService, $this->parametres);
151
				if (get_class($retour) == 'Exception') {
152
					throw new Exception($retour->getMessage(), RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
153
				}
154
			}
155
		}
156
		if (is_null($service)) {
26 alex 157
			$message = "Le service demandé '{$nomService}' n'existe pas !";
20 delphine 158
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
159
			throw new Exception($message, $code);
160
		}
161
		return $retour;
162
	}
163
 
164
	private function obtenirNomClasseService($mot) {
165
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
166
		return $classeNom;
167
	}
168
 
169
	private function filtrerRessourcesPourService() {
26 alex 170
		return $this->ressources;
20 delphine 171
	}
172
 
173
}
174
 
175
?>