Subversion Repositories eFlore/Applications.del

Rev

Rev 1821 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1047 aurelien 1
<?php
1820 jpm 2
// declare(encoding='UTF-8');
1047 aurelien 3
/**
1795 jpm 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>
1047 aurelien 16
*/
17
class Syndication extends RestService {
18
 
19
	private $parametres = array();
20
	private $ressources = array();
1821 jpm 21
	private $conteneur;
1047 aurelien 22
	private $methode = null;
1821 jpm 23
	private $sousServiceNom = null;
1047 aurelien 24
	private $cheminCourant = null;
1821 jpm 25
 
26
	private $serviceNom = 'syndication';
27
	private $format = 'atom';
1047 aurelien 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() {
1821 jpm 34
		$this->conteneur = new Conteneur();
1047 aurelien 35
		$this->cheminCourant = dirname(__FILE__).DS;
1821 jpm 36
		$this->squelette_dossier = dirname(__FILE__).DS.$this->serviceNom.DS.'squelettes'.DS;
37
		$this->formats_autorises = $this->conteneur->getParametreTableau('syndication.formats');
1047 aurelien 38
	}
39
 
40
	public function consulter($ressources, $parametres) {
41
		$this->methode = 'consulter';
1821 jpm 42
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
43
		$this->verifierRessourcesEtParametres();
44
		$this->format = isset($this->parametres['format']) ? $this->parametres['format'] : $this->format;
45
		return $this->executerService();
1047 aurelien 46
	}
1661 jpm 47
 
1821 jpm 48
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
1047 aurelien 49
		$this->ressources = $ressources;
50
		$this->parametres = $parametres;
51
	}
1661 jpm 52
 
1047 aurelien 53
	private function verifierRessourcesEtParametres() {
1821 jpm 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);
1057 aurelien 57
		}
1821 jpm 58
	}
1661 jpm 59
 
1821 jpm 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);
1047 aurelien 68
		}
1821 jpm 69
		$reponseHttp->emettreLesEntetes();
70
		$corps = $reponseHttp->getCorps();
71
		return $corps;
1047 aurelien 72
	}
73
 
74
	private function traiterRessources() {
1821 jpm 75
		$this->analyserRessources();
1047 aurelien 76
		$retour = $this->initialiserService();
77
		return $retour;
78
	}
1661 jpm 79
 
1821 jpm 80
	private function analyserRessources() {
1922 jpm 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';
1821 jpm 88
			}
89
		}
90
		if ($this->sousServiceNom == null) {
91
			$this->lancerMessageErreurRessource();
92
		}
1047 aurelien 93
	}
94
 
1821 jpm 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);
1047 aurelien 102
	}
103
 
1821 jpm 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";
1047 aurelien 113
	}
114
 
115
	private function initialiserService() {
1922 jpm 116
		$classe = $this->obtenirNomClasseService();
1047 aurelien 117
		$chemins = array();
1821 jpm 118
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
1047 aurelien 119
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
120
		$retour = '';
121
		$service = null;
1821 jpm 122
 
1047 aurelien 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') {
1821 jpm 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);
1047 aurelien 134
				}
135
			}
136
		}
1661 jpm 137
 
1047 aurelien 138
		if (is_null($service)) {
1821 jpm 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();
1047 aurelien 143
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
144
			throw new Exception($message, $code);
145
		}
146
		return $retour;
147
	}
1661 jpm 148
 
1922 jpm 149
	private function obtenirNomClasseService() {
150
		return str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $this->sousServiceNom))));
1047 aurelien 151
	}
1661 jpm 152
 
1821 jpm 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
 
1047 aurelien 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
	}
1821 jpm 178
}