Rev 1821 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
// declare(encoding='UTF-8');
/**
* Classe principale de chargement des sous-services de syndication.
*
* @category DEL
* @package Services
* @subpackage Syndication
* @version 0.1
* @author Mathias CHOUET <mathias@tela-botanica.org>
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
*/
class Syndication extends RestService {
private $parametres = array();
private $ressources = array();
private $conteneur;
private $methode = null;
private $sousServiceNom = null;
private $cheminCourant = null;
private $serviceNom = 'syndication';
private $format = 'atom';
private $squelette_dossier = null;
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
protected $utilisationParametresBruts = true;
public function __construct() {
$this->conteneur = new Conteneur();
$this->cheminCourant = dirname(__FILE__).DS;
$this->squelette_dossier = dirname(__FILE__).DS.$this->serviceNom.DS.'squelettes'.DS;
$this->formats_autorises = $this->conteneur->getParametreTableau('syndication.formats');
}
public function consulter($ressources, $parametres) {
$this->methode = 'consulter';
$this->initialiserRessourcesEtParametres($ressources, $parametres);
$this->verifierRessourcesEtParametres();
$this->format = isset($this->parametres['format']) ? $this->parametres['format'] : $this->format;
return $this->executerService();
}
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
$this->ressources = $ressources;
$this->parametres = $parametres;
}
private function verifierRessourcesEtParametres() {
if (isset($this->parametres['format']) && !in_array($this->parametres['format'], $this->formats_autorises)) {
$msg = "Vous devez indiquer un format de flux valide.\n".$this->getDoc();
throw new Exception($msg, RestServeur::HTTP_CODE_ERREUR);
}
}
private function executerService() {
$reponseHttp = new ReponseHttp();
try {
$donnees = $this->traiterRessources();
$resultat = $this->creerResultatService($donnees);
$reponseHttp->setResultatService($resultat);
} catch (Exception $e) {
$reponseHttp->ajouterErreur($e);
}
$reponseHttp->emettreLesEntetes();
$corps = $reponseHttp->getCorps();
return $corps;
}
private function traiterRessources() {
$this->analyserRessources();
$retour = $this->initialiserService();
return $retour;
}
private function analyserRessources() {
if ($this->methode == 'consulter') {
if (isset($this->ressources[0])) {
$this->sousServiceNom = $this->ressources[0];
}
}
if ($this->sousServiceNom == null) {
$this->lancerMessageErreurRessource();
}
}
private function lancerMessageErreurRessource() {
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
$message = "La ressource demandée '$ressource' ".
"n'est pas disponible pour le service {$this->serviceNom} !\n".
$this->getDoc();
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}
public function getDoc() {
$formatsAutorises = implode(', ', $this->formats_autorises);
return "Les URLs disponibles pour ce service sont :\n".
" * en GET :\n".
" - syndication/commentaires\n".
" - syndication/tags\n".
" - syndication/votes-par-protocole\n".
" Paramètres : \n".
" - format : $formatsAutorises";
}
private function initialiserService() {
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
$chemins = array();
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
$retour = '';
$service = null;
foreach ($chemins as $chemin) {
if (file_exists($chemin)) {
require_once $chemin;
$service = new $classe($this->conteneur);
if ($this->methode == 'consulter') {
$retour = $service->consulter();
} else {
$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
"ne possède pas de méthode '{$this->methode}' !";
$code = RestServeur::HTTP_NON_IMPLEMENTE;
throw new Exception($message, $code);
}
}
}
if (is_null($service)) {
$ressource = $this->serviceNom.'/'.implode('/', $this->ressources);
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
"est introuvable par le service '{$this->serviceNom}' !\n".
$this->getDoc();
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}
return $retour;
}
private function obtenirNomClasseService($mot) {
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
return $classeNom;
}
private function creerResultatService($donnees) {
$resultat = new ResultatService();
$resultat->mime = $this->getTypeMime();
$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
return $resultat;
}
private function getTypeMime() {
$mime = '';
switch ($this->format) {
case 'atom' :
$mime = 'application/atom+xml';
break;
case 'rss1' :
case 'rss2' :
$mime = 'application/rss+xml';
break;
case 'opml' :
$mime = 'text/x-opml';
break;
default:
$mime = 'text/html';
}
return $mime;
}
}