Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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

        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 setRessourcesVerif($ressourcesVerificateur) {
                $this->ressourcesVerif = $ressourcesVerificateur;
        }

        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();
        }

        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.'interfaces'.DS;
                $chemins[] = $this->cheminBiblio.'nom'.DS;
                $chemins[] = $this->cheminBiblio.'nom'.DS.'decorateurs'.DS;

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

        public function getServiceClasseNom() {
                $classeNom = '';
                if ($this->ressources->getNombre() == 2) {
                        if ($this->ressources->getServiceNom() == 'noms') {
                                $classeNom = 'NomsListe';
                        } else if ($this->ressources->getServiceNom() == 'taxons') {
                                $classeNom = 'TaxonsListe';
                        }

                } else if ($this->ressources->getNombre() == 3) {
                        $position3 = $this->ressources->getParPosition(2);
                        if ($this->ressources->etreId($position3)) {
                                if ($this->ressources->getServiceNom() == 'noms') {
                                        $classeNom = 'NomDetails';
                                } else if ($this->ressources->getServiceNom() == 'taxons') {
                                        $classeNom = 'TaxonDetails';
                                }
                        }
                } else if ($this->ressources->getNombre() == 4) {
                        $position3 = $this->ressources->getParPosition(2);
                        $position4 = $this->ressources->getParPosition(3);
                        if ($this->ressources->etreStats($position3)) {
                                if ($this->ressources->etreTypeDeStats($position4)) {
                                        $classeNom = 'NomsStats'.ucfirst($position4);
                                }
                        } else if ($this->ressources->etreId($position3)) {
                                if ($this->ressources->etreRelations($position4)) {
                                        $classeNom = 'NomRelations';
                                }
                        }
                } else if ($this->ressources->getNombre() == 5) {
                        $position3 = $this->ressources->getParPosition(2);
                        $position4 = $this->ressources->getParPosition(3);
                        $position5 = $this->ressources->getParPosition(4);
                        if ($this->ressources->etreId($position3)) {
                                if ($this->ressources->etreRelations($position4)) {
                                        if ($this->ressources->etreTypeDeRelations($position5)) {
                                                $classeNom = 'NomRelations'.ucfirst($position5);
                                        }
                                }
                        }
                }
                $classeNomProjet = $classeNom.'Generique';//.ucfirst($this->getNom());
                return $classeNomProjet;
        }

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

        private function verifierExistanceServiceClasse() {
                $service = $this->ressources->getServiceNom();
                $classe = $this->getServiceClasseNom();
                $projet = $this->getNom();

                $chemins = array();
                $chemins[] = $this->cheminBase.$projet.DS.$classe.'.php';
                $chemins[] = $this->cheminBase.'commun'.DS.$classe.'.php';

                $existe = false;
                foreach ($chemins as $chemin) {
                        if (file_exists($chemin)) {
                                $existe = true;
                                break;
                        }
                }
                if ($existe === false) {
                        $message = "La classe '$classe' du service demandé '$service' n'existe pas dans le projet '$projet' !";
                        $code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
                        throw new Exception($message, $code);
                }
        }

}
?>