2212 |
arthur |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Classe principale de chargement des sous-services de Protocoles.
|
|
|
5 |
*
|
|
|
6 |
* Urls possibles :
|
|
|
7 |
* http://localhost/service:del:0.1/protocoles => tous les protocoles
|
|
|
8 |
*
|
|
|
9 |
* @category DEL
|
|
|
10 |
* @package Services
|
|
|
11 |
* @subpackage Protocoles
|
|
|
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>
|
|
|
19 |
*/
|
|
|
20 |
class Protocoles extends RestService {
|
|
|
21 |
|
|
|
22 |
private $parametres = array();
|
|
|
23 |
private $ressources = array();
|
|
|
24 |
private $methode = null;
|
|
|
25 |
private $serviceNom = 'protocoles';
|
|
|
26 |
private $sousServiceNom = null;
|
|
|
27 |
private $cheminCourant = null;
|
|
|
28 |
|
|
|
29 |
private $conteneur;
|
|
|
30 |
|
|
|
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';
|
|
|
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() {
|
|
|
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 |
}
|
|
|
62 |
|
|
|
63 |
private function traiterRessources() {
|
|
|
64 |
$this->analyserRessources();
|
|
|
65 |
$retour = $this->initialiserService();
|
|
|
66 |
return $retour;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
private function analyserRessources() {
|
|
|
70 |
if ($this->methode == 'consulter') {
|
|
|
71 |
if (count($this->ressources) == 0) {
|
|
|
72 |
$this->sousServiceNom = 'liste-protocoles';
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
if ($this->sousServiceNom == null) {
|
|
|
76 |
$this->lancerMessageErreurRessource();
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private function lancerMessageErreurRessource() {
|
|
|
81 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
82 |
$message = "La ressource demandée '$ressource' ".
|
|
|
83 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
|
|
84 |
"Les URLs disponibles sont : \n".
|
|
|
85 |
" - en GET : protocoles (paramètres : aucun) \n";
|
|
|
86 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
87 |
throw new Exception($message, $code);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
private function initialiserService() {
|
|
|
91 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
|
|
92 |
$chemins = array();
|
|
|
93 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
|
|
94 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
95 |
$retour = '';
|
|
|
96 |
$service = null;
|
|
|
97 |
|
|
|
98 |
foreach ($chemins as $chemin) {
|
|
|
99 |
if (file_exists($chemin)) {
|
|
|
100 |
require_once $chemin;
|
|
|
101 |
$service = new $classe($this->conteneur);
|
|
|
102 |
if ($this->methode == 'consulter') {
|
|
|
103 |
$retour = $service->consulter();
|
|
|
104 |
} else {
|
|
|
105 |
$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
|
|
|
106 |
"ne possède pas de méthode '{$this->methode}' !";
|
|
|
107 |
$code = RestServeur::HTTP_NON_IMPLEMENTE;
|
|
|
108 |
throw new Exception($message, $code);
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
if (is_null($service)) {
|
|
|
114 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
115 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
116 |
"est introuvable par le service '{$this->serviceNom}' !";
|
|
|
117 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
118 |
throw new Exception($message, $code);
|
|
|
119 |
}
|
|
|
120 |
return $retour;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
private function obtenirNomClasseService($mot) {
|
|
|
124 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
125 |
return $classeNom;
|
|
|
126 |
}
|
|
|
127 |
}
|