Subversion Repositories eFlore/Applications.del

Rev

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

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