Rev 26 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
class Moissonnage extends RestService {
private $parametres = array();
private $ressources = array();
private $squeletteDossier = '';
private $nomService = '';
private $nomSource = '';
private $cache;
public function __construct() {
$this->squeletteDossier = dirname(__FILE__) . DIRECTORY_SEPARATOR;
}
public function consulter($ressources, $parametres) {
$resultat = '';
$reponseHttp = new ReponseHttp();
try {
$this->initialiserRessourcesEtParametres($ressources, $parametres);
$resultat = $this->traiterRessources();
$reponseHttp->setResultatService($resultat);
} catch (Exception $e) {
$reponseHttp->ajouterErreur($e);
}
$reponseHttp->emettreLesEntetes();
$corps = $reponseHttp->getCorps();
return $corps;
}
private function initialiserRessourcesEtParametres($ressources, $parametres) {
$this->ressources = $ressources;
$this->parametres = $parametres;
}
private function traiterRessources() {
$retour = '';
if ($this->avoirRessources()) {
if ($this->avoirRessourceSource()) {
$this->initialiserSource();
if ($this->avoirRessourceService()) {
$retour = $this->initialiserService();
}
}
}
return $retour;
}
private function avoirRessources() {
$presenceDeRessources = false;
if (isset($this->ressources) && count($this->ressources) > 0) {
$presenceDeRessources = true;
} else {
$message = "Accès au service refusé : aucune ressource indiquée.\n"
. "Veuillez indiquer au moins une source de données à interroger"
. " et le type informations a renvoyer.";
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
throw new Exception($message, $code);
}
return $presenceDeRessources;
}
private function avoirRessourceSource() {
$presenceRessourceDemandee = false;
$source = $this->ressources[0];
$sourcesDispo = $this->recupererTableauConfig('sourcesDispo');
if (in_array($source, $sourcesDispo)) {
$presenceRessourceDemandee = true;
} else {
$message = "La source de données '{$source}' n'est pas référencée dans nos services.\n"
. "Les sources disponibles sont les suivantes : " . implode(', ', $sourcesDispo) . ".\n";
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
throw new Exception($message, $code);
}
return $presenceRessourceDemandee;
}
private function recupererTableauConfig($parametres) {
$tableau = array();
$tableauPartiel = explode(',', Config::get($parametres));
$tableauPartiel = array_map('trim', $tableauPartiel);
foreach ($tableauPartiel as $champ) {
if (strpos($champ, '=') === false) {
$tableau[] = $champ;
} else {
list($cle, $val) = explode('=', $champ);
$clePropre = trim($cle);
$valeurPropre = trim($val);
$tableau[$clePropre] = $valeurPropre;
}
}
return $tableau;
}
private function initialiserSource() {
$this->chargerNomSource();
$this->chargerConfigSource();
// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
if (phpversion() < 5.3) {
spl_autoload_register(array($this, 'chargerClasseSource'));
} else {
spl_autoload_register(array($this, 'chargerClasseSource'), true , true);
}
}
private function chargerNomSource() {
$this->nomSource = $this->ressources[0];
}
private function chargerConfigSource() {
$source = $this->nomSource;
$chemin = Config::get('chemin_configurations')."config_$source.ini";
Config::charger($chemin);
}
private function chargerClasseSource($classe) {
if (class_exists($classe)) {
return null;
}
$cheminBiblio = Config::get('chemin_bibliotheque');
$chemins = array();
$chemins[] = $this->squeletteDossier . $this->nomSource . DIRECTORY_SEPARATOR;
$chemins[] = $this->squeletteDossier . 'commun' . DIRECTORY_SEPARATOR;
$chemins[] = $cheminBiblio;
$chemins[] = $cheminBiblio . 'robots' . DIRECTORY_SEPARATOR;
foreach ($chemins as $chemin) {
$chemin = $chemin . $classe . '.php';
if (file_exists($chemin)) {
require_once $chemin;
break;
}
}
}
private function avoirRessourceService() {
$presenceRessourceService = false;
$servicesDispo = $this->recupererTableauConfig('servicesDispo');
if (isset($this->ressources[1])) {
$service = $this->ressources[1];
if (in_array($service, $servicesDispo)) {
$presenceRessourceService = true;
} else {
$message = "La service demandé '$service' n'est pas disponible pour le projet "
. $this->nomSource . " !\n"
. "Les services disponibles sont : " . implode(', ', $servicesDispo);
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}
} else {
$message = "Vous n'avez pas indiqué de service pour le projet {$this->nomSource} !\n"
. "Les services disponibles sont : " . implode(', ', $servicesDispo);
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
throw new Exception($message, $code);
}
return $presenceRessourceService;
}
private function initialiserService() {
$this->chargerNomDuService();
$classe = $this->obtenirNomClasseService($this->nomService);
$chemins = array();
$chemins[] = $this->squeletteDossier . $this->nomSource . DS . $classe . '.php';
$chemins[] = $this->squeletteDossier . 'commun'.DS.$classe.'.php';
$retour = '';
$service = null;
foreach ($chemins as $chemin) {
if (file_exists($chemin)) {
$service = new $classe($this->getBdd());
$ressourcesPourService = $this->filtrerRessourcesPourService();
$this->cache = new CacheMoissonnage($service, $this->nomSource, $this->nomService,
Config::get('cache'));
$retour = $this->cache->consulter($ressourcesPourService, $this->parametres);
if (get_class($retour) == 'Exception') {
throw new Exception($retour->getMessage(), RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
}
}
}
if (is_null($service)) {
$message = "La service demandé '{$this->nomService}' n'existe pas dans le projet {$this->nomSource} !";
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}
return $retour;
}
private function chargerNomDuService() {
$this->nomService = $this->ressources[1];
}
private function obtenirNomClasseService($mot) {
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
return $classeNom;
}
private function filtrerRessourcesPourService() {
$ressourcesPourService = array();
$nbreDeRessources = count($this->ressources);
for ($i = 2; $i < $nbreDeRessources; $i++) {
$ressourcesPourService[] = $this->ressources[$i];
}
return $ressourcesPourService;
}
}
?>