858 |
gduche |
1 |
<?php
|
|
|
2 |
/**
|
1793 |
jpm |
3 |
* Classe principale de chargement des services concernant les commentaires.
|
|
|
4 |
*
|
|
|
5 |
* Encodage en entrée : utf8
|
|
|
6 |
* Encodage en sortie : utf8
|
|
|
7 |
*
|
|
|
8 |
* URLs possibles :
|
|
|
9 |
*
|
|
|
10 |
* GET :
|
|
|
11 |
* http://localhost/del/services/0.1/commentaires => liste tous les commentaires
|
|
|
12 |
* http://localhost/del/services/0.1/commentaires/#id => retourne le contenu d'un commentaire d'id #id
|
|
|
13 |
*
|
|
|
14 |
* PUT :
|
|
|
15 |
* http://localhost/del/services/0.1/commentaires => Ajoute un nouveau commentaire
|
|
|
16 |
*
|
|
|
17 |
* DELETE :
|
|
|
18 |
* http://localhost/del/services/0.1/commentaires/#id => supprime le commentaire d'id #id
|
|
|
19 |
*
|
|
|
20 |
* @category DEL
|
|
|
21 |
* @package Services
|
|
|
22 |
* @subpackage Commentaires
|
|
|
23 |
* @version 0.1
|
|
|
24 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
|
|
25 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
26 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
|
|
27 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
28 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
29 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
|
|
30 |
*/
|
858 |
gduche |
31 |
class Commentaires extends RestService {
|
|
|
32 |
|
|
|
33 |
private $parametres = array();
|
|
|
34 |
private $ressources = array();
|
|
|
35 |
private $methode = null;
|
1793 |
jpm |
36 |
private $serviceNom = 'commentaires';
|
|
|
37 |
private $sousServiceNom = null;
|
858 |
gduche |
38 |
private $cheminCourant = null;
|
|
|
39 |
|
|
|
40 |
private $conteneur;
|
1793 |
jpm |
41 |
|
858 |
gduche |
42 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
|
|
43 |
protected $utilisationParametresBruts = true;
|
|
|
44 |
|
|
|
45 |
public function __construct() {
|
|
|
46 |
$this->cheminCourant = dirname(__FILE__).DS;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public function consulter($ressources, $parametres) {
|
|
|
50 |
$this->methode = 'consulter';
|
1793 |
jpm |
51 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
52 |
return $this->executerService();
|
858 |
gduche |
53 |
}
|
1793 |
jpm |
54 |
|
|
|
55 |
public function ajouter($ressources, $requeteDonnees) {
|
858 |
gduche |
56 |
$this->methode = 'ajouter';
|
1793 |
jpm |
57 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
58 |
return $this->executerService();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public function supprimer($ressources) {
|
|
|
62 |
$this->methode = 'supprimer';
|
|
|
63 |
$this->initialiserRessourcesEtParametres($ressources);
|
|
|
64 |
return $this->executerService();
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
|
|
68 |
$this->ressources = $ressources;
|
|
|
69 |
$this->parametres = $parametres;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private function executerService() {
|
858 |
gduche |
73 |
$resultat = '';
|
|
|
74 |
$reponseHttp = new ReponseHttp();
|
|
|
75 |
try {
|
|
|
76 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
77 |
$resultat = $this->traiterRessources();
|
|
|
78 |
$reponseHttp->setResultatService($resultat);
|
|
|
79 |
} catch (Exception $e) {
|
|
|
80 |
$reponseHttp->ajouterErreur($e);
|
|
|
81 |
}
|
1793 |
jpm |
82 |
$reponseHttp->emettreLesEntetes();
|
858 |
gduche |
83 |
$corps = $reponseHttp->getCorps();
|
|
|
84 |
return $corps;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
private function traiterRessources() {
|
1793 |
jpm |
88 |
$this->chargerConfigService();
|
|
|
89 |
$this->analyserRessources();
|
858 |
gduche |
90 |
$retour = $this->initialiserService();
|
|
|
91 |
return $retour;
|
|
|
92 |
}
|
|
|
93 |
|
1793 |
jpm |
94 |
private function chargerConfigService() {
|
|
|
95 |
$chemin = Config::get('chemin_configurations')."config_{$this->serviceNom}.ini";
|
|
|
96 |
Config::charger($chemin);
|
858 |
gduche |
97 |
}
|
|
|
98 |
|
1793 |
jpm |
99 |
private function analyserRessources() {
|
|
|
100 |
if ($this->methode == 'consulter') {
|
|
|
101 |
if (!isset($this->ressources) || empty($this->ressources)) {
|
|
|
102 |
$this->sousServiceNom = 'liste-commentaires';
|
|
|
103 |
} else if (isset($this->ressources[0]) && count($this->ressources) == 1 && is_numeric($this->ressources[0])) {
|
|
|
104 |
$this->sousServiceNom = 'consulter-commentaire';
|
|
|
105 |
}
|
|
|
106 |
} else if ($this->methode == 'ajouter') {
|
|
|
107 |
$this->sousServiceNom = 'ajouter-commentaire';
|
|
|
108 |
} else if ($this->methode == 'supprimer') {
|
|
|
109 |
if (isset($this->ressources[0]) && count($this->ressources) == 1 && is_numeric($this->ressources[0])) {
|
|
|
110 |
$this->sousServiceNom = 'supprimer-commentaire';
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
if ($this->sousServiceNom == null) {
|
|
|
115 |
$this->lancerMessageErreurRessource();
|
|
|
116 |
}
|
858 |
gduche |
117 |
}
|
|
|
118 |
|
1793 |
jpm |
119 |
private function lancerMessageErreurRessource() {
|
|
|
120 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
121 |
$message = "La ressource demandée '$ressource' ".
|
|
|
122 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
|
|
123 |
"Les URLs disponibles sont : \n".
|
|
|
124 |
" - en GET : commentaires, commentaires/#id \n".
|
|
|
125 |
" - en PUT : commentaires".
|
|
|
126 |
" - en DELETE : commentaires/#id";
|
|
|
127 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
128 |
throw new Exception($message, $code);
|
|
|
129 |
}
|
|
|
130 |
|
858 |
gduche |
131 |
private function initialiserService() {
|
1793 |
jpm |
132 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
|
|
133 |
//echo $this->sousServiceNom.':'.$classe."\n";
|
|
|
134 |
//echo 'Ressources :'.print_r($this->ressources, true);
|
|
|
135 |
//echo 'Parametres :'.print_r($this->parametres, true);
|
858 |
gduche |
136 |
$chemins = array();
|
1793 |
jpm |
137 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
858 |
gduche |
138 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
139 |
$retour = '';
|
|
|
140 |
$service = null;
|
|
|
141 |
foreach ($chemins as $chemin) {
|
|
|
142 |
if (file_exists($chemin)) {
|
1793 |
jpm |
143 |
$this->conteneur->chargerConfiguration('config_'.$this->serviceNom.'.ini');
|
858 |
gduche |
144 |
require_once $chemin;
|
|
|
145 |
$service = new $classe($this->conteneur);
|
|
|
146 |
if ($this->methode == 'consulter') {
|
|
|
147 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
148 |
} elseif ($this->methode == 'ajouter') {
|
1793 |
jpm |
149 |
$retour = $service->ajouter($this->ressources, $this->parametres);
|
864 |
gduche |
150 |
} elseif ($this->methode == 'supprimer') {
|
1793 |
jpm |
151 |
$retour = $service->supprimer($this->ressources);
|
858 |
gduche |
152 |
}
|
|
|
153 |
}
|
|
|
154 |
}
|
1793 |
jpm |
155 |
|
858 |
gduche |
156 |
if (is_null($service)) {
|
1793 |
jpm |
157 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
158 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
159 |
"n'existe pas dans le service '{$this->serviceNom}' !";
|
858 |
gduche |
160 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
161 |
throw new Exception($message, $code);
|
|
|
162 |
}
|
|
|
163 |
return $retour;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
private function obtenirNomClasseService($mot) {
|
|
|
167 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
168 |
return $classeNom;
|
|
|
169 |
}
|
1793 |
jpm |
170 |
}
|