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->analyserRessources();
|
935 |
gduche |
65 |
$retour = $this->initialiserService();
|
|
|
66 |
return $retour;
|
|
|
67 |
}
|
|
|
68 |
|
1707 |
jpm |
69 |
private function analyserRessources() {
|
|
|
70 |
if ($this->methode == 'consulter') {
|
1806 |
jpm |
71 |
if (count($this->ressources) == 0 && $this->verifierPresenceParametre('masque.nom')) {
|
|
|
72 |
$this->sousServiceNom = 'liste-communes';
|
1707 |
jpm |
73 |
}
|
|
|
74 |
}
|
|
|
75 |
if ($this->sousServiceNom == null) {
|
|
|
76 |
$this->lancerMessageErreurRessource();
|
|
|
77 |
}
|
935 |
gduche |
78 |
}
|
|
|
79 |
|
1707 |
jpm |
80 |
private function verifierPresenceParametre($cle) {
|
1806 |
jpm |
81 |
if (isset($this->parametres[$cle]) && trim($this->parametres[$cle]) == '') {
|
|
|
82 |
$message = "Le service demandé '{$this->serviceNom}' ".
|
|
|
83 |
"nécessite l'utilisation d'un paramètre (non vide) : masque.nom \n";
|
|
|
84 |
throw new Exception($message, RestServeur::HTTP_CODE_ECHEC_CONDITION);
|
1707 |
jpm |
85 |
}
|
1806 |
jpm |
86 |
return true;
|
1707 |
jpm |
87 |
}
|
|
|
88 |
|
|
|
89 |
private function lancerMessageErreurRessource() {
|
|
|
90 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
91 |
$message = "La ressource demandée '$ressource' ".
|
|
|
92 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
|
|
93 |
"Les URLs disponibles sont : \n".
|
|
|
94 |
" - en GET : communes (paramètres : masque.nom) \n";
|
|
|
95 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
96 |
throw new Exception($message, $code);
|
|
|
97 |
}
|
|
|
98 |
|
935 |
gduche |
99 |
private function initialiserService() {
|
1707 |
jpm |
100 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
935 |
gduche |
101 |
$chemins = array();
|
1707 |
jpm |
102 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
935 |
gduche |
103 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
104 |
$retour = '';
|
|
|
105 |
$service = null;
|
1707 |
jpm |
106 |
|
935 |
gduche |
107 |
foreach ($chemins as $chemin) {
|
|
|
108 |
if (file_exists($chemin)) {
|
|
|
109 |
require_once $chemin;
|
|
|
110 |
$service = new $classe($this->conteneur);
|
|
|
111 |
if ($this->methode == 'consulter') {
|
1806 |
jpm |
112 |
$retour = $service->consulter($this->parametres);
|
935 |
gduche |
113 |
} else {
|
1707 |
jpm |
114 |
$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
|
|
|
115 |
"ne possède pas de méthode '{$this->methode}' !";
|
|
|
116 |
$code = RestServeur::HTTP_NON_IMPLEMENTE;
|
|
|
117 |
throw new Exception($message, $code);
|
935 |
gduche |
118 |
}
|
|
|
119 |
}
|
|
|
120 |
}
|
1700 |
jpm |
121 |
|
935 |
gduche |
122 |
if (is_null($service)) {
|
1707 |
jpm |
123 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
124 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
125 |
"est introuvable par le service '{$this->serviceNom}' !";
|
935 |
gduche |
126 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
127 |
throw new Exception($message, $code);
|
|
|
128 |
}
|
|
|
129 |
return $retour;
|
|
|
130 |
}
|
1700 |
jpm |
131 |
|
935 |
gduche |
132 |
private function obtenirNomClasseService($mot) {
|
|
|
133 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
134 |
return $classeNom;
|
|
|
135 |
}
|
1707 |
jpm |
136 |
}
|