Subversion Repositories eFlore/Applications.del

Rev

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

<?php
// declare(encoding='UTF-8');
/**
* Classe principale de chargement des sous-services de Plantnet.
*
 * @category   DEL
 * @package    Services
 * @subpackage Plantnet
 * @version    0.1
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
 * @author     Samuel DUFOUR-KOWALSKI <samuel.dufour@cirad.fr>
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @author     Aurelien PERONNET <aurelien@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>
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
*/
class PlantNet extends RestService {

        private $parametres = array();
        private $ressources = array();
        private $methode = null;
        private $projetNom = 'plantnet';
        private $serviceNom = 'changements';
        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) {
                $this->methode = 'consulter';
                $reponseHttp = new ReponseHttp();
                try {
                        $this->ressources = $ressources;
                        $this->parametres = $parametres;

                        $this->conteneur = new Conteneur($this->parametres);

                        $resultat = $this->initialiserService();
                        $reponseHttp->setResultatService($resultat);
                } catch (Exception $e) {
                        $reponseHttp->ajouterErreur($e);
                }
                $reponseHttp->emettreLesEntetes();
                return $reponseHttp->getCorps();
        }

        /*------------------------------------------------------------------------------------------------------------------
                                                                CONFIGURATION DU SERVICE
        ------------------------------------------------------------------------------------------------------------------*/
        private function initialiserService() {
                $classe = $this->obtenirNomClasseService($this->serviceNom);
                $chemins = array();
                $chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
                $retour = '';
                $service = null;
                foreach ($chemins as $chemin) {
                        if (file_exists($chemin)) {
                                require_once $chemin;
                                $service = new $classe($this->conteneur);
                                $retour = $service->consulter($this->ressources, $this->parametres);
                        }
                }

                if (is_null($service)) {
                        $message = "Le 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 obtenirNomClasseService($mot) {
                $classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
                return $classeNom;
        }
}