Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 945 | Rev 1012 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
791 raphael 30
	private $classe = null;
653 jpm 31
 
536 gduche 32
	private $cache;
653 jpm 33
 
3 jpm 34
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres brutes. */
35
	protected $utilisationParametresBruts = true;
36
 
37
	public function __construct() {
38
		$this->cheminCourrant = dirname(__FILE__).DS;
39
	}
40
 
41
	public function consulter($ressources, $parametres) {
42
		$resultat = '';
110 jpm 43
		$reponseHttp = new ReponseHttp();
3 jpm 44
		try {
45
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
46
			$resultat = $this->traiterRessources();
110 jpm 47
			$reponseHttp->setResultatService($resultat);
3 jpm 48
		} catch (Exception $e) {
110 jpm 49
			$reponseHttp->ajouterErreur($e);
3 jpm 50
		}
945 raphael 51
		if(strpos($_SERVER['SCRIPT_NAME'], 'phpunit') === FALSE) $reponseHttp->emettreLesEntetes();
110 jpm 52
		$corps = $reponseHttp->getCorps();
3 jpm 53
		return $corps;
54
	}
55
 
56
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
57
		$this->ressources = $ressources;
58
		$this->parametres = $parametres;
59
	}
60
 
61
	private function traiterRessources() {
62
		$retour = '';
63
		if ($this->avoirRessources()) {
64
			if ($this->avoirRessourceProjet()) {
791 raphael 65
				$this->chargerNomDuService(); // défini $this->serviceNom
66
				$this->initialiserProjet(); // autoload defined here
3 jpm 67
				if ($this->avoirRessourceService()) {
792 raphael 68
					$this->classe = self::debusquerClasse($this->projetNom, $this->serviceNom);
3 jpm 69
					$retour = $this->initialiserService();
70
				}
71
			}
72
		}
73
		return $retour;
74
	}
75
 
76
	private function avoirRessources() {
77
		$presenceDeRessources = false;
78
		if (isset($this->ressources) && count($this->ressources) > 0) {
79
			$presenceDeRessources = true;
80
		} else {
81
			$message = "Aucune ressource n'a été indiquée.\n".
82
				"Veuillez indiquer au moins un code de projet et un type de service.";
83
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
84
			throw new Exception($message, $code);
85
		}
86
		return $presenceDeRessources;
87
	}
88
 
89
	private function avoirRessourceProjet() {
90
		$presenceRessourceProjet = false;
91
		$projet = $this->ressources[0];
92
		$projetsDispo = Outils::recupererTableauConfig('projetsDispo');
93
		if (in_array($projet, $projetsDispo)) {
94
			$presenceRessourceProjet = true;
95
		} else {
96
			$message = "La ressource '$projet' n'indique pas un projet existant.\n".
97
				"Les projets existant sont :\n".implode(', ', $projetsDispo);
98
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
99
			throw new Exception($message, $code);
100
		}
101
		return $presenceRessourceProjet;
102
	}
103
 
104
	private function initialiserProjet() {
105
		$this->chargerNomDuProjet();
106
		$this->chargerConfigProjet();
145 jpm 107
 
108
		// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
146 jpm 109
		if (phpversion() < 5.3) {
110
			spl_autoload_register(array($this, 'chargerClasseProjet'));
111
		} else {
112
			spl_autoload_register(array($this, 'chargerClasseProjet'), true , true);
113
		}
3 jpm 114
	}
115
 
116
	private function chargerNomDuProjet() {
117
		$this->projetNom = $this->ressources[0];
118
	}
119
 
120
	private function chargerConfigProjet() {
121
		$projet = $this->projetNom;
122
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
123
		Config::charger($chemin);
124
	}
125
 
791 raphael 126
	/*
127
	  1) jusqu'à présent:
128
	  * le principe pour URL = a/b est: de charger
129
	  * require_once($chemin/a/ucfirst(c).php)
130
	  * new ucfirst(c); ucfirst(c)->consulter()
131
	  * sachant que ucfirst(c).php et la classe ucfirst(c) apparaîssent à de multiples emplacements (selon a)
132
 
133
	  1") Beurk... (php-className conflicts en PHP 5.2)
134
 
135
	  Ici nous faisons des cas particuliers pour Ontologies, mais en suivant ce principe, sont affectés:
811 raphael 136
	  Images, Informations, InformationsTaxonsSup,
791 raphael 137
	  LegendeCartes, NomCommune, Noms, NomsVernaculaires, Projets, Statuts,
138
	  Taxons, TaxonsCartes, Textes, ZoneGeo
139
 
140
	  cf:
141
	  $ grep -r '^[cC]lass '|grep -F '.php:'|egrep -w " \($(grep -rh '^[cC]lass '|awk '{print $2}'|sort|uniq -d|tr "\n" '|')\) " \
142
	  |sort -k2
143
 
144
	  PS: "Using two class with the same name"
145
	  http://stackoverflow.com/questions/4555186/using-two-class-with-the-same-name
146
	  > Stop.
147
	  > Whatever you are doing is wrong. Backup. Re-evaluate what you are doing and why.
148
	*/
792 raphael 149
	private static function debusquerClasse($p, $s) {
811 raphael 150
		if($s == 'ontologies') {
151
			switch($p) {
152
			case 'baseflor':
153
				return 'BaseFloreOntologies';
154
			case 'eflore':
155
				return 'EfloreOntologies';
156
			case 'chorodep':
157
				return 'ChorodepOntologies';
158
			case 'baseveg':
159
				return 'BasevegOntologies';
160
			case 'moissonnage':
814 raphael 161
				return 'MoissonnageOntologies';
811 raphael 162
			case 'commun':
163
				return 'Ontologies';
164
			}
165
		}
791 raphael 166
 
811 raphael 167
		if($s == 'cartes') {
168
			switch($p) {
169
			case 'bdtxa':
170
				return 'BdtxaCartes';
171
			case 'eflore':
172
				return 'EfloreCartes';
173
			case 'chorodep':
174
				return 'ChorodepCartes';
175
			case 'moissonnage':
814 raphael 176
				return 'MoissonnageCartes';
811 raphael 177
			}
791 raphael 178
		}
811 raphael 179
 
791 raphael 180
		return NULL;
181
	}
182
 
3 jpm 183
	private function chargerClasseProjet($classe) {
184
		if (class_exists($classe)) {
185
			return null;
186
		}
187
 
791 raphael 188
		if($this->serviceNom == 'ontologies') {
189
			$c = NULL;
190
			switch($this->projetNom) {
191
			case 'baseflor':
192
				$c = 'BaseFloreOntologies';
193
				break;
194
			case 'eflore':
195
				$c = 'EfloreOntologies';
196
				break;
197
			case 'chorodep':
198
				$c = 'ChorodepOntologies';
199
				break;
200
			case 'baseveg':
201
				$c = 'BasevegOntologies';
202
				break;
203
			case 'moissonnage':
814 raphael 204
				$c = 'MoissonnageOntologies';
791 raphael 205
				break;
206
			case 'commun':
207
				$c = 'Ontologies';
208
				break;
209
			}
210
			if($c) {
211
				require_once($this->cheminCourrant . 'commun' . DS . 'Commun.php');
212
				require_once($this->cheminCourrant . $this->projetNom . DS . $this->obtenirNomClasseService($this->serviceNom) . '.php');
213
				return;
214
			}
215
		}
216
 
811 raphael 217
		// problème de class-name conflict. Exemple:
218
		// phpunit --verbose --debug --filter 'ChorodepCartesTest::testCarteGenerique|EfloreCartesTest::testCarteGenerale'
219
		if($this->serviceNom == 'cartes') {
220
			$c = NULL;
221
			switch($this->projetNom) {
222
			case 'bdtxa':
223
				$c = 'BdtxaCartes';
224
				break;
225
			case 'eflore':
226
				$c = 'EfloreCartes';
227
				break;
228
			case 'chorodep':
229
				$c = 'ChorodepCartes';
230
				break;
815 raphael 231
			case 'moissonnage':
814 raphael 232
				$c = 'MoissonnageCartes';
811 raphael 233
				break;
234
			}
235
			if($c) {
236
				require_once($this->cheminCourrant . 'commun' . DS . 'Commun.php');
237
				require_once($this->cheminCourrant . $this->projetNom . DS . $this->obtenirNomClasseService($this->serviceNom) . '.php');
238
				return;
239
			}
240
		}
241
 
274 jpm 242
		$cheminBiblio = Config::get('chemin_bibliotheque');
3 jpm 243
		$chemins = array();
244
		$chemins[] = $this->cheminCourrant.$this->projetNom.DS;
245
		$chemins[] = $this->cheminCourrant.'commun'.DS;
274 jpm 246
		$chemins[] = $cheminBiblio;
247
		$chemins[] = $cheminBiblio.'robots'.DS;
3 jpm 248
 
249
		foreach ($chemins as $chemin) {
250
			$chemin = $chemin.$classe.'.php';
251
			if (file_exists($chemin)) {
252
				require_once $chemin;
245 jpm 253
				break;
3 jpm 254
			}
255
		}
256
	}
257
 
258
	private function avoirRessourceService() {
259
		$presenceRessourceService = false;
260
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
145 jpm 261
		if (isset($this->ressources[1])) {
262
			$service = $this->ressources[1];
263
			if (in_array($service, $servicesDispo)) {
264
				$presenceRessourceService = true;
265
			} else {
957 aurelien 266
				$message = "Le service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
145 jpm 267
					"Les services disponibles sont : ".implode(', ', $servicesDispo);
268
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
269
				throw new Exception($message, $code);
270
			}
3 jpm 271
		} else {
145 jpm 272
			$message = "Vous n'avez pas indiqué de service pour le projet {$this->projetNom} !\n".
3 jpm 273
				"Les services disponibles sont : ".implode(', ', $servicesDispo);
145 jpm 274
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
3 jpm 275
			throw new Exception($message, $code);
276
		}
277
		return $presenceRessourceService;
278
	}
279
 
280
	private function initialiserService() {
791 raphael 281
		if($this->classe) {
282
			$classe = $this->classe;
283
			$service = new $classe($this->getBdd());
852 raphael 284
			return $service->consulter($this->filtrerRessourcesPourService(), $this->parametres, $this->getBdd());
791 raphael 285
		}
110 jpm 286
		$classe = $this->obtenirNomClasseService($this->serviceNom);
3 jpm 287
		$chemins = array();
288
		$chemins[] = $this->cheminCourrant.$this->projetNom.DS.$classe.'.php';
289
		$chemins[] = $this->cheminCourrant.'commun'.DS.$classe.'.php';
290
 
291
		$service = null;
292
		foreach ($chemins as $chemin) {
293
			if (file_exists($chemin)) {
294
				$service = new $classe($this->getBdd());
811 raphael 295
				// Affichage utile lors de PHPUnit pour détecter les conflits d'autoload de classes de même nom
296
				// $reflector = new ReflectionClass($classe);
297
				// printf("===> Projets init classe '%s' depuis '%s', mais provenant de '%s'\n", $classe, $chemin, $reflector->getFileName());
3 jpm 298
				$ressourcesPourService = $this->filtrerRessourcesPourService();
929 raphael 299
				return $service->consulter($ressourcesPourService, $this->parametres, $this->getBdd());
3 jpm 300
			}
301
		}
302
		if (is_null($service)) {
957 aurelien 303
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
3 jpm 304
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
305
			throw new Exception($message, $code);
306
		}
791 raphael 307
		return NULL;
3 jpm 308
	}
309
 
310
	private function chargerNomDuService() {
311
		$this->serviceNom = $this->ressources[1];
312
	}
313
 
110 jpm 314
	private function obtenirNomClasseService($mot) {
791 raphael 315
		return str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
3 jpm 316
	}
317
 
318
	private function filtrerRessourcesPourService() {
319
		$ressourcesPourService = array();
320
		$nbreDeRessources = count($this->ressources);
321
		for ($i = 2; $i < $nbreDeRessources; $i++) {
322
			$ressourcesPourService[] = $this->ressources[$i];
323
		}
324
		return $ressourcesPourService;
325
	}
326
}
327
?>