18,118 → 18,124 |
|
private $parametres = array(); |
private $ressources = array(); |
private $conteneur; |
private $methode = null; |
private $projetNom = array(); |
private $serviceNom = array(); |
private $sousServiceNom = null; |
private $cheminCourant = null; |
|
private $serviceNom = 'syndication'; |
private $format = 'atom'; |
private $squelette_dossier = null; |
private $formats_autorises = null; |
|
private $conteneur; |
|
/** 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.'syndication'.DS.'squelettes'.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'; |
$resultat = ''; |
$reponseHttp = new ReponseHttp(); |
try { |
$this->initialiserProjet(); |
$this->initialiserRessourcesEtParametres($ressources, $parametres); |
$this->conteneur = new Conteneur($this->parametres); |
$this->verifierRessourcesEtParametres(); |
$resultat = $this->traiterRessources(); |
$reponseHttp->setResultatService($this->creerResultatService($resultat)); |
} catch (Exception $e) { |
$reponseHttp->ajouterErreur($e); |
} |
$reponseHttp->emettreLesEntetes(); |
$corps = $reponseHttp->getCorps(); |
return $corps; |
$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) { |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) { |
$this->ressources = $ressources; |
$this->parametres = $parametres; |
} |
|
private function verifierRessourcesEtParametres() { |
$servicesDispos = Config::get('servicesDispo'); |
if (!isset($this->ressources[0]) || !in_array($this->ressources[0], explode(',',$servicesDispos))) { |
$message = "Vous devez indiquer un nom de service valide, les services disponibles sont ".$servicesDispos; |
$code = RestServeur::HTTP_CODE_ERREUR; |
throw new Exception($message, $code); |
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); |
} |
} |
|
$chaineFormatsAutorises = Config::get('formatsRss'); |
$this->formats_autorises = explode(',', $chaineFormatsAutorises); |
if (!isset($this->ressources[1]) || !in_array($this->ressources[1], $this->formats_autorises)) { |
$message = "Vous devez indiquer un format de flux valide, les formats acceptés sont ".$chaineFormatsAutorises; |
$code = RestServeur::HTTP_CODE_ERREUR; |
throw new Exception($message, $code); |
} else { |
$this->format = $this->ressources[1]; |
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() { |
$retour = ''; |
$this->analyserRessources(); |
$retour = $this->initialiserService(); |
return $retour; |
} |
|
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 analyserRessources() { |
if ($this->methode == 'consulter') { |
if (isset($this->ressources[0])) { |
$this->sousServiceNom = $this->ressources[0]; |
} |
} |
if ($this->sousServiceNom == null) { |
$this->lancerMessageErreurRessource(); |
} |
} |
|
/*------------------------------------------------------------------------------------------------------------------ |
CONFIGURATION DU PROJET |
------------------------------------------------------------------------------------------------------------------*/ |
private function initialiserProjet() { |
$this->projetNom = 'syndication'; |
$this->chargerConfigProjet(); |
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); |
} |
|
private function chargerConfigProjet() { |
$projet = $this->projetNom; |
$chemin = Config::get('chemin_configurations')."config_$projet.ini"; |
Config::charger($chemin); |
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"; |
} |
|
/*------------------------------------------------------------------------------------------------------------------ |
CONFIGURATION DU SERVICE |
------------------------------------------------------------------------------------------------------------------*/ |
private function initialiserService() { |
$this->chargerNomService(); |
|
$classe = $this->obtenirNomClasseService($this->serviceNom); |
$classe = $this->obtenirNomClasseService($this->sousServiceNom); |
$chemins = array(); |
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php'; |
$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)) { |
$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini'); |
require_once $chemin; |
$service = new $classe($this->conteneur); |
if ($this->methode == 'consulter') { |
$retour = $service->consulter($this->ressources, $this->parametres); |
$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)) { |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !"; |
$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); |
} |
136,21 → 142,18 |
return $retour; |
} |
|
private function chargerNomService() { |
if (!isset($this->ressources[0])) { |
$message = "Vous devez indiquer un nom de service"; |
$code = RestServeur::HTTP_CODE_ERREUR; |
throw new Exception($message, $code); |
} else { |
$this->serviceNom = $this->ressources[0]; |
} |
} |
|
private function obtenirNomClasseService($mot) { |
$classeNom = 'Syndication'.ucwords($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) { |
169,4 → 172,4 |
} |
return $mime; |
} |
} |
} |