summaryrefslogtreecommitdiffstats
path: root/system/classes/debug.php
blob: 85a8956fe7ee2224e37e1bdd2b1c2b40f517aa04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

/**
 * Handles error reporting and debugging
 */
class Debug {

    /**
     * Caught exception
     * @var Exception 
     * @access public  
     */
	public $exception;

    /**
     * An array of logged items
     * @var array  
     * @access public 
     * @static 
     */
	public static $logged=array();

    /**
     * Displays the error page
     * 
     * @return void   
     * @access public 
     */
	public function render() {
		ob_end_clean();
		$view = View::get('debug');
		$view->exception = $this->exception;
		$view->log = Debug::$logged;
		header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); 
		header("Status: 404 Not Found");
		echo $view->render();
	}

    /**
     * Catches errors and exceptions and processes them
     * 
     * @param Exception $exception Caught exception
     * @return void    
     * @access public  
     * @static 
     */
	public static function onError($exception) {
		set_exception_handler(array('Debug', 'internalException'));
		set_error_handler ( array('Debug', 'internalError'), E_ALL);
		$error = new Debug();
		$error->exception = $exception;
		$error->render();
	}

    /**
     * Converts PHP Errors to Exceptions
     * 
     * @param string        $errno   Error number
     * @param string        $errstr  Error message
     * @param string        $errfile File in which the error occured
     * @param string        $errline Line at which the error occured
     * @return void           
     * @access public         
     * @throws ErrorException Throws converted exception to be immediately caught
     * @static 
     */
	public static function errorHandler($errno, $errstr, $errfile, $errline) {
		throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
	}

    /**
     * Handles exceptions that occured while inside the error handler. Prevents recursion.
     * 
     * @param Exception  $exception Caught exception
     * @return void   
     * @access public 
     * @static 
     */
	public static function internalException($exception) {
		echo $exception->getMessage().' in '.$exception->getFile().' on line '.$exception->getLine();
	}

    /**
     * Handles errors that occured while inside the error handler. Prevents recursion.
     * 
     * @param string        $errno   Error number
     * @param string        $errstr  Error message
     * @param string        $errfile File in which the error occured
     * @param string        $errline Line at which the error occured
     * @return void    
     * @access public  
     * @static 
     */
	public static function internalError($errno, $errstr, $errfile, $errline) {
		echo $errstr.' in '.$errfile.' on line '.$errline;
	}

    /**
     * Initializes the error handler
     * 
     * @return void   
     * @access public 
     * @static 
     */
	public static function init(){
		set_exception_handler(array('Debug', 'onError'));
		set_error_handler ( array('Debug', 'errorHandler'), E_ALL);
	}

    /**
     * Adds an item to the log.
     * 
     * @param mixed $val Item to be logged
     * @return void    
     * @access public  
     * @static 
     */
	public static function log($val){
		array_unshift(Debug::$logged,$val);
	}
	
}