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;
|
|
|
30 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres brutes. */
|
|
|
31 |
protected $utilisationParametresBruts = true;
|
|
|
32 |
|
|
|
33 |
public function __construct() {
|
|
|
34 |
$this->cheminCourrant = dirname(__FILE__).DS;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public function consulter($ressources, $parametres) {
|
|
|
38 |
$resultat = '';
|
110 |
jpm |
39 |
$reponseHttp = new ReponseHttp();
|
3 |
jpm |
40 |
try {
|
|
|
41 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
42 |
$resultat = $this->traiterRessources();
|
110 |
jpm |
43 |
$reponseHttp->setResultatService($resultat);
|
3 |
jpm |
44 |
} catch (Exception $e) {
|
110 |
jpm |
45 |
$reponseHttp->ajouterErreur($e);
|
3 |
jpm |
46 |
}
|
110 |
jpm |
47 |
$reponseHttp->emettreLesEntetes();
|
|
|
48 |
$corps = $reponseHttp->getCorps();
|
3 |
jpm |
49 |
return $corps;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
private function initialiserRessourcesEtParametres($ressources, $parametres) {
|
|
|
53 |
$this->ressources = $ressources;
|
|
|
54 |
$this->parametres = $parametres;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
private function traiterRessources() {
|
|
|
58 |
$retour = '';
|
|
|
59 |
if ($this->avoirRessources()) {
|
|
|
60 |
if ($this->avoirRessourceProjet()) {
|
|
|
61 |
$this->initialiserProjet();
|
|
|
62 |
if ($this->avoirRessourceService()) {
|
|
|
63 |
$retour = $this->initialiserService();
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
return $retour;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
private function avoirRessources() {
|
|
|
71 |
$presenceDeRessources = false;
|
|
|
72 |
if (isset($this->ressources) && count($this->ressources) > 0) {
|
|
|
73 |
$presenceDeRessources = true;
|
|
|
74 |
} else {
|
|
|
75 |
$message = "Aucune ressource n'a été indiquée.\n".
|
|
|
76 |
"Veuillez indiquer au moins un code de projet et un type de service.";
|
|
|
77 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
78 |
throw new Exception($message, $code);
|
|
|
79 |
}
|
|
|
80 |
return $presenceDeRessources;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
private function avoirRessourceProjet() {
|
|
|
84 |
$presenceRessourceProjet = false;
|
|
|
85 |
$projet = $this->ressources[0];
|
|
|
86 |
$projetsDispo = Outils::recupererTableauConfig('projetsDispo');
|
|
|
87 |
if (in_array($projet, $projetsDispo)) {
|
|
|
88 |
$presenceRessourceProjet = true;
|
|
|
89 |
} else {
|
|
|
90 |
$message = "La ressource '$projet' n'indique pas un projet existant.\n".
|
|
|
91 |
"Les projets existant sont :\n".implode(', ', $projetsDispo);
|
|
|
92 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
93 |
throw new Exception($message, $code);
|
|
|
94 |
}
|
|
|
95 |
return $presenceRessourceProjet;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
private function initialiserProjet() {
|
|
|
99 |
$this->chargerNomDuProjet();
|
|
|
100 |
$this->chargerConfigProjet();
|
145 |
jpm |
101 |
|
|
|
102 |
// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
|
146 |
jpm |
103 |
if (phpversion() < 5.3) {
|
|
|
104 |
spl_autoload_register(array($this, 'chargerClasseProjet'));
|
|
|
105 |
} else {
|
|
|
106 |
spl_autoload_register(array($this, 'chargerClasseProjet'), true , true);
|
|
|
107 |
}
|
3 |
jpm |
108 |
}
|
|
|
109 |
|
|
|
110 |
private function chargerNomDuProjet() {
|
|
|
111 |
$this->projetNom = $this->ressources[0];
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
private function chargerConfigProjet() {
|
|
|
115 |
$projet = $this->projetNom;
|
|
|
116 |
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
|
|
|
117 |
Config::charger($chemin);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
private function chargerClasseProjet($classe) {
|
|
|
121 |
if (class_exists($classe)) {
|
|
|
122 |
return null;
|
|
|
123 |
}
|
|
|
124 |
|
274 |
jpm |
125 |
$cheminBiblio = Config::get('chemin_bibliotheque');
|
3 |
jpm |
126 |
$chemins = array();
|
|
|
127 |
$chemins[] = $this->cheminCourrant.$this->projetNom.DS;
|
|
|
128 |
$chemins[] = $this->cheminCourrant.'commun'.DS;
|
274 |
jpm |
129 |
$chemins[] = $cheminBiblio;
|
|
|
130 |
$chemins[] = $cheminBiblio.'robots'.DS;
|
3 |
jpm |
131 |
|
|
|
132 |
foreach ($chemins as $chemin) {
|
|
|
133 |
$chemin = $chemin.$classe.'.php';
|
|
|
134 |
if (file_exists($chemin)) {
|
|
|
135 |
require_once $chemin;
|
245 |
jpm |
136 |
break;
|
3 |
jpm |
137 |
}
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
private function avoirRessourceService() {
|
|
|
142 |
$presenceRessourceService = false;
|
|
|
143 |
$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
|
145 |
jpm |
144 |
if (isset($this->ressources[1])) {
|
|
|
145 |
$service = $this->ressources[1];
|
|
|
146 |
if (in_array($service, $servicesDispo)) {
|
|
|
147 |
$presenceRessourceService = true;
|
|
|
148 |
} else {
|
|
|
149 |
$message = "La service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
|
|
|
150 |
"Les services disponibles sont : ".implode(', ', $servicesDispo);
|
|
|
151 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
152 |
throw new Exception($message, $code);
|
|
|
153 |
}
|
3 |
jpm |
154 |
} else {
|
145 |
jpm |
155 |
$message = "Vous n'avez pas indiqué de service pour le projet {$this->projetNom} !\n".
|
3 |
jpm |
156 |
"Les services disponibles sont : ".implode(', ', $servicesDispo);
|
145 |
jpm |
157 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
3 |
jpm |
158 |
throw new Exception($message, $code);
|
|
|
159 |
}
|
|
|
160 |
return $presenceRessourceService;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
private function initialiserService() {
|
|
|
164 |
$this->chargerNomDuService();
|
110 |
jpm |
165 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
3 |
jpm |
166 |
$chemins = array();
|
|
|
167 |
$chemins[] = $this->cheminCourrant.$this->projetNom.DS.$classe.'.php';
|
|
|
168 |
$chemins[] = $this->cheminCourrant.'commun'.DS.$classe.'.php';
|
|
|
169 |
|
|
|
170 |
$retour = '';
|
|
|
171 |
$service = null;
|
|
|
172 |
foreach ($chemins as $chemin) {
|
|
|
173 |
if (file_exists($chemin)) {
|
|
|
174 |
$service = new $classe($this->getBdd());
|
|
|
175 |
$ressourcesPourService = $this->filtrerRessourcesPourService();
|
|
|
176 |
$retour = $service->consulter($ressourcesPourService, $this->parametres);
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
if (is_null($service)) {
|
|
|
180 |
$message = "La service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
|
|
181 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
182 |
throw new Exception($message, $code);
|
|
|
183 |
}
|
|
|
184 |
return $retour;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
private function chargerNomDuService() {
|
|
|
188 |
$this->serviceNom = $this->ressources[1];
|
|
|
189 |
}
|
|
|
190 |
|
110 |
jpm |
191 |
private function obtenirNomClasseService($mot) {
|
3 |
jpm |
192 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
193 |
return $classeNom;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
private function filtrerRessourcesPourService() {
|
|
|
197 |
$ressourcesPourService = array();
|
|
|
198 |
$nbreDeRessources = count($this->ressources);
|
|
|
199 |
for ($i = 2; $i < $nbreDeRessources; $i++) {
|
|
|
200 |
$ressourcesPourService[] = $this->ressources[$i];
|
|
|
201 |
}
|
|
|
202 |
return $ressourcesPourService;
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
?>
|