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 |
|
|
|
125 |
$chemins = array();
|
|
|
126 |
$chemins[] = $this->cheminCourrant.$this->projetNom.DS;
|
|
|
127 |
$chemins[] = $this->cheminCourrant.'commun'.DS;
|
|
|
128 |
$chemins[] = Config::get('chemin_bibliotheque');
|
|
|
129 |
|
|
|
130 |
foreach ($chemins as $chemin) {
|
|
|
131 |
$chemin = $chemin.$classe.'.php';
|
|
|
132 |
if (file_exists($chemin)) {
|
|
|
133 |
require_once $chemin;
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
private function avoirRessourceService() {
|
|
|
139 |
$presenceRessourceService = false;
|
|
|
140 |
$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
|
145 |
jpm |
141 |
if (isset($this->ressources[1])) {
|
|
|
142 |
$service = $this->ressources[1];
|
|
|
143 |
if (in_array($service, $servicesDispo)) {
|
|
|
144 |
$presenceRessourceService = true;
|
|
|
145 |
} else {
|
|
|
146 |
$message = "La service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
|
|
|
147 |
"Les services disponibles sont : ".implode(', ', $servicesDispo);
|
|
|
148 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
149 |
throw new Exception($message, $code);
|
|
|
150 |
}
|
3 |
jpm |
151 |
} else {
|
145 |
jpm |
152 |
$message = "Vous n'avez pas indiqué de service pour le projet {$this->projetNom} !\n".
|
3 |
jpm |
153 |
"Les services disponibles sont : ".implode(', ', $servicesDispo);
|
145 |
jpm |
154 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
3 |
jpm |
155 |
throw new Exception($message, $code);
|
|
|
156 |
}
|
|
|
157 |
return $presenceRessourceService;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
private function initialiserService() {
|
|
|
161 |
$this->chargerNomDuService();
|
110 |
jpm |
162 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
3 |
jpm |
163 |
$chemins = array();
|
|
|
164 |
$chemins[] = $this->cheminCourrant.$this->projetNom.DS.$classe.'.php';
|
|
|
165 |
$chemins[] = $this->cheminCourrant.'commun'.DS.$classe.'.php';
|
|
|
166 |
|
|
|
167 |
$retour = '';
|
|
|
168 |
$service = null;
|
|
|
169 |
foreach ($chemins as $chemin) {
|
|
|
170 |
if (file_exists($chemin)) {
|
|
|
171 |
$service = new $classe($this->getBdd());
|
|
|
172 |
$ressourcesPourService = $this->filtrerRessourcesPourService();
|
|
|
173 |
$retour = $service->consulter($ressourcesPourService, $this->parametres);
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
if (is_null($service)) {
|
|
|
177 |
$message = "La service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
|
|
178 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
179 |
throw new Exception($message, $code);
|
|
|
180 |
}
|
|
|
181 |
return $retour;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
private function chargerNomDuService() {
|
|
|
185 |
$this->serviceNom = $this->ressources[1];
|
|
|
186 |
}
|
|
|
187 |
|
110 |
jpm |
188 |
private function obtenirNomClasseService($mot) {
|
3 |
jpm |
189 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
190 |
return $classeNom;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
private function filtrerRessourcesPourService() {
|
|
|
194 |
$ressourcesPourService = array();
|
|
|
195 |
$nbreDeRessources = count($this->ressources);
|
|
|
196 |
for ($i = 2; $i < $nbreDeRessources; $i++) {
|
|
|
197 |
$ressourcesPourService[] = $this->ressources[$i];
|
|
|
198 |
}
|
|
|
199 |
return $ressourcesPourService;
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
?>
|