Subversion Repositories eFlore/Applications.cel

Rev

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

<?php
/**
 * Le conteneur encapsule les classes et les paramètres de config.
 * Il gère leur instanciation, ainsi que la récupération des paramètres depuis les fichiers de configuration.
 *
 * @category php 5.2
 * @package cel
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @copyright Copyright (c) 2012, Tela Botanica (accueil@tela-botanica.org)
 * @license     http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
 * @license     http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
 * @version     $Id$
 */
class Conteneur {
        protected $parametres = array();
        protected $partages = array();

        public function __construct(array $parametres = null) {
                $this->parametres = is_null($parametres) ? array() : $parametres;
        }

        public function getParametre($cle) {
                $valeur = isset($this->parametres[$cle]) ? $this->parametres[$cle] : null;
                if (is_null($valeur) && $this->etreParametreASection($cle)) {
                        $valeur = $this->getParametreAvecSection($cle);
                }
                return $valeur;
        }

        private function etreParametreASection($cle) {
                return strpos($cle, '.') ? true : false;
        }

        private function getParametreAvecSection($cle) {
                $cleComposee = explode('.', $cle);
                $valeur = $this->parametres;
                foreach ($cleComposee as $cleCourrante) {
                        $valeur = isset($valeur[$cleCourrante]) ? $valeur[$cleCourrante] : null;
                }
                return $valeur;
        }

        public function getParametreTableau($cle) {
                $tableau = array();
                $parametre = $this->getParametre($cle);
                if (empty($parametre) === false) {
                        $tableauPartiel = explode(',', $parametre);
                        $tableauPartiel = array_map('trim', $tableauPartiel);
                        foreach ($tableauPartiel as $champ) {
                                if (strpos($champ, '=') === false) {
                                        $tableau[] = trim($champ);
                                } else {
                                        list($cle, $val) = explode('=', $champ);
                                        $tableau[trim($cle)] = trim($val);
                                }
                        }
                }
                return $tableau;
        }

        public function setParametre($cle, $valeur) {
                $this->parametres[$cle] = $valeur;
        }

        public function getBdd() {
                if (!isset($this->partages['Bdd'])){
                        $this->partages['Bdd'] = new Bdd($this->getParametre('database_cel'));
                }
                return $this->partages['Bdd'];
        }

        public function getScript() {
                if (!isset($this->partages['Script'])){
                        $this->partages['Script'] = new Script();
                }
                return $this->partages['Script'];
        }
}
?>