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