Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 231 | Blame | Last modification | View Log | RSS feed

<?php
class Projet {
        private $ressources = null;
        private $paramsVerif = null;
        private $ressourcesVerif = null;
        private $versionVerif = null;
        private $cheminBase = '';
        private $cheminConfig = '';
        private $cheminBiblio = '';
        private $serviceGenerique = '';

        public function __construct(Ressources $ressources) {
                $this->ressources = $ressources;
        }

        public function setCheminBase($chemin) {
                $this->cheminBase = $chemin;
        }

        public function setCheminConfig($chemin) {
                $this->cheminConfig = $chemin;
        }

        public function setCheminBiblio($chemin) {
                $this->cheminBiblio = $chemin;
        }

        public function setParamsVerif($paramsVerificateur) {
                $this->paramsVerif = $paramsVerificateur;
        }

        public function setVersionVerif($versionVerificateur) {
                $this->versionVerif = $versionVerificateur;
        }

        public function setRessourcesVerif($ressourcesVerificateur) {
                $this->ressourcesVerif = $ressourcesVerificateur;
        }

        public function setServiceGenerique($generique) {
                $this->serviceGenerique = $generique;
        }

        public function initialiser() {
                $this->chargerConfig();
                // 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 chargerConfig() {
                $projet = $this->getNom();
                $chemin = $this->cheminConfig."config_$projet.ini";
                Config::charger($chemin);
        }

        public function getNom() {
                return $this->ressources->getProjetNom();
        }

        public function getClasse() {
                return $this->ressources->getServiceClasse().ucfirst($this->getNom());
        }

        private function chargerClasseProjet($classe) {
                if (class_exists($classe)) {
                        return null;
                }

                $chemins = array();
                $chemins[] = $this->cheminBase.$this->getNom().DS;
                $chemins[] = $this->cheminBase.'commun'.DS;
                $chemins[] = $this->cheminBiblio;
                $chemins[] = $this->cheminBiblio.'generique'.DS;
                $chemins[] = $this->cheminBiblio.'interfaces'.DS;
                $chemins[] = $this->cheminBiblio.'nom'.DS;
                $chemins[] = $this->cheminBiblio.'nom'.DS.'decorateurs'.DS;
                $chemins[] = $this->cheminBiblio.'ontologie'.DS;
                $chemins[] = $this->cheminBiblio.'ontologie'.DS.'decorateurs'.DS;

                foreach ($chemins as $chemin) {
                        $chemin = $chemin.$classe.'.php';
                        if (file_exists($chemin)) {
                                require_once $chemin;
                        }
                }
        }

        public function verifier() {
                $this->paramsVerif->verifier();
                $this->ressourcesVerif->verifier();
                $this->versionVerif->verifier();
                $this->verifierExistanceServiceClasse();
        }

        private function verifierExistanceServiceClasse() {
                $classe = $this->getClasse();
                $existe = $this->verifierExistanceClasseDuProjet($classe);

                if ($existe === false) {
                        $service = $this->ressources->getServiceNom();
                        $projet = $this->getNom();
                        $message = "La classe du service demandé '$service' n'existe pas dans le projet '$projet' !";
                        $code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
                        throw new Exception($message, $code);
                }
        }

        private function verifierExistanceClasseDuProjet($classe) {
                $chemins = array();
                $chemins[] = $this->cheminBase.$this->getNom().DS.$classe.'.php';
                $chemins[] = $this->cheminBase.'commun'.DS.$classe.'.php';

                $existe = false;
                foreach ($chemins as $chemin) {
                        if (file_exists($chemin)) {
                                $existe = true;
                                break;
                        }
                }
                return $existe;
        }

        public function consulter() {
                $serviceNom = $this->getClasse();
                $service = new $serviceNom($this->serviceGenerique);
                $retour = $service->consulter();
                return $retour;
        }

}
?>