Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 110 | Rev 146 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 110 Rev 145
1
<?php
1
<?php
2
/**
2
/**
3
* Description :
3
* Description :
4
* Classe principale de chargement des services d'eFlore.
4
* Classe principale de chargement des services d'eFlore.
5
*
5
*
6
* Encodage en entrée : utf8
6
* Encodage en entrée : utf8
7
* Encodage en sortie : utf8
7
* Encodage en sortie : utf8
8
* @package eflore-projets
8
* @package eflore-projets
9
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
9
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
10
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
10
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
11
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
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>
13
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
* @version 0.1
14
* @version 0.1
15
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
15
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
16
*/
16
*/
17
class Projets extends RestService {
17
class Projets extends RestService {
18
 
18
 
19
	/** Contients les paramètres.*/
19
	/** Contients les paramètres.*/
20
	private $parametres = array();
20
	private $parametres = array();
21
	/** Contients les ressources.*/
21
	/** Contients les ressources.*/
22
	private $ressources = array();
22
	private $ressources = array();
23
 
23
 
24
	/** Nom du projet courrant. */
24
	/** Nom du projet courrant. */
25
	private $projetNom = array();
25
	private $projetNom = array();
26
	/** Nom du service demandé. */
26
	/** Nom du service demandé. */
27
	private $serviceNom = array();
27
	private $serviceNom = array();
28
	/** Chemin vers le dossier courrant. */
28
	/** Chemin vers le dossier courrant. */
29
	private $cheminCourrant = null;
29
	private $cheminCourrant = null;
30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres brutes. */
30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres brutes. */
31
	protected $utilisationParametresBruts = true;
31
	protected $utilisationParametresBruts = true;
32
 
32
 
33
	public function __construct() {
33
	public function __construct() {
34
		$this->cheminCourrant = dirname(__FILE__).DS;
34
		$this->cheminCourrant = dirname(__FILE__).DS;
35
	}
35
	}
36
 
36
 
37
	public function consulter($ressources, $parametres) {
37
	public function consulter($ressources, $parametres) {
38
		$resultat = '';
38
		$resultat = '';
39
		$reponseHttp = new ReponseHttp();
39
		$reponseHttp = new ReponseHttp();
40
		try {
40
		try {
41
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
41
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
42
			$resultat = $this->traiterRessources();
42
			$resultat = $this->traiterRessources();
43
			$reponseHttp->setResultatService($resultat);
43
			$reponseHttp->setResultatService($resultat);
44
		} catch (Exception $e) {
44
		} catch (Exception $e) {
45
			$reponseHttp->ajouterErreur($e);
45
			$reponseHttp->ajouterErreur($e);
46
		}
46
		}
47
		$reponseHttp->emettreLesEntetes();
47
		$reponseHttp->emettreLesEntetes();
48
		$corps = $reponseHttp->getCorps();
48
		$corps = $reponseHttp->getCorps();
49
		return $corps;
49
		return $corps;
50
	}
50
	}
51
 
51
 
52
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
52
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
53
		$this->ressources = $ressources;
53
		$this->ressources = $ressources;
54
		$this->parametres = $parametres;
54
		$this->parametres = $parametres;
55
	}
55
	}
56
 
56
 
57
	private function traiterRessources() {
57
	private function traiterRessources() {
58
		$retour = '';
58
		$retour = '';
59
		if ($this->avoirRessources()) {
59
		if ($this->avoirRessources()) {
60
			if ($this->avoirRessourceProjet()) {
60
			if ($this->avoirRessourceProjet()) {
61
				$this->initialiserProjet();
61
				$this->initialiserProjet();
62
				if ($this->avoirRessourceService()) {
62
				if ($this->avoirRessourceService()) {
63
					$retour = $this->initialiserService();
63
					$retour = $this->initialiserService();
64
				}
64
				}
65
			}
65
			}
66
		}
66
		}
67
		return $retour;
67
		return $retour;
68
	}
68
	}
69
 
69
 
70
	private function avoirRessources() {
70
	private function avoirRessources() {
71
		$presenceDeRessources = false;
71
		$presenceDeRessources = false;
72
		if (isset($this->ressources) && count($this->ressources) > 0) {
72
		if (isset($this->ressources) && count($this->ressources) > 0) {
73
			$presenceDeRessources = true;
73
			$presenceDeRessources = true;
74
		} else {
74
		} else {
75
			$message = "Aucune ressource n'a été indiquée.\n".
75
			$message = "Aucune ressource n'a été indiquée.\n".
76
				"Veuillez indiquer au moins un code de projet et un type de service.";
76
				"Veuillez indiquer au moins un code de projet et un type de service.";
77
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
77
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
78
			throw new Exception($message, $code);
78
			throw new Exception($message, $code);
79
		}
79
		}
80
		return $presenceDeRessources;
80
		return $presenceDeRessources;
81
	}
81
	}
82
 
82
 
83
	private function avoirRessourceProjet() {
83
	private function avoirRessourceProjet() {
84
		$presenceRessourceProjet = false;
84
		$presenceRessourceProjet = false;
85
		$projet = $this->ressources[0];
85
		$projet = $this->ressources[0];
86
		$projetsDispo = Outils::recupererTableauConfig('projetsDispo');
86
		$projetsDispo = Outils::recupererTableauConfig('projetsDispo');
87
		if (in_array($projet, $projetsDispo)) {
87
		if (in_array($projet, $projetsDispo)) {
88
			$presenceRessourceProjet = true;
88
			$presenceRessourceProjet = true;
89
		} else {
89
		} else {
90
			$message = "La ressource '$projet' n'indique pas un projet existant.\n".
90
			$message = "La ressource '$projet' n'indique pas un projet existant.\n".
91
				"Les projets existant sont :\n".implode(', ', $projetsDispo);
91
				"Les projets existant sont :\n".implode(', ', $projetsDispo);
92
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
92
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
93
			throw new Exception($message, $code);
93
			throw new Exception($message, $code);
94
		}
94
		}
95
		return $presenceRessourceProjet;
95
		return $presenceRessourceProjet;
96
	}
96
	}
97
 
97
 
98
	private function initialiserProjet() {
98
	private function initialiserProjet() {
99
		$this->chargerNomDuProjet();
99
		$this->chargerNomDuProjet();
100
		$this->chargerConfigProjet();
100
		$this->chargerConfigProjet();
-
 
101
 
101
		// Enregistrement en première position des autoload de la méthode gérant les classes des services
102
		// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
102
		spl_autoload_register(array($this, 'chargerClasseProjet'), true , true);
103
		spl_autoload_register(array($this, 'chargerClasseProjet'), true , true);
103
	}
104
	}
104
 
105
 
105
	private function chargerNomDuProjet() {
106
	private function chargerNomDuProjet() {
106
		$this->projetNom = $this->ressources[0];
107
		$this->projetNom = $this->ressources[0];
107
	}
108
	}
108
 
109
 
109
	private function chargerConfigProjet() {
110
	private function chargerConfigProjet() {
110
		$projet = $this->projetNom;
111
		$projet = $this->projetNom;
111
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
112
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
112
		Config::charger($chemin);
113
		Config::charger($chemin);
113
	}
114
	}
114
 
115
 
115
	private function chargerClasseProjet($classe) {
116
	private function chargerClasseProjet($classe) {
116
		if (class_exists($classe)) {
117
		if (class_exists($classe)) {
117
			return null;
118
			return null;
118
		}
119
		}
119
 
120
 
120
		$chemins = array();
121
		$chemins = array();
121
		$chemins[] = $this->cheminCourrant.$this->projetNom.DS;
122
		$chemins[] = $this->cheminCourrant.$this->projetNom.DS;
122
		$chemins[] = $this->cheminCourrant.'commun'.DS;
123
		$chemins[] = $this->cheminCourrant.'commun'.DS;
123
		$chemins[] = Config::get('chemin_bibliotheque');
124
		$chemins[] = Config::get('chemin_bibliotheque');
124
 
125
 
125
		foreach ($chemins as $chemin) {
126
		foreach ($chemins as $chemin) {
126
			$chemin = $chemin.$classe.'.php';
127
			$chemin = $chemin.$classe.'.php';
-
 
128
			print_r($chemin);
127
			if (file_exists($chemin)) {
129
			if (file_exists($chemin)) {
128
				require_once $chemin;
130
				require_once $chemin;
129
			}
131
			}
130
		}
132
		}
131
	}
133
	}
132
 
134
 
133
	private function avoirRessourceService() {
135
	private function avoirRessourceService() {
134
		$presenceRessourceService = false;
136
		$presenceRessourceService = false;
135
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
137
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
-
 
138
		if (isset($this->ressources[1])) {
136
		$service = $this->ressources[1];
139
			$service = $this->ressources[1];
137
		if (in_array($service, $servicesDispo)) {
140
			if (in_array($service, $servicesDispo)) {
138
			$presenceRessourceService = true;
141
				$presenceRessourceService = true;
-
 
142
			} else {
-
 
143
				$message = "La service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
-
 
144
					"Les services disponibles sont : ".implode(', ', $servicesDispo);
-
 
145
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
-
 
146
				throw new Exception($message, $code);
-
 
147
			}
139
		} else {
148
		} else {
140
			$message = "La service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
149
			$message = "Vous n'avez pas indiqué de service pour le projet {$this->projetNom} !\n".
141
				"Les services disponibles sont : ".implode(', ', $servicesDispo);
150
				"Les services disponibles sont : ".implode(', ', $servicesDispo);
142
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
151
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
143
			throw new Exception($message, $code);
152
			throw new Exception($message, $code);
144
		}
153
		}
145
		return $presenceRessourceService;
154
		return $presenceRessourceService;
146
	}
155
	}
147
 
156
 
148
	private function initialiserService() {
157
	private function initialiserService() {
149
		$this->chargerNomDuService();
158
		$this->chargerNomDuService();
150
		$classe = $this->obtenirNomClasseService($this->serviceNom);
159
		$classe = $this->obtenirNomClasseService($this->serviceNom);
151
		$chemins = array();
160
		$chemins = array();
152
		$chemins[] = $this->cheminCourrant.$this->projetNom.DS.$classe.'.php';
161
		$chemins[] = $this->cheminCourrant.$this->projetNom.DS.$classe.'.php';
153
		$chemins[] = $this->cheminCourrant.'commun'.DS.$classe.'.php';
162
		$chemins[] = $this->cheminCourrant.'commun'.DS.$classe.'.php';
154
 
163
 
155
		$retour = '';
164
		$retour = '';
156
		$service = null;
165
		$service = null;
157
		foreach ($chemins as $chemin) {
166
		foreach ($chemins as $chemin) {
158
			if (file_exists($chemin)) {
167
			if (file_exists($chemin)) {
-
 
168
				if (phpversion() < 5.3) {
-
 
169
					// L'option prepend de spl_autoload_register ne fonctionne pas avant php 5.3
-
 
170
					// TODO : améliorer ce comportement!
-
 
171
					require_once $chemin;
-
 
172
				}
159
				$service = new $classe($this->getBdd());
173
				$service = new $classe($this->getBdd());
160
				$ressourcesPourService = $this->filtrerRessourcesPourService();
174
				$ressourcesPourService = $this->filtrerRessourcesPourService();
161
				$retour = $service->consulter($ressourcesPourService, $this->parametres);
175
				$retour = $service->consulter($ressourcesPourService, $this->parametres);
162
			}
176
			}
163
		}
177
		}
164
		if (is_null($service)) {
178
		if (is_null($service)) {
165
			$message = "La service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
179
			$message = "La service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
166
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
180
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
167
			throw new Exception($message, $code);
181
			throw new Exception($message, $code);
168
		}
182
		}
169
		return $retour;
183
		return $retour;
170
	}
184
	}
171
 
185
 
172
	private function chargerNomDuService() {
186
	private function chargerNomDuService() {
173
		$this->serviceNom = $this->ressources[1];
187
		$this->serviceNom = $this->ressources[1];
174
	}
188
	}
175
 
189
 
176
	private function obtenirNomClasseService($mot) {
190
	private function obtenirNomClasseService($mot) {
177
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
191
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
178
		return $classeNom;
192
		return $classeNom;
179
	}
193
	}
180
 
194
 
181
	private function filtrerRessourcesPourService() {
195
	private function filtrerRessourcesPourService() {
182
		$ressourcesPourService = array();
196
		$ressourcesPourService = array();
183
		$nbreDeRessources = count($this->ressources);
197
		$nbreDeRessources = count($this->ressources);
184
		for ($i = 2; $i < $nbreDeRessources; $i++) {
198
		for ($i = 2; $i < $nbreDeRessources; $i++) {
185
			$ressourcesPourService[] = $this->ressources[$i];
199
			$ressourcesPourService[] = $this->ressources[$i];
186
		}
200
		}
187
		return $ressourcesPourService;
201
		return $ressourcesPourService;
188
	}
202
	}
189
}
203
}
190
?>
204
?>