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