-
Notifications
You must be signed in to change notification settings - Fork 16
/
observer-exception-handler.php
194 lines (168 loc) · 5.42 KB
/
observer-exception-handler.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* This is an example of using the observer pattern to handle exceptions. You
* can attach an arbitrary number of observers to the ExceptionHandler
* for handling exceptions in different ways. This could be extensible by
* adding more observers, or only having observers act on distinct types of
* exceptions.
*
* The ExceptionHandler class sends uncaught exception messages to the proper
* handlers. This is done using SplObserver/SplSubject.
*/
class ExceptionHandler implements SplSubject
{
/**
* An array of SplObserver objects to notify of Exceptions.
*
* @var array
*/
private $_observers = array();
/**
* The uncaught Exception that needs to be handled.
*
* @var Exception
*/
protected $_exception;
/**
* Constructor method for ExceptionHandler.
*
* @return ExceptionHandler
*/
function __construct() { }
/**
* A custom method for returning the exception.
*
* @access public
* @return Exception
*/
public function getException()
{
return $this->_exception;
}
/**
* Attaches an SplObserver to the ExceptionHandler to be notified when an
* uncaught Exception is thrown.
*
* @access public
* @param SplObserver The observer to attach
* @return void
*/
public function attach(SplObserver $obs)
{
$id = spl_object_hash($obs);
$this->_observers[$id] = $obs;
}
/**
* Detaches the SplObserver from the ExceptionHandler, so it will no longer
* be notified when an uncaught Exception is thrown.
*
* @access public
* @param SplObserver The observer to detach
* @return void
*/
public function detach(SplObserver $obs)
{
$id = spl_object_hash($obs);
unset($this->_observers[$id]);
}
/**
* Notify all observers of the uncaught Exception so they can handle it as
* needed.
*
* @access public
* @return void
*/
public function notify()
{
foreach($this->_observers as $obs) {
$obs->update($this);
}
}
/**
* This is the method that should be set as the default Exception handler by
* the calling code.
*
* @access public
* @return void
*/
public function handle(Exception $e)
{
$this->_exception = $e;
$this->notify();
}
}
/**
* The Logger exception handler is responsible for logging uncaught
* exceptions to a file for debugging. It is an extension of what
* would be your actual Logger class.
*/
class ExceptionLogger extends Logger implements SplObserver
{
/**
* Update the error_log with information about the Exception.
*
* @param SplSubject $subject The ExceptionHandler
* @return bool
*/
public function update(SplSubject $subject)
{
$exception = $subject->getException();
$output = 'File: ' . $exception->getFile() . PHP_EOL;
$output .= 'Line: ' . $exception->getLine() . PHP_EOL;
$output .= 'Message: ' . PHP_EOL . $exception->getMessage() . PHP_EOL;
$output .= 'Stack Trace:' . PHP_EOL . $exception->getTraceAsString() . PHP_EOL;
echo "\n\nThe following message was sent to your default PHP error log:\n\n";
echo $output;
return error_log($output);
}
}
/**
* The Mailer exception handler is responsible for mailing uncaught
* exceptions to an administrator for notifications. It is an extension
* of what would be your actual Mailer class.
*/
class ExceptionMailer extends Mailer implements SplObserver
{
/**
* Mail the sysadmin with Exception information.
*
* @param SplSubject $subject The ExceptionHandler
* @return bool
*/
public function update(SplSubject $subject)
{
$exception = $subject->getException();
// perhaps emailer also would like to know the server in question
$output = 'Server: ' . $_SERVER['HOSTNAME'] . PHP_EOL;
$output .= 'File: ' . $exception->getFile() . PHP_EOL;
$output .= 'Line: ' . $exception->getLine() . PHP_EOL;
$output .= 'Message: ' . PHP_EOL . $exception->getMessage() . PHP_EOL;
$output .= 'Stack Trace:' . PHP_EOL . $exception->getTraceAsString() . PHP_EOL;
$headers = 'From: webmaster@yourdomain.com' . "\r\n" .
'Reply-To: webmaster@yourdomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
echo "\n\nThe following email (would be) sent to your webmaster@yourdomain.com:\n\n";
echo $output;
//return mail('webmaster@yourdomain.com', 'Exception Thrown', $output, $headers);
}
}
/**
* Assume this Mailer class is your actual mailer class (i.e. SwiftMailer).
*/
class Mailer { }
/**
* Assume this Logger class is your actual logger class.
*/
class Logger { }
//====================================
// BELOW THIS LINE RUNS THE ABOVE CODE
//====================================
// Create the ExceptionHandler
$handler = new ExceptionHandler();
// Attach an Exception Logger and Mailer
$handler->attach(new ExceptionLogger());
$handler->attach(new ExceptionMailer());
// Set ExceptionHandler::handle() as the default
set_exception_handler(array($handler, 'handle'));
// throw an exception for handling
throw new Exception("This is a test of the emergency broadcast system\n", 0);