Subversion Repositories eFlore/Applications.del

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
935 gduche 1
<?php
2
/**
1707 jpm 3
 * Classe principale de chargement des sous-services d'accès aux infos sur les communes.
4
 *
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
7
 *
8
 * @category	DEL
9
 * @package		Services
10
 * @subpackage	Communes
11
 * @version		0.1
12
 * @author		Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
13
 * @author		Delphine CAUQUIL <delphine@tela-botanica.org>
14
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
15
 * @license		GPL v3 <http://www.gnu.org/licenses/gpl.txt>
16
 * @license		CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
17
 * @copyright	©1999-2014, Tela Botanica (<accueil@tela-botanica.org>)
18
 */
935 gduche 19
class Communes extends RestService {
20
 
21
	private $parametres = array();
22
	private $ressources = array();
23
	private $methode = null;
1707 jpm 24
	private $serviceNom = 'communes';
25
	private $sousServiceNom = null;
935 gduche 26
	private $cheminCourant = null;
27
 
28
	private $conteneur;
1700 jpm 29
 
935 gduche 30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
31
	protected $utilisationParametresBruts = true;
32
 
33
	public function __construct() {
34
		$this->cheminCourant = dirname(__FILE__).DS;
35
	}
36
 
37
	public function consulter($ressources, $parametres) {
38
		$this->methode = 'consulter';
1707 jpm 39
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
40
		return $this->executerService();
41
	}
42
 
43
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
44
		$this->ressources = $ressources;
45
		$this->parametres = $parametres;
46
	}
47
 
48
	private function executerService() {
935 gduche 49
		$reponseHttp = new ReponseHttp();
50
		try {
51
			$this->conteneur = new Conteneur($this->parametres);
52
			$resultat = $this->traiterRessources();
53
			$reponseHttp->setResultatService($resultat);
54
		} catch (Exception $e) {
55
			$reponseHttp->ajouterErreur($e);
56
		}
57
		$reponseHttp->emettreLesEntetes();
58
		$corps = $reponseHttp->getCorps();
59
		return $corps;
60
	}
1700 jpm 61
 
935 gduche 62
	private function traiterRessources() {
1707 jpm 63
		$this->chargerConfigService();
64
		$this->analyserRessources();
935 gduche 65
		$retour = $this->initialiserService();
66
		return $retour;
67
	}
68
 
1707 jpm 69
	private function chargerConfigService() {
70
		$chemin = Config::get('chemin_configurations')."config_{$this->serviceNom}.ini";
71
		Config::charger($chemin);
935 gduche 72
	}
73
 
1707 jpm 74
	private function analyserRessources() {
75
		if ($this->methode == 'consulter') {
76
			if (count($this->ressources) == 0) {
77
				if ($this->verifierPresenceParametre('masque.nom')) {
78
					$this->sousServiceNom = 'liste-communes';
79
				} else {
80
					$message = "Le service demandé '{$this->serviceNom}' ".
81
						"nécessite l'utilisation d'un paramètre (non vide) : masque.nom \n";
82
					$code = RestServeur::HTTP_CODE_ECHEC_CONDITION;
83
					throw new Exception($message, $code);
84
				}
85
			}
86
		}
87
		if ($this->sousServiceNom == null) {
88
			$this->lancerMessageErreurRessource();
89
		}
935 gduche 90
	}
91
 
1707 jpm 92
	private function verifierPresenceParametre($cle) {
93
		$ok = false;
94
		if (isset($this->parametres[$cle]) && trim($this->parametres[$cle]) != '') {
95
			$ok = true;
96
		}
97
		return $ok;
98
	}
99
 
100
	private function lancerMessageErreurRessource() {
101
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
102
		$message = "La ressource demandée '$ressource' ".
103
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
104
			"Les URLs disponibles sont : \n".
105
			" - en GET : communes (paramètres : masque.nom) \n";
106
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
107
		throw new Exception($message, $code);
108
	}
109
 
935 gduche 110
	private function initialiserService() {
1707 jpm 111
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
935 gduche 112
		$chemins = array();
1707 jpm 113
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
935 gduche 114
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
115
		$retour = '';
116
		$service = null;
1707 jpm 117
 
935 gduche 118
		foreach ($chemins as $chemin) {
119
			if (file_exists($chemin)) {
1707 jpm 120
				$this->conteneur->chargerConfiguration('config_'.$this->serviceNom.'.ini');
935 gduche 121
				require_once $chemin;
122
				$service = new $classe($this->conteneur);
123
				if ($this->methode == 'consulter') {
124
					$retour = $service->consulter($this->ressources, $this->parametres);
125
				} else {
1707 jpm 126
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
127
						"ne possède pas de méthode '{$this->methode}' !";
128
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
129
					throw new Exception($message, $code);
935 gduche 130
				}
131
			}
132
		}
1700 jpm 133
 
935 gduche 134
		if (is_null($service)) {
1707 jpm 135
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
136
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
137
				"est introuvable par le service '{$this->serviceNom}' !";
935 gduche 138
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
139
			throw new Exception($message, $code);
140
		}
141
		return $retour;
142
	}
1700 jpm 143
 
935 gduche 144
	private function obtenirNomClasseService($mot) {
145
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
146
		return $classeNom;
147
	}
1707 jpm 148
}