Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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