Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 26 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php


class Projets extends RestService {

        
        private $parametres = array();
        private $ressources = array();
        
        private $squeletteDossier = '';
        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->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 d'informations a renvoyer.";
                        $code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
                        throw new Exception($message, $code);
                }
                return $presenceDeRessources;
        }
        
        private function chargerConfiguration($nomParametre) {
                $tableau = array();
                $tableauPartiel = explode(',', Config::get($nomParametre));
                $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;
        }
        
        public static function chargerConfigurationSource($nomSource) {
                $chemin = Config::get('chemin_configurations')."config_$nomSource.ini";
                Config::charger($chemin);
        }
        
        private function chargerClasse($classe) {
                if (class_exists($classe)) {
                        return null;
                }
                
                $cheminBiblio = Config::get('chemin_bibliotheque');
                $chemins = array();
                $chemins[] = $this->squeletteDossier;
                $chemins[] = $this->squeletteDossier.'commun' .DIRECTORY_SEPARATOR;
                $chemins[] = $this->squeletteDossier.'sources'.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->chargerConfiguration('services_dispo');
                if (isset($this->ressources[0])) {
                        $service = $this->ressources[0];
                        if (in_array($service, $servicesDispo)) {
                                $presenceRessourceService = true;
                        } else {
                                $message = "La service demandé '$service' n'est pas disponible !\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 !\n"
                                . "Les services disponibles sont : ".implode(', ', $servicesDispo);
                        $code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
                        throw new Exception($message, $code);
                }
                return $presenceRessourceService;
        }

        private function initialiserService() {
                $nomService = 'commun';
                $classe = $this->obtenirNomClasseService($nomService);
                $chemins = array();
                $chemins[] = $this->squeletteDossier.'commun'.DIRECTORY_SEPARATOR.'Commun.php';
                
                // 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, 'chargerClasse'));
                } else {
                        spl_autoload_register(array($this, 'chargerClasse'), true , true);
                }
        
                $retour = '';
                $service = null;
                foreach ($chemins as $chemin) {
                        if (file_exists($chemin)) {
                                $service = new $classe($this->getBdd());
                                $ressourcesPourService = $this->filtrerRessourcesPourService();
                                $this->cache = new CacheMoissonnage($service, $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 = "Le service demandé '{$nomService}' n'existe pas !";
                        $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 filtrerRessourcesPourService() {
                return $this->ressources;
        }
        
}

?>