Subversion Repositories Applications.referentiel

Rev

Rev 37 | Rev 53 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
// declare(encoding='UTF-8');
/**
* Classe modèle spécifique à l'application, donc d'accés au données, elle ne devrait pas être appelée de l'extérieur.
* Elle est abstraite donc doit obligatoirement être étendue.
*
* @category             Php5
* @package              Referentiel
* @author               Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @copyright    2010 Tela-Botanica
* @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              SVN: $Id$
*/
abstract class Dao {
        const HTTP_REQUETE_SEPARATEUR = '&';
        protected $distinction = '0';
        protected $limite_debut = null;
        protected $limite_nbre = null;
        protected $url_jrest = null;
        
        public function __construct() {
                $this->url_jrest = Config::get('url_jrest');
        }
        
        //+----------------------------------------------------------------------------------------------------------------+
        // ACCESSEURS
        
        public function avoirLimitation() {
                $limitation = false;
                if (!is_null($this->limite_debut) && !is_null($this->limite_nbre)) {
                        $limitation = true;
                }
                return $limitation;
        }
        
        public function setDistinction($distinct) {
                $this->distinction = $distinct;
        }
        
        public function getDistinction() {
                return $this->distinction;
        }
        
        public function setLimitation($limite_debut, $limite_nbre) {
                $this->limite_debut = $limite_debut;
                $this->limite_nbre = $limite_nbre;
        }
        
        public function getLimiteDebut() {
                return $this->limite_debut;
        }
        public function getLimiteNbre() {
                return $this->limite_nbre;
        }
        
        //+----------------------------------------------------------------------------------------------------------------+
        // MÉTHODES
        
        protected function envoyerRequeteConsultation($url) {
                $url = $this->traiterUrlParametres($url);
                $retour = $this->envoyerRequete($url, 'GET');
                return $retour;
        }
        
        protected function envoyerRequeteAjout($url, Array $donnees) {
                $retour = $this->envoyerRequete($url, 'PUT', $donnees);
                return $retour;
        }
        
        protected function envoyerRequeteModif($url, Array $donnees) {
                $retour = $this->envoyerRequete($url, 'POST', $donnees);
                return $retour;
        }
        
        protected function envoyerRequeteSuppression($url) {
                $retour = $this->envoyerRequete($url, 'DELETE');
                return $retour;
        }
        
        private function envoyerRequete($url, $mode, Array $donnees = array()) {
                $contenu = false;
                if ($mode != 'GET' && $mode != 'PUT' && $mode != 'POST' && $mode != 'DELETE') {
                        $e = "Le mode de requête '$mode' n'est pas accepté!";
                        trigger_error($e, E_USER_WARNING);
                } else {
                        $contexte = stream_context_create(array(
                                'http' => array(
                                'method' => $mode,
                                        'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                                'content' => http_build_query($donnees, null, self::HTTP_REQUETE_SEPARATEUR))));
                        $flux = @fopen($url, 'r', false, $contexte);
                        if (!$flux) {
                                $this->traiterEntete($http_response_header, $url);
                                $e = "L'ouverture de l'url '$url' a échoué!";
                                trigger_error($e, E_USER_WARNING);
                        } else {
                                // Informations sur les en-têtes et métadonnées du flux
                                $entetes = stream_get_meta_data($flux);
                                $this->traiterEntete($entetes, $url);
                                
                                // Contenu actuel de $url
                                $contenu = stream_get_contents($flux);
                                
                                fclose($flux);
                        }
                }
                return $contenu;
        }
        
        private function traiterUrlParametres($url) {
                $parametres = array();
                if (! is_null($this->getLimiteDebut())) {
                        $parametres[] = 'start='.$this->getLimiteDebut();
                }
                if (! is_null($this->getLimiteNbre())) {
                        $parametres[] = 'limit='.$this->getLimiteNbre();
                }
                if ($this->getDistinction() != 0) {
                        $parametres[] = 'distinct='.$this->getDistinction();
                }
                if (count($parametres) > 0) {
                        $url_parametres = implode('&', $parametres);
                        $url = $url.'?'.$url_parametres;
                }
                return $url;
        }
        
        private function traiterEntete($entetes, $uri) {
                $infos = $this->analyserEntete($entetes, $uri);
                $this->traiterEnteteDebug($infos);
                $this->traiterEnteteMessage($infos);
        }
        
        private function analyserEntete($entetes, $uri) {
                $infos = array('date' => null, 'uri' => $uri, 'debugs' => null, 'messages' => null);
                
                if (isset($entetes['wrapper_data'])) {
                        $entetes = $entetes['wrapper_data'];
                }
                foreach ($entetes as $entete) {
                        if (preg_match('/^X-DebugJrest-Data: (.+)$/', $entete, $match)) {
                                $infos['debugs'] = json_decode($match[1]);
                        }
                        if (preg_match('/^X-MessageJrest-Data: (.+)$/', $entete, $match)) {
                                $infos['messages'] = json_decode($match[1]);
                        }
                        if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
                                $infos['date'] = $match[1];
                        }
                }
                return $infos;
        }
        
        private function traiterEnteteDebug($entetes) {
                if (isset($entetes['debugs'])) {
                        $date = $entetes['date'];
                        $uri = $entetes['uri'];
                        $debugs = $entetes['debugs'];
                        foreach ($debugs as $debug) {
                                Debug::printr("DEBUG : $date - $uri :\n$debug");
                        }
                }
        }
        
        private function traiterEnteteMessage($entetes) {
                if (isset($entetes['messages'])) {
                        $date = $entetes['date'];
                        $uri = $entetes['uri'];
                        $messages = $entetes['messages'];
                        foreach ($messages as $message) {
                                Debug::printr("MESSAGE : $date - $uri :\n$message");
                        }
                }
        }
}