2,42 → 2,42 |
// declare(encoding='UTF-8'); |
/** |
* classe de gestion des exceptions. |
* |
* PHP Version 5 |
* |
* |
* PHP Version 5 |
* |
* @category Class |
* @package Framework |
* @author aurelien <aurelien@tela-botanica.org> |
* @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$$ |
* @link /doc/framework/ |
* |
* @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$$ |
* @link /doc/framework/ |
* |
*/ |
class GestionnaireException { |
|
|
/** |
* Liste des exceptions enregistrées |
*/ |
private static $exceptions; |
|
private static $exceptions = array(); |
|
/** |
* Détermine si l'on affiche ou nom le contexte |
*/ |
private $contexte; |
|
private static $contexte = false; |
|
/** |
* Definit si php est lancé en ligne de commande ou en mode serveur |
*/ |
private $mode; |
|
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) |
44,26 → 44,28 |
*/ |
public function __construct($contexte = false) { |
self::$exceptions = array(); |
$this->contexte = $contexte; |
$this->mode = php_sapi_name(); |
set_exception_handler(array($this,'gererException')); |
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 function getContexte() { |
return $this->contexte; |
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) { |
$this->contexte = $contexte; |
self::$mode = $contexte; |
} |
|
|
/** Fonction d'accès au singleton |
* @return GestionnaireErreur le gestionnaire d'exceptions courant |
*/ |
72,33 → 74,54 |
return self::$gestionnaireException; |
} |
self::$gestionnaireException = new GestionnaireException; |
return self::$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 |
self::$exceptions[] = $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 function getExceptions() { |
public static function getExceptions() { |
$retour = ''; |
foreach (self::$exceptions as $e) { |
switch($this->mode) { |
case 'cli' : |
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 .= 'Ligne : '.$e->getLine()."\n"; |
$retour .= 'Message : '.$e->getMessage()."\n"; |
$retour .= 'Fichier : '.$e->getFile()."\n"; |
$retour .= 'Ligne : '.$e->getLine()."\n"; |
if ($this->getContexte()) { |
if (self::getContexte()) { |
$retour .= 'Contexte : '."\n".print_r($e->getTraceAsString(), true)."\n"; |
} |
break; |
108,8 → 131,8 |
$retour .= '<span class="debogage_fichier">'.'Fichier : '.$e->getFile().'</span>'."\n"; |
$retour .= '<span class="debogage_ligne">'.'Ligne : '.$e->getLine().'</span>'."\n"; |
$retour .= '</pre>'."\n"; |
|
if ($this->getContexte()) { |
|
if (self::getContexte()) { |
$retour .= '<pre>'."\n"; |
$retour .= '<strong>Contexte : </strong>'."\n".print_r($e->getTraceAsString(), true)."\n"; |
$retour .= '</pre>'."\n"; |
119,5 → 142,5 |
return $retour; |
} |
|
} |
} |
?> |