3 |
jpm |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Description :
|
|
|
4 |
* Classe principale de chargement des services d'eFlore.
|
|
|
5 |
*
|
|
|
6 |
* Encodage en entrée : utf8
|
|
|
7 |
* Encodage en sortie : utf8
|
|
|
8 |
* @package eflore-projets
|
|
|
9 |
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
|
|
|
10 |
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
|
|
|
11 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
12 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
13 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
14 |
* @version 0.1
|
|
|
15 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
|
|
|
16 |
*/
|
|
|
17 |
class Projets extends RestService {
|
|
|
18 |
|
|
|
19 |
/** Contients les paramètres.*/
|
|
|
20 |
private $parametres = array();
|
|
|
21 |
/** Contients les ressources.*/
|
|
|
22 |
private $ressources = array();
|
|
|
23 |
|
|
|
24 |
/** Nom du projet courrant. */
|
|
|
25 |
private $projetNom = array();
|
|
|
26 |
/** Nom du service demandé. */
|
|
|
27 |
private $serviceNom = array();
|
|
|
28 |
/** Chemin vers le dossier courrant. */
|
|
|
29 |
private $cheminCourrant = null;
|
791 |
raphael |
30 |
private $classe = null;
|
1217 |
delphine |
31 |
|
|
|
32 |
private $methode = "consulter";
|
653 |
jpm |
33 |
|
536 |
gduche |
34 |
private $cache;
|
653 |
jpm |
35 |
|
3 |
jpm |
36 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres brutes. */
|
|
|
37 |
protected $utilisationParametresBruts = true;
|
|
|
38 |
|
|
|
39 |
public function __construct() {
|
|
|
40 |
$this->cheminCourrant = dirname(__FILE__).DS;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public function consulter($ressources, $parametres) {
|
|
|
44 |
$resultat = '';
|
110 |
jpm |
45 |
$reponseHttp = new ReponseHttp();
|
3 |
jpm |
46 |
try {
|
|
|
47 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
48 |
$resultat = $this->traiterRessources();
|
110 |
jpm |
49 |
$reponseHttp->setResultatService($resultat);
|
3 |
jpm |
50 |
} catch (Exception $e) {
|
110 |
jpm |
51 |
$reponseHttp->ajouterErreur($e);
|
3 |
jpm |
52 |
}
|
945 |
raphael |
53 |
if(strpos($_SERVER['SCRIPT_NAME'], 'phpunit') === FALSE) $reponseHttp->emettreLesEntetes();
|
110 |
jpm |
54 |
$corps = $reponseHttp->getCorps();
|
3 |
jpm |
55 |
return $corps;
|
|
|
56 |
}
|
1217 |
delphine |
57 |
|
|
|
58 |
public function modifier($ressources, $parametres) {
|
|
|
59 |
$resultat = '';
|
|
|
60 |
$reponseHttp = new ReponseHttp();
|
|
|
61 |
$this->methode = "modifier";
|
|
|
62 |
try {
|
|
|
63 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
64 |
$resultat = $this->traiterRessources();
|
|
|
65 |
$reponseHttp->setResultatService($resultat);
|
|
|
66 |
} catch (Exception $e) {
|
|
|
67 |
$reponseHttp->ajouterErreur($e);
|
|
|
68 |
}
|
|
|
69 |
if(strpos($_SERVER['SCRIPT_NAME'], 'phpunit') === FALSE) $reponseHttp->emettreLesEntetes();
|
|
|
70 |
$corps = $reponseHttp->getCorps();
|
|
|
71 |
return $corps;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public function ajouter($ressources, $parametres) {
|
|
|
75 |
$resultat = '';
|
|
|
76 |
$reponseHttp = new ReponseHttp();
|
|
|
77 |
$this->methode = "ajouter";
|
|
|
78 |
try {
|
|
|
79 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
80 |
$resultat = $this->traiterRessources();
|
|
|
81 |
$reponseHttp->setResultatService($resultat);
|
|
|
82 |
} catch (Exception $e) {
|
|
|
83 |
$reponseHttp->ajouterErreur($e);
|
|
|
84 |
}
|
|
|
85 |
if(strpos($_SERVER['SCRIPT_NAME'], 'phpunit') === FALSE) $reponseHttp->emettreLesEntetes();
|
|
|
86 |
$corps = $reponseHttp->getCorps();
|
|
|
87 |
return $corps;
|
|
|
88 |
}
|
3 |
jpm |
89 |
|
|
|
90 |
private function initialiserRessourcesEtParametres($ressources, $parametres) {
|
|
|
91 |
$this->ressources = $ressources;
|
|
|
92 |
$this->parametres = $parametres;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
private function traiterRessources() {
|
|
|
96 |
$retour = '';
|
|
|
97 |
if ($this->avoirRessources()) {
|
|
|
98 |
if ($this->avoirRessourceProjet()) {
|
791 |
raphael |
99 |
$this->chargerNomDuService(); // défini $this->serviceNom
|
|
|
100 |
$this->initialiserProjet(); // autoload defined here
|
3 |
jpm |
101 |
if ($this->avoirRessourceService()) {
|
792 |
raphael |
102 |
$this->classe = self::debusquerClasse($this->projetNom, $this->serviceNom);
|
3 |
jpm |
103 |
$retour = $this->initialiserService();
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
return $retour;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
private function avoirRessources() {
|
|
|
111 |
$presenceDeRessources = false;
|
|
|
112 |
if (isset($this->ressources) && count($this->ressources) > 0) {
|
|
|
113 |
$presenceDeRessources = true;
|
|
|
114 |
} else {
|
|
|
115 |
$message = "Aucune ressource n'a été indiquée.\n".
|
|
|
116 |
"Veuillez indiquer au moins un code de projet et un type de service.";
|
|
|
117 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
118 |
throw new Exception($message, $code);
|
|
|
119 |
}
|
|
|
120 |
return $presenceDeRessources;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
private function avoirRessourceProjet() {
|
|
|
124 |
$presenceRessourceProjet = false;
|
|
|
125 |
$projet = $this->ressources[0];
|
|
|
126 |
$projetsDispo = Outils::recupererTableauConfig('projetsDispo');
|
|
|
127 |
if (in_array($projet, $projetsDispo)) {
|
|
|
128 |
$presenceRessourceProjet = true;
|
|
|
129 |
} else {
|
|
|
130 |
$message = "La ressource '$projet' n'indique pas un projet existant.\n".
|
|
|
131 |
"Les projets existant sont :\n".implode(', ', $projetsDispo);
|
|
|
132 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
133 |
throw new Exception($message, $code);
|
|
|
134 |
}
|
|
|
135 |
return $presenceRessourceProjet;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
private function initialiserProjet() {
|
|
|
139 |
$this->chargerNomDuProjet();
|
|
|
140 |
$this->chargerConfigProjet();
|
145 |
jpm |
141 |
|
|
|
142 |
// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
|
146 |
jpm |
143 |
if (phpversion() < 5.3) {
|
|
|
144 |
spl_autoload_register(array($this, 'chargerClasseProjet'));
|
|
|
145 |
} else {
|
|
|
146 |
spl_autoload_register(array($this, 'chargerClasseProjet'), true , true);
|
|
|
147 |
}
|
3 |
jpm |
148 |
}
|
|
|
149 |
|
|
|
150 |
private function chargerNomDuProjet() {
|
|
|
151 |
$this->projetNom = $this->ressources[0];
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
private function chargerConfigProjet() {
|
|
|
155 |
$projet = $this->projetNom;
|
|
|
156 |
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
|
|
|
157 |
Config::charger($chemin);
|
|
|
158 |
}
|
|
|
159 |
|
791 |
raphael |
160 |
/*
|
|
|
161 |
1) jusqu'à présent:
|
|
|
162 |
* le principe pour URL = a/b est: de charger
|
|
|
163 |
* require_once($chemin/a/ucfirst(c).php)
|
|
|
164 |
* new ucfirst(c); ucfirst(c)->consulter()
|
|
|
165 |
* sachant que ucfirst(c).php et la classe ucfirst(c) apparaîssent à de multiples emplacements (selon a)
|
|
|
166 |
|
|
|
167 |
1") Beurk... (php-className conflicts en PHP 5.2)
|
|
|
168 |
|
|
|
169 |
Ici nous faisons des cas particuliers pour Ontologies, mais en suivant ce principe, sont affectés:
|
811 |
raphael |
170 |
Images, Informations, InformationsTaxonsSup,
|
791 |
raphael |
171 |
LegendeCartes, NomCommune, Noms, NomsVernaculaires, Projets, Statuts,
|
|
|
172 |
Taxons, TaxonsCartes, Textes, ZoneGeo
|
|
|
173 |
|
|
|
174 |
cf:
|
|
|
175 |
$ grep -r '^[cC]lass '|grep -F '.php:'|egrep -w " \($(grep -rh '^[cC]lass '|awk '{print $2}'|sort|uniq -d|tr "\n" '|')\) " \
|
|
|
176 |
|sort -k2
|
|
|
177 |
|
|
|
178 |
PS: "Using two class with the same name"
|
|
|
179 |
http://stackoverflow.com/questions/4555186/using-two-class-with-the-same-name
|
|
|
180 |
> Stop.
|
|
|
181 |
> Whatever you are doing is wrong. Backup. Re-evaluate what you are doing and why.
|
|
|
182 |
*/
|
792 |
raphael |
183 |
private static function debusquerClasse($p, $s) {
|
811 |
raphael |
184 |
if($s == 'ontologies') {
|
|
|
185 |
switch($p) {
|
|
|
186 |
case 'baseflor':
|
|
|
187 |
return 'BaseFloreOntologies';
|
|
|
188 |
case 'eflore':
|
|
|
189 |
return 'EfloreOntologies';
|
|
|
190 |
case 'chorodep':
|
|
|
191 |
return 'ChorodepOntologies';
|
|
|
192 |
case 'baseveg':
|
|
|
193 |
return 'BasevegOntologies';
|
|
|
194 |
case 'moissonnage':
|
814 |
raphael |
195 |
return 'MoissonnageOntologies';
|
811 |
raphael |
196 |
case 'commun':
|
|
|
197 |
return 'Ontologies';
|
|
|
198 |
}
|
|
|
199 |
}
|
791 |
raphael |
200 |
|
811 |
raphael |
201 |
if($s == 'cartes') {
|
|
|
202 |
switch($p) {
|
|
|
203 |
case 'bdtxa':
|
|
|
204 |
return 'BdtxaCartes';
|
|
|
205 |
case 'eflore':
|
|
|
206 |
return 'EfloreCartes';
|
|
|
207 |
case 'chorodep':
|
|
|
208 |
return 'ChorodepCartes';
|
|
|
209 |
case 'moissonnage':
|
814 |
raphael |
210 |
return 'MoissonnageCartes';
|
1012 |
aurelien |
211 |
case 'sophy':
|
|
|
212 |
return 'SophyCartes';
|
811 |
raphael |
213 |
}
|
791 |
raphael |
214 |
}
|
811 |
raphael |
215 |
|
791 |
raphael |
216 |
return NULL;
|
|
|
217 |
}
|
|
|
218 |
|
3 |
jpm |
219 |
private function chargerClasseProjet($classe) {
|
|
|
220 |
if (class_exists($classe)) {
|
|
|
221 |
return null;
|
|
|
222 |
}
|
|
|
223 |
|
791 |
raphael |
224 |
if($this->serviceNom == 'ontologies') {
|
|
|
225 |
$c = NULL;
|
|
|
226 |
switch($this->projetNom) {
|
|
|
227 |
case 'baseflor':
|
|
|
228 |
$c = 'BaseFloreOntologies';
|
|
|
229 |
break;
|
|
|
230 |
case 'eflore':
|
|
|
231 |
$c = 'EfloreOntologies';
|
|
|
232 |
break;
|
|
|
233 |
case 'chorodep':
|
|
|
234 |
$c = 'ChorodepOntologies';
|
|
|
235 |
break;
|
|
|
236 |
case 'baseveg':
|
|
|
237 |
$c = 'BasevegOntologies';
|
|
|
238 |
break;
|
|
|
239 |
case 'moissonnage':
|
814 |
raphael |
240 |
$c = 'MoissonnageOntologies';
|
791 |
raphael |
241 |
break;
|
|
|
242 |
case 'commun':
|
|
|
243 |
$c = 'Ontologies';
|
|
|
244 |
break;
|
|
|
245 |
}
|
|
|
246 |
if($c) {
|
|
|
247 |
require_once($this->cheminCourrant . 'commun' . DS . 'Commun.php');
|
|
|
248 |
require_once($this->cheminCourrant . $this->projetNom . DS . $this->obtenirNomClasseService($this->serviceNom) . '.php');
|
|
|
249 |
return;
|
|
|
250 |
}
|
|
|
251 |
}
|
|
|
252 |
|
811 |
raphael |
253 |
// problème de class-name conflict. Exemple:
|
|
|
254 |
// phpunit --verbose --debug --filter 'ChorodepCartesTest::testCarteGenerique|EfloreCartesTest::testCarteGenerale'
|
|
|
255 |
if($this->serviceNom == 'cartes') {
|
|
|
256 |
$c = NULL;
|
|
|
257 |
switch($this->projetNom) {
|
|
|
258 |
case 'bdtxa':
|
|
|
259 |
$c = 'BdtxaCartes';
|
|
|
260 |
break;
|
|
|
261 |
case 'eflore':
|
|
|
262 |
$c = 'EfloreCartes';
|
|
|
263 |
break;
|
|
|
264 |
case 'chorodep':
|
|
|
265 |
$c = 'ChorodepCartes';
|
|
|
266 |
break;
|
815 |
raphael |
267 |
case 'moissonnage':
|
814 |
raphael |
268 |
$c = 'MoissonnageCartes';
|
811 |
raphael |
269 |
break;
|
1012 |
aurelien |
270 |
case 'sophy':
|
|
|
271 |
$c = 'SophyCartes';
|
|
|
272 |
break;
|
811 |
raphael |
273 |
}
|
|
|
274 |
if($c) {
|
|
|
275 |
require_once($this->cheminCourrant . 'commun' . DS . 'Commun.php');
|
|
|
276 |
require_once($this->cheminCourrant . $this->projetNom . DS . $this->obtenirNomClasseService($this->serviceNom) . '.php');
|
|
|
277 |
return;
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
|
274 |
jpm |
281 |
$cheminBiblio = Config::get('chemin_bibliotheque');
|
3 |
jpm |
282 |
$chemins = array();
|
|
|
283 |
$chemins[] = $this->cheminCourrant.$this->projetNom.DS;
|
|
|
284 |
$chemins[] = $this->cheminCourrant.'commun'.DS;
|
274 |
jpm |
285 |
$chemins[] = $cheminBiblio;
|
|
|
286 |
$chemins[] = $cheminBiblio.'robots'.DS;
|
3 |
jpm |
287 |
|
|
|
288 |
foreach ($chemins as $chemin) {
|
|
|
289 |
$chemin = $chemin.$classe.'.php';
|
|
|
290 |
if (file_exists($chemin)) {
|
|
|
291 |
require_once $chemin;
|
245 |
jpm |
292 |
break;
|
3 |
jpm |
293 |
}
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
private function avoirRessourceService() {
|
|
|
298 |
$presenceRessourceService = false;
|
|
|
299 |
$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
|
145 |
jpm |
300 |
if (isset($this->ressources[1])) {
|
|
|
301 |
$service = $this->ressources[1];
|
|
|
302 |
if (in_array($service, $servicesDispo)) {
|
|
|
303 |
$presenceRessourceService = true;
|
|
|
304 |
} else {
|
957 |
aurelien |
305 |
$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
|
145 |
jpm |
306 |
"Les services disponibles sont : ".implode(', ', $servicesDispo);
|
|
|
307 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
308 |
throw new Exception($message, $code);
|
|
|
309 |
}
|
3 |
jpm |
310 |
} else {
|
145 |
jpm |
311 |
$message = "Vous n'avez pas indiqué de service pour le projet {$this->projetNom} !\n".
|
3 |
jpm |
312 |
"Les services disponibles sont : ".implode(', ', $servicesDispo);
|
145 |
jpm |
313 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
3 |
jpm |
314 |
throw new Exception($message, $code);
|
|
|
315 |
}
|
|
|
316 |
return $presenceRessourceService;
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
private function initialiserService() {
|
791 |
raphael |
320 |
if($this->classe) {
|
|
|
321 |
$classe = $this->classe;
|
|
|
322 |
$service = new $classe($this->getBdd());
|
852 |
raphael |
323 |
return $service->consulter($this->filtrerRessourcesPourService(), $this->parametres, $this->getBdd());
|
791 |
raphael |
324 |
}
|
110 |
jpm |
325 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
3 |
jpm |
326 |
$chemins = array();
|
|
|
327 |
$chemins[] = $this->cheminCourrant.$this->projetNom.DS.$classe.'.php';
|
|
|
328 |
$chemins[] = $this->cheminCourrant.'commun'.DS.$classe.'.php';
|
|
|
329 |
|
|
|
330 |
$service = null;
|
|
|
331 |
foreach ($chemins as $chemin) {
|
|
|
332 |
if (file_exists($chemin)) {
|
|
|
333 |
$service = new $classe($this->getBdd());
|
811 |
raphael |
334 |
// Affichage utile lors de PHPUnit pour détecter les conflits d'autoload de classes de même nom
|
|
|
335 |
// $reflector = new ReflectionClass($classe);
|
|
|
336 |
// printf("===> Projets init classe '%s' depuis '%s', mais provenant de '%s'\n", $classe, $chemin, $reflector->getFileName());
|
3 |
jpm |
337 |
$ressourcesPourService = $this->filtrerRessourcesPourService();
|
1217 |
delphine |
338 |
$methode = $this->methode;
|
|
|
339 |
return $service->$methode($ressourcesPourService, $this->parametres, $this->getBdd());
|
3 |
jpm |
340 |
}
|
|
|
341 |
}
|
|
|
342 |
if (is_null($service)) {
|
957 |
aurelien |
343 |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
3 |
jpm |
344 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
345 |
throw new Exception($message, $code);
|
|
|
346 |
}
|
791 |
raphael |
347 |
return NULL;
|
3 |
jpm |
348 |
}
|
|
|
349 |
|
|
|
350 |
private function chargerNomDuService() {
|
|
|
351 |
$this->serviceNom = $this->ressources[1];
|
|
|
352 |
}
|
|
|
353 |
|
110 |
jpm |
354 |
private function obtenirNomClasseService($mot) {
|
791 |
raphael |
355 |
return str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
3 |
jpm |
356 |
}
|
|
|
357 |
|
|
|
358 |
private function filtrerRessourcesPourService() {
|
|
|
359 |
$ressourcesPourService = array();
|
|
|
360 |
$nbreDeRessources = count($this->ressources);
|
|
|
361 |
for ($i = 2; $i < $nbreDeRessources; $i++) {
|
|
|
362 |
$ressourcesPourService[] = $this->ressources[$i];
|
|
|
363 |
}
|
|
|
364 |
return $ressourcesPourService;
|
|
|
365 |
}
|
|
|
366 |
}
|
|
|
367 |
?>
|