Subversion Repositories Applications.framework

Rev

Rev 115 | Rev 122 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 115 Rev 120
Line 1... Line 1...
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
* classe de gestion des exceptions.
4
* classe de gestion des exceptions.
5
* 
5
*
6
* PHP Version 5 
6
* PHP Version 5
7
* 
7
*
8
* @category  Class
8
* @category  Class
9
* @package   Framework
9
* @package   Framework
10
* @author    aurelien <aurelien@tela-botanica.org>
10
* @author	aurelien <aurelien@tela-botanica.org>
11
* @copyright 2009 Tela-Botanica
11
* @copyright 2009 Tela-Botanica
12
* @license   http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL  
12
* @license   http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
13
* @license   http://www.gnu.org/licenses/gpl.html Licence GNU-GPL  
13
* @license   http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
14
* @version   SVN: $$Id: GestionnaireException.php 115 2009-09-01 09:02:25Z jp_milcent $$ 
14
* @version   SVN: $$Id: GestionnaireException.php 120 2009-09-01 12:21:14Z aurelien $$
15
* @link      /doc/framework/ 
15
* @link	  /doc/framework/
16
* 
16
*
17
*/
17
*/
18
class GestionnaireException {
18
class GestionnaireException {
19
	
19
 
20
	/**
20
	/**
21
	 * Liste des exceptions enregistrées
21
	 * Liste des exceptions enregistrées
22
	 */
22
	 */
23
	private static $exceptions;
23
	private static $exceptions = array();
24
	
24
 
25
	/**
25
	/**
26
	 * Détermine si l'on affiche ou nom le contexte
26
	 * Détermine si l'on affiche ou nom le contexte
27
	 */
27
	 */
28
	private $contexte;
28
	private static $contexte = false;
29
	
29
 
30
	/**
30
	/**
31
	 * Definit si php est lancé en ligne de commande ou en mode serveur
31
	 * Definit si php est lancé en ligne de commande ou en mode serveur
32
	 */
32
	 */
33
	private $mode;		
33
	private static $mode = php_sapi_name ;
34
	
34
 
35
	/**
35
	/**
36
	 * le gestionnaire d'exception est un singleton
36
	 * le gestionnaire d'exception est un singleton
37
	 * et possède donc un "pointeur statique sur lui-même"
37
	 * et possède donc un "pointeur statique sur lui-même"
38
	 */
38
	 */
39
	private static $gestionnaireException;
39
	private static $gestionnaireException;
40
	
40
 
41
	/**
41
	/**
42
	 * Constructeur avec paramètres optionnel
42
	 * Constructeur avec paramètres optionnel
43
	 * @param bool indique si l'on veut afficher ou non le contexte des exceptions (i.e. la trace)
43
	 * @param bool indique si l'on veut afficher ou non le contexte des exceptions (i.e. la trace)
44
	 */
44
	 */
45
	public function __construct($contexte = false) {
45
	public function __construct($contexte = false) {
46
		self::$exceptions = array();
46
		self::$exceptions = array();
47
		$this->contexte = $contexte;
47
		self::$mode = $contexte;
48
		$this->mode = php_sapi_name();
48
		self::$mode = php_sapi_name();
-
 
49
 
49
		set_exception_handler(array($this,'gererException'));
50
		set_exception_handler(array(get_class($this),'gererException'));
-
 
51
		set_error_handler(array(get_class($this),'gererErreur'));
50
	}
52
	}
51
	
53
 
52
	/**
54
	/**
53
	 * Renvoie le booleen définissant si l'on affiche le contexte ou non
55
	 * Renvoie le booleen définissant si l'on affiche le contexte ou non
54
	 */
56
	 */
55
	public function getContexte() {
57
	public static function getContexte() {
56
		return $this->contexte;
58
		return self::$mode;
57
	}
59
	}
58
	
60
 
59
	/**
61
	/**
60
	 * Definit si l'on veut afficher le contexte ou non
62
	 * Definit si l'on veut afficher le contexte ou non
61
	 * @param bool true si on veut afficher le contexte, false sinon, par défaut vaut false
63
	 * @param bool true si on veut afficher le contexte, false sinon, par défaut vaut false
62
	 */
64
	 */
63
	public function setContexte($contexte) {
65
	public function setContexte($contexte) {
64
		$this->contexte = $contexte;
66
		self::$mode = $contexte;
65
	}
67
	}
66
	
68
 
67
	 /** Fonction d'accès au singleton
69
	 /** Fonction d'accès au singleton
68
	 * @return GestionnaireErreur le gestionnaire d'exceptions courant
70
	 * @return GestionnaireErreur le gestionnaire d'exceptions courant
69
	 */
71
	 */
70
	public static function getInstance() {
72
	public static function getInstance() {
71
		if (self::$gestionnaireException instanceof GestionnaireException) {
73
		if (self::$gestionnaireException instanceof GestionnaireException) {
72
			return self::$gestionnaireException;
74
			return self::$gestionnaireException;
73
		}
75
		}
74
		self::$gestionnaireException = new GestionnaireException;
76
		self::$gestionnaireException = new GestionnaireException;
-
 
77
 
75
	    return self::$gestionnaireException;
78
		return self::$gestionnaireException;
76
	}
79
	}
77
	
80
 
78
	/**
81
	/**
79
	 * Fonction de gestion des exceptions, remplace le handler par défaut
82
	 * Fonction de gestion des exceptions, remplace le handler par défaut
80
	 */
83
	 */
81
	public static function gererException(Exception $e) {
84
	public static function gererException(Exception $e) {
-
 
85
 
82
		// pour le moment on se contente de l'ajouter au tableau
86
		// pour le moment on se contente de l'ajouter au tableau et de les afficher
83
		self::$exceptions[] = $e;	
87
		self::$exceptions[] = $e;
-
 
88
 
-
 
89
		if($e->getCode() == 0 || $e->getCode() >= error_reporting()) {
-
 
90
			echo $e->getCode().'   '.error_reporting();
-
 
91
			echo self::getExceptions();
-
 
92
			return;
-
 
93
		}
-
 
94
	}
-
 
95
 
-
 
96
	public static function gererErreur($niveau,  $message,  $fichier,  $ligne,  $contexte){
-
 
97
		// si aucun rapport d'error, on sort directement
-
 
98
		if (error_reporting() == 0)
-
 
99
		{
-
 
100
			return;
-
 
101
		}
-
 
102
 
-
 
103
		// sinon on crée une exception
-
 
104
		$e = new ErrorException($message, 0, $niveau, $fichier, $ligne);
-
 
105
		// que l'on donne au tableau d'exceptions
-
 
106
		self::$exceptions[] = $e;
84
	}
107
	}
85
	
108
 
86
	/**
109
	/**
87
	 * Renvoie les exceptions au format (X)HTML
110
	 * Renvoie les exceptions au format (X)HTML
88
	 * ou bien au format texte suivant le mode d'utilisation de PHP
111
	 * ou bien au format texte suivant le mode d'utilisation de PHP
89
	 */
112
	 */
90
	public function getExceptions() {
113
	public static function getExceptions() {
91
		$retour = '';
114
		$retour = '';
92
		foreach (self::$exceptions as $e) { 
115
		foreach (self::$exceptions as $e) {
93
			switch($this->mode) {
116
			switch(self::$mode) {
94
				case 'cli' : 
117
				case 'cli' :
95
					$retour .= $e->getMessage()."\n";
118
					$retour .= $e->getMessage()."\n";
96
					$retour .= 'Fichier : '.$e->getFile()."\n";
119
					$retour .= 'Fichier : '.$e->getFile()."\n";
97
					$retour .= 'Ligne : '.$e->getLine()."\n";						
120
					$retour .= 'Ligne : '.$e->getLine()."\n";
98
					$retour .= 'Message : '.$e->getMessage()."\n";
121
					$retour .= 'Message : '.$e->getMessage()."\n";
99
					$retour .= 'Fichier : '.$e->getFile()."\n";
122
					$retour .= 'Fichier : '.$e->getFile()."\n";
100
					$retour .= 'Ligne : '.$e->getLine()."\n";
123
					$retour .= 'Ligne : '.$e->getLine()."\n";
101
					if ($this->getContexte()) {
124
					if (self::getContexte()) {
102
						$retour .= 'Contexte : '."\n".print_r($e->getTraceAsString(), true)."\n";
125
						$retour .= 'Contexte : '."\n".print_r($e->getTraceAsString(), true)."\n";
103
					}
126
					}
104
					break;
127
					break;
105
				default:
128
				default:
106
					$retour .= '<pre class="debogage">'."\n";
129
					$retour .= '<pre class="debogage">'."\n";
107
					$retour .= htmlentities($e->getMessage())."\n";
130
					$retour .= htmlentities($e->getMessage())."\n";
108
					$retour .= '<span class="debogage_fichier">'.'Fichier : '.$e->getFile().'</span>'."\n";
131
					$retour .= '<span class="debogage_fichier">'.'Fichier : '.$e->getFile().'</span>'."\n";
109
					$retour .= '<span class="debogage_ligne">'.'Ligne : '.$e->getLine().'</span>'."\n";
132
					$retour .= '<span class="debogage_ligne">'.'Ligne : '.$e->getLine().'</span>'."\n";
110
					$retour .= '</pre>'."\n";
133
					$retour .= '</pre>'."\n";
111
							
134
 
112
					if ($this->getContexte()) {
135
					if (self::getContexte()) {
113
						$retour .= '<pre>'."\n";
136
						$retour .= '<pre>'."\n";
114
						$retour .= '<strong>Contexte : </strong>'."\n".print_r($e->getTraceAsString(), true)."\n";
137
						$retour .= '<strong>Contexte : </strong>'."\n".print_r($e->getTraceAsString(), true)."\n";
115
						$retour .= '</pre>'."\n";
138
						$retour .= '</pre>'."\n";
116
					}
139
					}
117
			}
140
			}
118
		}
141
		}
119
		return $retour;
142
		return $retour;
120
	}
143
	}
Line 121... Line 144...
121
 
144
 
122
}  
145
}