Subversion Repositories Applications.framework

Rev

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

<?php
// declare(encoding='UTF-8');
/**
* classe de gestion des exceptions.
*
* PHP Version 5
*
* @category  Class
* @package   Framework
* @author       aurelien <aurelien@tela-botanica.org>
* @copyright 2009 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: GestionnaireException.php 120 2009-09-01 12:21:14Z aurelien $$
* @link   /doc/framework/
*
*/
class GestionnaireException {

        /**
         * Liste des exceptions enregistrées
         */
        private static $exceptions = array();

        /**
         * Détermine si l'on affiche ou nom le contexte
         */
        private static $contexte = false;

        /**
         * Definit si php est lancé en ligne de commande ou en mode serveur
         */
        private static $mode = php_sapi_name ;

        /**
         * le gestionnaire d'exception est un singleton
         * et possède donc un "pointeur statique sur lui-même"
         */
        private static $gestionnaireException;

        /**
         * Constructeur avec paramètres optionnel
         * @param bool indique si l'on veut afficher ou non le contexte des exceptions (i.e. la trace)
         */
        public function __construct($contexte = false) {
                self::$exceptions = array();
                self::$mode = $contexte;
                self::$mode = php_sapi_name();

                set_exception_handler(array(get_class($this),'gererException'));
                set_error_handler(array(get_class($this),'gererErreur'));
        }

        /**
         * Renvoie le booleen définissant si l'on affiche le contexte ou non
         */
        public static function getContexte() {
                return self::$mode;
        }

        /**
         * Definit si l'on veut afficher le contexte ou non
         * @param bool true si on veut afficher le contexte, false sinon, par défaut vaut false
         */
        public function setContexte($contexte) {
                self::$mode = $contexte;
        }

         /** Fonction d'accès au singleton
         * @return GestionnaireErreur le gestionnaire d'exceptions courant
         */
        public static function getInstance() {
                if (self::$gestionnaireException instanceof GestionnaireException) {
                        return self::$gestionnaireException;
                }
                self::$gestionnaireException = new GestionnaireException;

                return self::$gestionnaireException;
        }

        /**
         * Fonction de gestion des exceptions, remplace le handler par défaut
         */
        public static function gererException(Exception $e) {

                // pour le moment on se contente de l'ajouter au tableau et de les afficher
                self::$exceptions[] = $e;

                if($e->getCode() == 0 || $e->getCode() >= error_reporting()) {
                        echo $e->getCode().'   '.error_reporting();
                        echo self::getExceptions();
                        return;
                }
        }

        public static function gererErreur($niveau,  $message,  $fichier,  $ligne,  $contexte){
                // si aucun rapport d'error, on sort directement
                if (error_reporting() == 0)
                {
                        return;
                }

                // sinon on crée une exception
                $e = new ErrorException($message, 0, $niveau, $fichier, $ligne);
                // que l'on donne au tableau d'exceptions
                self::$exceptions[] = $e;
        }

        /**
         * Renvoie les exceptions au format (X)HTML
         * ou bien au format texte suivant le mode d'utilisation de PHP
         */
        public static function getExceptions() {
                $retour = '';
                foreach (self::$exceptions as $e) {
                        switch(self::$mode) {
                                case 'cli' :
                                        $retour .= $e->getMessage()."\n";
                                        $retour .= 'Fichier : '.$e->getFile()."\n";
                                        $retour .= 'Ligne : '.$e->getLine()."\n";
                                        $retour .= 'Message : '.$e->getMessage()."\n";
                                        $retour .= 'Fichier : '.$e->getFile()."\n";
                                        $retour .= 'Ligne : '.$e->getLine()."\n";
                                        if (self::getContexte()) {
                                                $retour .= 'Contexte : '."\n".print_r($e->getTraceAsString(), true)."\n";
                                        }
                                        break;
                                default:
                                        $retour .= '<pre class="debogage">'."\n";
                                        $retour .= htmlentities($e->getMessage())."\n";
                                        $retour .= '<span class="debogage_fichier">'.'Fichier : '.$e->getFile().'</span>'."\n";
                                        $retour .= '<span class="debogage_ligne">'.'Ligne : '.$e->getLine().'</span>'."\n";
                                        $retour .= '</pre>'."\n";

                                        if (self::getContexte()) {
                                                $retour .= '<pre>'."\n";
                                                $retour .= '<strong>Contexte : </strong>'."\n".print_r($e->getTraceAsString(), true)."\n";
                                                $retour .= '</pre>'."\n";
                                        }
                        }
                }
                return $retour;
        }

}
?>