813 |
aurelien |
1 |
<?php
|
1817 |
jpm |
2 |
// declare(encoding='UTF-8');
|
813 |
aurelien |
3 |
/**
|
1795 |
jpm |
4 |
* Classe principale de chargement des sous-services de Protocoles.
|
|
|
5 |
*
|
1817 |
jpm |
6 |
* Urls possibles :
|
|
|
7 |
* http://localhost/service:del:0.1/protocoles => tous les protocoles
|
|
|
8 |
*
|
1795 |
jpm |
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 |
*/
|
813 |
aurelien |
20 |
class Protocoles extends RestService {
|
|
|
21 |
|
|
|
22 |
private $parametres = array();
|
|
|
23 |
private $ressources = array();
|
|
|
24 |
private $methode = null;
|
1817 |
jpm |
25 |
private $serviceNom = 'protocoles';
|
|
|
26 |
private $sousServiceNom = null;
|
813 |
aurelien |
27 |
private $cheminCourant = null;
|
|
|
28 |
|
|
|
29 |
private $conteneur;
|
1795 |
jpm |
30 |
|
813 |
aurelien |
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';
|
1817 |
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() {
|
813 |
aurelien |
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 |
}
|
1795 |
jpm |
62 |
|
813 |
aurelien |
63 |
private function traiterRessources() {
|
1817 |
jpm |
64 |
$this->analyserRessources();
|
|
|
65 |
$retour = $this->initialiserService();
|
813 |
aurelien |
66 |
return $retour;
|
|
|
67 |
}
|
1795 |
jpm |
68 |
|
1817 |
jpm |
69 |
private function analyserRessources() {
|
|
|
70 |
if ($this->methode == 'consulter') {
|
|
|
71 |
if (count($this->ressources) == 0) {
|
|
|
72 |
$this->sousServiceNom = 'liste-protocoles';
|
813 |
aurelien |
73 |
}
|
|
|
74 |
}
|
1817 |
jpm |
75 |
if ($this->sousServiceNom == null) {
|
|
|
76 |
$this->lancerMessageErreurRessource();
|
|
|
77 |
}
|
813 |
aurelien |
78 |
}
|
1795 |
jpm |
79 |
|
1817 |
jpm |
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);
|
813 |
aurelien |
88 |
}
|
|
|
89 |
|
|
|
90 |
private function initialiserService() {
|
1817 |
jpm |
91 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
813 |
aurelien |
92 |
$chemins = array();
|
1817 |
jpm |
93 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
813 |
aurelien |
94 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
95 |
$retour = '';
|
|
|
96 |
$service = null;
|
1817 |
jpm |
97 |
|
813 |
aurelien |
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') {
|
1817 |
jpm |
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);
|
813 |
aurelien |
109 |
}
|
|
|
110 |
}
|
|
|
111 |
}
|
1795 |
jpm |
112 |
|
813 |
aurelien |
113 |
if (is_null($service)) {
|
1817 |
jpm |
114 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
115 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
116 |
"est introuvable par le service '{$this->serviceNom}' !";
|
813 |
aurelien |
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 |
}
|
1817 |
jpm |
127 |
}
|