New file |
0,0 → 1,206 |
<?php |
/** |
* Description : |
* Classe principale de chargement des services d'eFlore. |
* |
* Encodage en entrée : utf8 |
* Encodage en sortie : utf8 |
* @package eflore-projets |
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org> |
* @author Delphine CAUQUIL <delphine@tela-botanica.org> |
* @author Jean-Pascal MILCENT <jpm@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> |
* @version 0.1 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org) |
*/ |
class Del extends RestService { |
|
private $parametres = array(); |
private $ressources = array(); |
private $projetNom = array(); |
private $serviceNom = array(); |
private $cheminCourant = null; |
|
private $conteneur; |
|
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */ |
protected $utilisationParametresBruts = true; |
|
public function __construct() { |
$this->cheminCourant = dirname(__FILE__).DS; |
} |
|
public function consulter($ressources, $parametres) { |
|
$resultat = ''; |
$reponseHttp = new ReponseHttp(); |
try { |
$this->initialiserRessourcesEtParametres($ressources, $parametres); |
$this->conteneur = new Conteneur($this->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->avoirRessourceProjet()) { |
$this->initialiserProjet(); |
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 = "Aucune ressource n'a été indiquée.\n". |
"Veuillez indiquer au moins un code de projet et un type de service."; |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE; |
throw new Exception($message, $code); |
} |
return $presenceDeRessources; |
} |
|
private function avoirRessourceProjet() { |
$presenceRessourceProjet = false; |
$projet = $this->ressources[0]; |
$projetsDispo = Outils::recupererTableauConfig('projetsDispo'); |
if (in_array($projet, $projetsDispo)) { |
$presenceRessourceProjet = true; |
} else { |
$message = "La ressource '$projet' n'indique pas un projet existant.\n". |
"Les projets existant sont :\n".implode(', ', $projetsDispo); |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE; |
throw new Exception($message, $code); |
} |
return $presenceRessourceProjet; |
} |
|
private function initialiserProjet() { |
$this->chargerNomDuProjet(); |
$this->chargerConfigProjet(); |
|
// 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, 'chargerClasseProjet')); |
} else { |
spl_autoload_register(array($this, 'chargerClasseProjet'), true , true); |
} |
} |
|
private function chargerNomDuProjet() { |
$this->projetNom = $this->ressources[0]; |
} |
|
private function chargerConfigProjet() { |
$projet = $this->projetNom; |
$chemin = Config::get('chemin_configurations')."config_$projet.ini"; |
Config::charger($chemin); |
} |
|
private function chargerClasseProjet($classe) { |
if (class_exists($classe)) { |
return null; |
} |
|
$cheminBiblio = Config::get('chemin_bibliotheque'); |
$chemins = array(); |
$chemins[] = $this->cheminCourant.$this->projetNom.DS; |
$chemins[] = $this->cheminCourant.'commun'.DS; |
$chemins[] = $cheminBiblio; |
$chemins[] = $cheminBiblio.'robots'.DS; |
|
foreach ($chemins as $chemin) { |
$chemin = $chemin.$classe.'.php'; |
if (file_exists($chemin)) { |
require_once $chemin; |
break; |
} |
} |
} |
|
private function avoirRessourceService() { |
$presenceRessourceService = false; |
$servicesDispo = Outils::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->projetNom} !\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->projetNom} !\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->serviceNom); |
$chemins = array(); |
$chemins[] = $this->cheminCourant.$this->projetNom.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'); |
$service = new $classe($this->conteneur, 'config_'.$this->projetNom.'.ini'); |
$ressourcesPourService = $this->filtrerRessourcesPourService(); |
$retour = $service->consulter($ressourcesPourService, $this->parametres); |
} |
} |
if (is_null($service)) { |
$message = "La service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !"; |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE; |
throw new Exception($message, $code); |
} |
return $retour; |
} |
|
private function chargerNomDuService() { |
$this->serviceNom = $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; |
} |
} |
?> |