20 |
delphine |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
class Moissonnage extends RestService {
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
private $parametres = array();
|
|
|
8 |
private $ressources = array();
|
|
|
9 |
|
|
|
10 |
private $squeletteDossier = '';
|
|
|
11 |
|
|
|
12 |
private $nomService = '';
|
|
|
13 |
private $nomSource = '';
|
|
|
14 |
|
|
|
15 |
private $cache;
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
public function __construct() {
|
|
|
19 |
$this->squeletteDossier = dirname(__FILE__) . DIRECTORY_SEPARATOR;
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
public function consulter($ressources, $parametres) {
|
|
|
24 |
$resultat = '';
|
|
|
25 |
$reponseHttp = new ReponseHttp();
|
|
|
26 |
try {
|
|
|
27 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
28 |
$resultat = $this->traiterRessources();
|
|
|
29 |
$reponseHttp->setResultatService($resultat);
|
|
|
30 |
} catch (Exception $e) {
|
|
|
31 |
$reponseHttp->ajouterErreur($e);
|
|
|
32 |
}
|
|
|
33 |
$reponseHttp->emettreLesEntetes();
|
|
|
34 |
$corps = $reponseHttp->getCorps();
|
|
|
35 |
return $corps;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
private function initialiserRessourcesEtParametres($ressources, $parametres) {
|
|
|
39 |
$this->ressources = $ressources;
|
|
|
40 |
$this->parametres = $parametres;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
private function traiterRessources() {
|
|
|
44 |
$retour = '';
|
|
|
45 |
if ($this->avoirRessources()) {
|
|
|
46 |
if ($this->avoirRessourceSource()) {
|
|
|
47 |
$this->initialiserSource();
|
|
|
48 |
if ($this->avoirRessourceService()) {
|
|
|
49 |
$retour = $this->initialiserService();
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
return $retour;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
private function avoirRessources() {
|
|
|
57 |
$presenceDeRessources = false;
|
|
|
58 |
if (isset($this->ressources) && count($this->ressources) > 0) {
|
|
|
59 |
$presenceDeRessources = true;
|
|
|
60 |
} else {
|
|
|
61 |
$message = "Accès au service refusé : aucune ressource indiquée.\n"
|
|
|
62 |
. "Veuillez indiquer au moins une source de données à interroger"
|
|
|
63 |
. " et le type informations a renvoyer.";
|
|
|
64 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
65 |
throw new Exception($message, $code);
|
|
|
66 |
}
|
|
|
67 |
return $presenceDeRessources;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
private function avoirRessourceSource() {
|
|
|
71 |
$presenceRessourceDemandee = false;
|
|
|
72 |
$source = $this->ressources[0];
|
|
|
73 |
$sourcesDispo = $this->recupererTableauConfig('sourcesDispo');
|
|
|
74 |
if (in_array($source, $sourcesDispo)) {
|
|
|
75 |
$presenceRessourceDemandee = true;
|
|
|
76 |
} else {
|
|
|
77 |
$message = "La source de données '{$source}' n'est pas référencée dans nos services.\n"
|
|
|
78 |
. "Les sources disponibles sont les suivantes : " . implode(', ', $sourcesDispo) . ".\n";
|
|
|
79 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
80 |
throw new Exception($message, $code);
|
|
|
81 |
}
|
|
|
82 |
return $presenceRessourceDemandee;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
private function recupererTableauConfig($parametres) {
|
|
|
86 |
$tableau = array();
|
|
|
87 |
$tableauPartiel = explode(',', Config::get($parametres));
|
|
|
88 |
$tableauPartiel = array_map('trim', $tableauPartiel);
|
|
|
89 |
foreach ($tableauPartiel as $champ) {
|
|
|
90 |
if (strpos($champ, '=') === false) {
|
|
|
91 |
$tableau[] = $champ;
|
|
|
92 |
} else {
|
|
|
93 |
list($cle, $val) = explode('=', $champ);
|
|
|
94 |
$clePropre = trim($cle);
|
|
|
95 |
$valeurPropre = trim($val);
|
|
|
96 |
$tableau[$clePropre] = $valeurPropre;
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
return $tableau;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
private function initialiserSource() {
|
|
|
103 |
$this->chargerNomSource();
|
|
|
104 |
$this->chargerConfigSource();
|
|
|
105 |
|
|
|
106 |
// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
|
|
|
107 |
if (phpversion() < 5.3) {
|
|
|
108 |
spl_autoload_register(array($this, 'chargerClasseSource'));
|
|
|
109 |
} else {
|
|
|
110 |
spl_autoload_register(array($this, 'chargerClasseSource'), true , true);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
private function chargerNomSource() {
|
|
|
115 |
$this->nomSource = $this->ressources[0];
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
private function chargerConfigSource() {
|
|
|
119 |
$source = $this->nomSource;
|
|
|
120 |
$chemin = Config::get('chemin_configurations')."config_$source.ini";
|
|
|
121 |
Config::charger($chemin);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
private function chargerClasseSource($classe) {
|
|
|
125 |
if (class_exists($classe)) {
|
|
|
126 |
return null;
|
|
|
127 |
}
|
|
|
128 |
$cheminBiblio = Config::get('chemin_bibliotheque');
|
|
|
129 |
$chemins = array();
|
|
|
130 |
$chemins[] = $this->squeletteDossier . $this->nomSource . DIRECTORY_SEPARATOR;
|
|
|
131 |
$chemins[] = $this->squeletteDossier . 'commun' . DIRECTORY_SEPARATOR;
|
|
|
132 |
$chemins[] = $cheminBiblio;
|
|
|
133 |
$chemins[] = $cheminBiblio . 'robots' . DIRECTORY_SEPARATOR;
|
|
|
134 |
|
|
|
135 |
foreach ($chemins as $chemin) {
|
|
|
136 |
$chemin = $chemin . $classe . '.php';
|
|
|
137 |
if (file_exists($chemin)) {
|
|
|
138 |
require_once $chemin;
|
|
|
139 |
break;
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
private function avoirRessourceService() {
|
|
|
145 |
$presenceRessourceService = false;
|
|
|
146 |
$servicesDispo = $this->recupererTableauConfig('servicesDispo');
|
|
|
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 "
|
|
|
153 |
. $this->nomSource . " !\n"
|
|
|
154 |
. "Les services disponibles sont : " . implode(', ', $servicesDispo);
|
|
|
155 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
156 |
throw new Exception($message, $code);
|
|
|
157 |
}
|
|
|
158 |
} else {
|
|
|
159 |
$message = "Vous n'avez pas indiqué de service pour le projet {$this->nomSource} !\n"
|
|
|
160 |
. "Les services disponibles sont : " . implode(', ', $servicesDispo);
|
|
|
161 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
162 |
throw new Exception($message, $code);
|
|
|
163 |
}
|
|
|
164 |
return $presenceRessourceService;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
private function initialiserService() {
|
|
|
168 |
$this->chargerNomDuService();
|
|
|
169 |
$classe = $this->obtenirNomClasseService($this->nomService);
|
|
|
170 |
$chemins = array();
|
|
|
171 |
$chemins[] = $this->squeletteDossier . $this->nomSource . DS . $classe . '.php';
|
|
|
172 |
$chemins[] = $this->squeletteDossier . 'commun'.DS.$classe.'.php';
|
|
|
173 |
|
|
|
174 |
$retour = '';
|
|
|
175 |
$service = null;
|
|
|
176 |
foreach ($chemins as $chemin) {
|
|
|
177 |
if (file_exists($chemin)) {
|
|
|
178 |
$service = new $classe($this->getBdd());
|
|
|
179 |
$ressourcesPourService = $this->filtrerRessourcesPourService();
|
|
|
180 |
$this->cache = new CacheMoissonnage($service, $this->nomSource, $this->nomService,
|
|
|
181 |
Config::get('cache'));
|
|
|
182 |
$retour = $this->cache->consulter($ressourcesPourService, $this->parametres);
|
|
|
183 |
if (get_class($retour) == 'Exception') {
|
|
|
184 |
throw new Exception($retour->getMessage(), RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
if (is_null($service)) {
|
|
|
189 |
$message = "La service demandé '{$this->nomService}' n'existe pas dans le projet {$this->nomSource} !";
|
|
|
190 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
191 |
throw new Exception($message, $code);
|
|
|
192 |
}
|
|
|
193 |
return $retour;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
private function chargerNomDuService() {
|
|
|
197 |
$this->nomService = $this->ressources[1];
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
private function obtenirNomClasseService($mot) {
|
|
|
201 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
202 |
return $classeNom;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
private function filtrerRessourcesPourService() {
|
|
|
206 |
$ressourcesPourService = array();
|
|
|
207 |
$nbreDeRessources = count($this->ressources);
|
|
|
208 |
for ($i = 2; $i < $nbreDeRessources; $i++) {
|
|
|
209 |
$ressourcesPourService[] = $this->ressources[$i];
|
|
|
210 |
}
|
|
|
211 |
return $ressourcesPourService;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
?>
|