Subversion Repositories eFlore/Applications.del

Rev

Rev 1795 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1795 Rev 1816
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
 * Classe principale de chargement des sous-services Noms et Taxons utilisés par DEL.
4
 * Classe principale de chargement des sous-services Noms et Taxons utilisés par DEL.
5
 *
5
 *
6
 * @category   DEL
6
 * @category   DEL
7
 * @package    Services
7
 * @package    Services
8
 * @subpackage NomsTaxons
8
 * @subpackage NomsTaxons
9
 * @version    0.1
9
 * @version    0.1
10
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
10
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
11
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
12
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
13
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
13
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
14
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
15
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
15
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
16
 */
16
 */
17
class Nomstaxons extends RestService {
17
class Nomstaxons extends RestService {
18
 
-
 
19
 
18
 
20
	private $parametres = array();
19
	private $parametres = array();
21
	private $ressources = array();
20
	private $ressources = array();
22
	private $methode = null;
21
	private $methode = null;
23
	private $projetNom = array();
22
	private $serviceNom = 'nomstaxons';
24
	private $serviceNom = array();
23
	private $sousServiceNom = null;
25
	private $cheminCourant = null;
24
	private $cheminCourant = null;
26
 
25
 
27
	private $conteneur;
26
	private $conteneur;
28
 
27
 
29
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
28
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
30
	protected $utilisationParametresBruts = true;
29
	protected $utilisationParametresBruts = true;
31
 
30
 
32
	public function __construct() {
31
	public function __construct() {
33
		$this->cheminCourant = dirname(__FILE__).DS;
32
		$this->cheminCourant = dirname(__FILE__).DS;
34
	}
33
	}
35
 
34
 
36
	public function consulter($ressources, $parametres) {
35
	public function consulter($ressources, $parametres) {
37
		$this->methode = 'consulter';
36
		$this->methode = 'consulter';
-
 
37
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
-
 
38
		return $this->executerService();
-
 
39
	}
-
 
40
 
-
 
41
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
38
		$resultat = '';
42
		$this->ressources = $ressources;
-
 
43
		$this->parametres = $parametres;
-
 
44
	}
-
 
45
 
-
 
46
	private function executerService() {
39
		$reponseHttp = new ReponseHttp();
47
		$reponseHttp = new ReponseHttp();
40
		try {
48
		try {
41
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
-
 
42
			$this->conteneur = new Conteneur($this->parametres);
49
			$this->conteneur = new Conteneur($this->parametres);
43
			$resultat = $this->traiterRessources();
50
			$resultat = $this->traiterRessources();
44
			$reponseHttp->setResultatService($resultat);
51
			$reponseHttp->setResultatService($resultat);
45
		} catch (Exception $e) {
52
		} catch (Exception $e) {
46
			$reponseHttp->ajouterErreur($e);
53
			$reponseHttp->ajouterErreur($e);
47
		}
54
		}
48
		$reponseHttp->emettreLesEntetes();
55
		$reponseHttp->emettreLesEntetes();
49
		$corps = $reponseHttp->getCorps();
56
		$corps = $reponseHttp->getCorps();
50
		return $corps;
57
		return $corps;
51
	}
58
	}
52
 
-
 
53
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
-
 
54
		$this->ressources = $ressources;
-
 
55
		$this->parametres = $parametres;
-
 
56
	}
-
 
57
 
59
 
58
	private function traiterRessources() {
-
 
59
		$retour = '';
60
	private function traiterRessources() {
60
		$this->initialiserProjet();
61
		$this->analyserRessources();
61
		$retour = $this->initialiserService();
62
		$retour = $this->initialiserService();
62
		return $retour;
63
		return $retour;
63
	}
64
	}
64
 
65
 
65
	/*------------------------------------------------------------------------------------------------------------------
66
	private function analyserRessources() {
66
										CONFIGURATION DU PROJET
-
 
67
	------------------------------------------------------------------------------------------------------------------*/
67
		if ($this->methode == 'consulter') {
68
	private function initialiserProjet() {
68
			if (count($this->ressources) == 0
-
 
69
				&& $this->verifierPresenceParametre('masque.nom')
69
		$this->projetNom = 'nomstaxons';
70
				&& $this->verifierPresenceParametre('masque.referentiel')) {
70
		$this->chargerConfigProjet();
71
				$this->sousServiceNom = 'liste-taxons';
71
	}
72
			}
72
 
73
		}
73
	private function chargerConfigProjet() {
74
		if ($this->sousServiceNom == null) {
74
		$projet = $this->projetNom;
-
 
75
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
75
			$this->lancerMessageErreurRessource();
76
		Config::charger($chemin);
76
		}
77
	}
77
	}
-
 
78
 
78
 
79
	private function verifierPresenceParametre($cle) {
79
	/*------------------------------------------------------------------------------------------------------------------
80
		if (isset($this->parametres[$cle]) && trim($this->parametres[$cle]) == '') {
80
								CONFIGURATION DU SERVICE
81
			$message = "Le service demandé '{$this->serviceNom}' ".
81
	------------------------------------------------------------------------------------------------------------------*/
82
				"nécessite l'utilisation de paramètres (non vide) : masque.nom & masque.referentiel\n";
-
 
83
			throw new Exception($message, RestServeur::HTTP_CODE_ECHEC_CONDITION);
82
	private function initialiserService() {
84
		}
-
 
85
		return true;
-
 
86
	}
-
 
87
 
-
 
88
	private function lancerMessageErreurRessource() {
-
 
89
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
-
 
90
		$message = "La ressource demandée '$ressource' ".
-
 
91
			"n'est pas disponible pour le service ".$this->serviceNom." !\n".
-
 
92
			"Les URLs disponibles sont : \n".
-
 
93
			" - en GET : nomstaxons (paramètres : masque.nom & masque.referentiel) \n";
-
 
94
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
-
 
95
		throw new Exception($message, $code);
-
 
96
	}
83
		$this->chargerNomService();
97
 
84
 
98
	private function initialiserService() {
85
		$classe = $this->obtenirNomClasseService($this->serviceNom);
99
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
86
		$chemins = array();
100
		$chemins = array();
87
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
101
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
88
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
102
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
89
		$retour = '';
103
		$retour = '';
90
		$service = null;
104
		$service = null;
-
 
105
 
91
		foreach ($chemins as $chemin) {
106
		foreach ($chemins as $chemin) {
92
			if (file_exists($chemin)) {
107
			if (file_exists($chemin)) {
93
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
-
 
94
				require_once $chemin;
108
				require_once $chemin;
95
				$service = new $classe($this->conteneur);
109
				$service = new $classe($this->conteneur);
96
				if ($this->methode == 'consulter') {
110
				if ($this->methode == 'consulter') {
97
					$retour = $service->consulter($this->ressources, $this->parametres);
111
					$retour = $service->consulter();
98
				} else {
112
				} else {
-
 
113
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
-
 
114
						"ne possède pas de méthode '{$this->methode}' !";
-
 
115
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
99
					//TODO : throw exception
116
					throw new Exception($message, $code);
100
				}
117
				}
101
			}
118
			}
102
		}
119
		}
103
 
120
 
104
		if (is_null($service)) {
121
		if (is_null($service)) {
-
 
122
			$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
105
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
123
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
-
 
124
				"est introuvable par le service '{$this->serviceNom}' !";
106
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
125
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
107
			throw new Exception($message, $code);
126
			throw new Exception($message, $code);
108
		}
127
		}
109
		return $retour;
128
		return $retour;
110
	}
129
	}
111
 
-
 
112
	private function chargerNomService() {
-
 
113
		// si la méthode est POST, on ajouter un commentaire
-
 
114
		$this->serviceNom = 'liste-taxons';
-
 
115
	}
-
 
116
 
130
 
117
	private function obtenirNomClasseService($mot) {
131
	private function obtenirNomClasseService($mot) {
118
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
132
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
119
		return $classeNom;
133
		return $classeNom;
120
	}
134
	}
121
}
-
 
122
?>
-
 
123
135
}
-
 
136
124
137