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