This repository has been archived by the owner on Nov 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Errbit.php
233 lines (202 loc) · 5.62 KB
/
Errbit.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Errbit PHP Notifier.
*
* Copyright © Flippa.com Pty. Ltd.
* See the LICENSE file for details.
*/
require_once dirname(__FILE__) . '/Errbit/Exception.php';
require_once dirname(__FILE__) . '/Errbit/XmlBuilder.php';
require_once dirname(__FILE__) . '/Errbit/Notice.php';
require_once dirname(__FILE__) . '/Errbit/Errors/Base.php';
require_once dirname(__FILE__) . '/Errbit/Errors/Notice.php';
require_once dirname(__FILE__) . '/Errbit/Errors/Warning.php';
require_once dirname(__FILE__) . '/Errbit/Errors/Error.php';
require_once dirname(__FILE__) . '/Errbit/Errors/Fatal.php';
require_once dirname(__FILE__) . '/Errbit/ErrorHandlers.php';
/**
* The Errbit client.
*
* @example Configuring the client
* Errbit::instance()->configure(array( ... ))->start();
*
* @example Notify an Exception manually
* Errbit::instance()->notify($exception);
*/
class Errbit {
private static $_instance = null;
/**
* Get a singleton instance of the client.
*
* This is the intended way to access the Errbit client.
*
* @return [Errbit]
* a singleton
*/
public static function instance() {
if (!isset(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
const VERSION = '0.0.1';
const API_VERSION = '2.2';
const PROJECT_NAME = 'errbit-php';
const PROJECT_URL = 'https://github.com/flippa/errbit-php';
const NOTICES_PATH = '/notifier_api/v2/notices/';
private $_config;
private $_observers = array();
/**
* Initialize a new client with the given config.
*
* This is made public for flexibility, though it is not expected you
* should use it.
*
* @param [Array] $config
* the configuration for the API
*/
public function __construct($config = array()) {
$this->_config = $config;
}
/**
* Add a handler to be invoked after a notification occurs.
*
* @param [Callback] $callback
* any callable function
*
* @return [Errbit]
* the current instance
*/
public function onNotify($callback) {
if (!is_callable($callback)) {
throw new Errbit_Exception('Notify callback must be callable');
}
$this->_observers[] = $callback;
return $this;
}
/**
* Set the full configuration for the client.
*
* The only required keys are `api_key' and `host', but other supported
* options are:
*
* - api_key
* - host
* - port
* - secure
* - project_root
* - environment_name
* - url
* - controller
* - action
* - session_data
* - parameters
* - cgi_data
* - params_filters
* - backtrace_filters
*
* @param [Array] $config
* the full configuration
*
* @return [Errbit]
* the current instance of the client
*/
public function configure($config = array()) {
$this->_config = array_merge($this->_config, $config);
$this->_checkConfig();
return $this;
}
/**
* Register all error handlers around this instance.
*
* @param [Array] $handlers
* an array of handler names (one or all of 'exception', 'error', 'fatal')
*
* @return [Errbit]
* the current instance
*/
public function start($handlers = array('exception', 'error', 'fatal')) {
$this->_checkConfig();
Errbit_ErrorHandlers::register($this, $handlers);
return $this;
}
/**
* Notify an individual exception manually.
*
* @param [Exception] $exception
* the Exception to notify (errors must first be converted)
*
* @param [Array] $options
* an array of options, which override the client configuration
*
* @return [Errbit]
* the current instance
*/
public function notify($exception, $options = array()) {
$config = array_merge($this->_config, $options);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $this->_buildApiUrl(),
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $this->_buildNoticeFor($exception, $config),
CURLOPT_HTTPHEADER => array(
'Content-Type: text/xml',
'Accept: text/xml, application/xml'
)
));
curl_exec($ch);
foreach ($this->_observers as $observer) {
$observer($exception, $config);
}
return $this;
}
// -- Private Methods
private function _checkConfig() {
if (empty($this->_config['api_key'])) {
throw new Errbit_Exception("`api_key' must be configured");
}
if (empty($this->_config['host'])) {
throw new Errbit_Exception("`host' must be configured");
}
if (empty($this->_config['port'])) {
$this->_config['port'] = !empty($this->_config['secure']) ? 443 : 80;
}
if (!isset($this->_config['secure'])) {
$this->_config['secure'] = ($this->_config['port'] == 443);
}
if (empty($this->_config['hostname'])) {
$this->_config['hostname'] = gethostname() ? gethostname() : '<unknown>';
}
if (empty($this->_config['project_root'])) {
$this->_config['project_root'] = dirname(__FILE__);
}
if (empty($this->_config['environment_name'])) {
$this->_config['environment_name'] = 'development';
}
if (!isset($this->_config['params_filters'])) {
$this->_config['params_filters'] = array('/password/');
}
if (!isset($this->_config['backtrace_filters'])) {
$this->_config['backtrace_filters'] = array(
sprintf('/^%s/', preg_quote($this->_config['project_root'], '/')) => '[PROJECT_ROOT]'
);
}
}
private function _buildApiUrl() {
$this->_checkConfig();
return implode(
'',
array(
$this->_config['secure'] ? 'https://' : 'http://',
$this->_config['host'],
':' . $this->_config['port'],
self::NOTICES_PATH
)
);
}
private function _buildNoticeFor($exception, $options) {
return Errbit_Notice::forException($exception, $options)->asXml();
}
}