Subversion Repositories eFlore/Applications.del

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2212 arthur 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Classe principale de chargement des sous-services de syndication.
5
 *
6
 * @category   DEL
7
 * @package    Services
8
 * @subpackage Syndication
9
 * @version    0.1
10
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
11
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
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>
15
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
16
*/
17
class Syndication extends RestService {
18
 
19
	private $parametres = array();
20
	private $ressources = array();
21
	private $conteneur;
22
	private $methode = null;
23
	private $sousServiceNom = null;
24
	private $cheminCourant = null;
25
 
26
	private $serviceNom = 'syndication';
27
	private $format = 'atom';
28
	private $squelette_dossier = null;
29
 
30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
31
	protected $utilisationParametresBruts = true;
32
 
33
	public function __construct() {
34
		$this->conteneur = new Conteneur();
35
		$this->cheminCourant = dirname(__FILE__).DS;
36
		$this->squelette_dossier = dirname(__FILE__).DS.$this->serviceNom.DS.'squelettes'.DS;
37
		$this->formats_autorises = $this->conteneur->getParametreTableau('syndication.formats');
38
	}
39
 
40
	public function consulter($ressources, $parametres) {
41
		$this->methode = 'consulter';
42
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
43
		$this->verifierRessourcesEtParametres();
44
		$this->format = isset($this->parametres['format']) ? $this->parametres['format'] : $this->format;
45
		return $this->executerService();
46
	}
47
 
48
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
49
		$this->ressources = $ressources;
50
		$this->parametres = $parametres;
51
	}
52
 
53
	private function verifierRessourcesEtParametres() {
54
		if (isset($this->parametres['format']) && !in_array($this->parametres['format'], $this->formats_autorises)) {
55
			$msg = "Vous devez indiquer un format de flux valide.\n".$this->getDoc();
56
			throw new Exception($msg, RestServeur::HTTP_CODE_ERREUR);
57
		}
58
	}
59
 
60
	private function executerService() {
61
		$reponseHttp = new ReponseHttp();
62
		try {
63
			$donnees = $this->traiterRessources();
64
			$resultat = $this->creerResultatService($donnees);
65
			$reponseHttp->setResultatService($resultat);
66
		} catch (Exception $e) {
67
			$reponseHttp->ajouterErreur($e);
68
		}
69
		$reponseHttp->emettreLesEntetes();
70
		$corps = $reponseHttp->getCorps();
71
		return $corps;
72
	}
73
 
74
	private function traiterRessources() {
75
		$this->analyserRessources();
76
		$retour = $this->initialiserService();
77
		return $retour;
78
	}
79
 
80
	private function analyserRessources() {
81
		if ($this->methode == 'consulter' && isset($this->ressources[0])) {
82
			if (preg_match('/^tags|votes-?-Par-?Tag|tags-?Par-?Protocole$/i', $this->ressources[0])) {
83
				$this->sousServiceNom = 'tags';
84
			} else if (preg_match('/^votes|votes-?Par-?Protocole$/i', $this->ressources[0])){
85
				$this->sousServiceNom = 'votes';
86
			} else if ($this->ressources[0] == 'commentaires') {
87
				$this->sousServiceNom = 'commentaires';
88
			}
89
		}
90
		if ($this->sousServiceNom == null) {
91
			$this->lancerMessageErreurRessource();
92
		}
93
	}
94
 
95
	private function lancerMessageErreurRessource() {
96
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
97
		$message = "La ressource demandée '$ressource' ".
98
			"n'est pas disponible pour le service {$this->serviceNom} !\n".
99
			$this->getDoc();
100
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
101
		throw new Exception($message, $code);
102
	}
103
 
104
	public function getDoc() {
105
		$formatsAutorises = implode(', ', $this->formats_autorises);
106
		return "Les URLs disponibles pour ce service sont :\n".
107
			" * en GET :\n".
108
			" - syndication/commentaires\n".
109
			" - syndication/tags\n".
110
			" - syndication/votes-par-protocole\n".
111
			" Paramètres : \n".
112
			"  - format : $formatsAutorises";
113
	}
114
 
115
	private function initialiserService() {
116
		$classe = $this->obtenirNomClasseService();
117
		$chemins = array();
118
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
119
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
120
		$retour = '';
121
		$service = null;
122
 
123
		foreach ($chemins as $chemin) {
124
			if (file_exists($chemin)) {
125
				require_once $chemin;
126
				$service = new $classe($this->conteneur);
127
				if ($this->methode == 'consulter') {
128
					$retour = $service->consulter();
129
				} else {
130
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
131
						"ne possède pas de méthode '{$this->methode}' !";
132
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
133
					throw new Exception($message, $code);
134
				}
135
			}
136
		}
137
 
138
		if (is_null($service)) {
139
			$ressource = $this->serviceNom.'/'.implode('/', $this->ressources);
140
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
141
				"est introuvable par le service '{$this->serviceNom}' !\n".
142
				$this->getDoc();
143
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
144
			throw new Exception($message, $code);
145
		}
146
		return $retour;
147
	}
148
 
149
	private function obtenirNomClasseService() {
150
		return str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $this->sousServiceNom))));
151
	}
152
 
153
	private function creerResultatService($donnees) {
154
		$resultat = new ResultatService();
155
		$resultat->mime = $this->getTypeMime();
156
		$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
157
		return $resultat;
158
	}
159
 
160
	private function getTypeMime() {
161
		$mime = '';
162
		switch ($this->format) {
163
			case 'atom' :
164
				$mime = 'application/atom+xml';
165
				break;
166
			case 'rss1' :
167
			case 'rss2' :
168
				$mime = 'application/rss+xml';
169
				break;
170
			case 'opml' :
171
				$mime = 'text/x-opml';
172
				break;
173
			default:
174
				$mime = 'text/html';
175
		}
176
		return $mime;
177
	}
178
}