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