Subversion Repositories eFlore/Applications.del

Rev

Rev 1049 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1047 aurelien 1
<?php
2
/**
3
* Description :
4
* Classe principale de chargement des services de syndication
5
*
6
* Encodage en entrée : utf8
7
* Encodage en sortie : utf8
8
* @package del-service
9
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
10
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
11
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
12
* @version 0.1
13
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
14
*/
15
class Syndication extends RestService {
16
 
17
 
18
	private $parametres = array();
19
	private $ressources = array();
20
	private $methode = null;
21
	private $projetNom = array();
22
	private $serviceNom = array();
23
	private $cheminCourant = null;
24
	private $squelette_dossier = null;
25
	private $format = null;
26
 
27
	private $conteneur;
28
 
29
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
30
	protected $utilisationParametresBruts = true;
31
 
32
	public function __construct() {
33
		$this->cheminCourant = dirname(__FILE__).DS;
34
		$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'syndication'.DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
35
	}
36
 
37
	public function consulter($ressources, $parametres) {
38
		$this->methode = 'consulter';
39
		$resultat = '';
40
		$reponseHttp = new ReponseHttp();
41
		try {
42
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
43
			$this->verifierRessourcesEtParametres();
44
			$this->conteneur = new Conteneur($this->parametres);
45
			$resultat = $this->traiterRessources();
46
			$reponseHttp->setResultatService($this->creerResultatService($resultat));
47
		} catch (Exception $e) {
48
			$reponseHttp->ajouterErreur($e);
49
		}
50
		$reponseHttp->emettreLesEntetes();
51
		$corps = $reponseHttp->getCorps();
52
		return $corps;
53
	}
54
 
55
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
56
		$this->ressources = $ressources;
57
		$this->parametres = $parametres;
58
	}
59
 
60
	private function verifierRessourcesEtParametres() {
61
		if (!isset($this->ressources[1])) {
62
			$message = "Vous devez indiquer un format de flux";
63
			$code = RestServeur::HTTP_CODE_ERREUR;
64
			throw new Exception($message, $code);
65
		} else {
66
			$this->format = $this->ressources[1];
67
		}
68
	}
69
 
70
	private function traiterRessources() {
71
		$retour = '';
72
		$this->initialiserProjet();
73
		$retour = $this->initialiserService();
74
 
75
		return $retour;
76
	}
77
 
78
	private function creerResultatService($donnees) {
79
		$resultat = new ResultatService();
80
		$resultat->mime = $this->getTypeMime();
81
		$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
82
 
83
		return $resultat;
84
	}
85
 
86
	/*------------------------------------------------------------------------------------------------------------------
87
										CONFIGURATION DU PROJET
88
	------------------------------------------------------------------------------------------------------------------*/
89
	private function initialiserProjet() {
90
		$this->projetNom = 'syndication';
91
		$this->chargerConfigProjet();
92
	}
93
 
94
	private function chargerConfigProjet() {
95
		$projet = $this->projetNom;
96
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
97
		Config::charger($chemin);
98
	}
99
 
100
	/*------------------------------------------------------------------------------------------------------------------
101
								CONFIGURATION DU SERVICE
102
	------------------------------------------------------------------------------------------------------------------*/
103
	private function initialiserService() {
104
		$this->chargerNomService();
105
 
106
		$classe = $this->obtenirNomClasseService($this->serviceNom);
107
		$chemins = array();
108
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
109
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
110
		$retour = '';
111
		$service = null;
112
		foreach ($chemins as $chemin) {
113
			if (file_exists($chemin)) {
114
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
115
				require_once $chemin;
116
				$service = new $classe($this->conteneur);
117
				if ($this->methode == 'consulter') {
118
					$retour = $service->consulter($this->ressources, $this->parametres);
119
				}
120
			}
121
		}
122
 
123
		if (is_null($service)) {
124
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
125
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
126
			throw new Exception($message, $code);
127
		}
128
		return $retour;
129
	}
130
 
131
	private function chargerNomService() {
132
		if (!isset($this->ressources[0])) {
133
			$message = "Vous devez indiquer un nom de service";
134
			$code = RestServeur::HTTP_CODE_ERREUR;
135
			throw new Exception($message, $code);
136
		} else {
137
			$this->serviceNom = $this->ressources[0];
138
		}
139
	}
140
 
141
	private function obtenirNomClasseService($mot) {
142
		$classeNom = 'Syndication'.ucwords($mot);
143
		return $classeNom;
144
	}
145
 
146
	private function getTypeMime() {
147
		$mime = '';
148
		switch ($this->format) {
149
			case 'atom' :
150
				$mime = 'application/atom+xml';
151
				break;
152
			case 'rss1' :
153
			case 'rss2' :
154
				$mime = 'application/rss+xml';
155
				break;
156
			case 'opml' :
157
				$mime = 'text/x-opml';
158
				break;
159
			default:
160
				$mime = 'text/html';
161
		}
162
		return $mime;
163
	}
164
}
165
?>