Skip to content
Browse files

Simplified configuration and moved examples to directory

  • Loading branch information...
1 parent ec9f2f0 commit 29c914173a0861aaae599e7764a7f9f015bc6eed Andrew Kane committed Feb 8, 2011
Showing with 74 additions and 68 deletions.
  1. +20 −16 README.md
  2. +2 −2 example_advanced.php → examples/advanced.php
  3. +1 −1 example_simple.php → examples/simple.php
  4. +40 −38 exceptional.php
  5. +11 −11 exceptional/remote.php
View
36 README.md
@@ -1,44 +1,48 @@
# Exceptional-PHP
-PHP wrapper for the http://getexceptional.com API (for PHP 5.1.0+)
+You can have the power of [Exceptional](http://getexceptional.com) in PHP.
-### Super simple setup
+## Super simple setup
require "path/to/exceptional.php";
Exceptional::setup("YOUR-API-KEY");
-
-You can also pass additional configuration options.
- // turn errors off in development
- define("PHP_ENV", ...); // code to determine environment
- $send_errors = (PHP_ENV == "production"); // defaults to true
+You can turn off exception notifications by passing an empty string for the API key. This is great for development.
- // turn ssl on (requires openssl)
- $use_ssl = true; // defaults to false
+ if (PHP_ENV == "production") {
+ $api_key = "YOUR-API-KEY"
+ }
+ else {
+ $api_key = ""
+ }
- Exceptional::setup("YOUR-API-KEY", $send_errors, $use_ssl);
+ Exceptional::setup($api_key);
-### Exceptions and errors!
+You can turn on SSL by setting the second parameter to `true`.
+
+ Exceptional::setup($api_key, true);
+
+## Exceptions and errors!
Exceptional-PHP catches both errors and exceptions. You can control which errors are caught. If you want to ignore certain errors, use `error_reporting()`. Here are some common settings:
error_reporting(E_ALL & ~E_STRICT); // ignore strict errors
error_reporting(E_ALL & ~E_NOTICE); // ignore notices
error_reporting(E_ALL & ~(E_STRICT|E_NOTICE)); // ignore both
-Custom error and exception handlers are supported - see example_advanced.php.
+Custom error and exception handlers are supported - see examples/advanced.php.
Fatal and parse errors are caught, too - as long the setup file parses correctly.
-### 404 Support
+## 404 support
Add the following code to your 404 handler to track 404 errors:
throw new Http404Error();
-
-### Send extra data
+
+## Send extra data
$context = array(
"user_id" => 1
);
- Exceptional::context($context);
+ Exceptional::context($context);
View
4 example_advanced.php → examples/advanced.php
@@ -16,8 +16,8 @@ function my_exception_handler($exception) {
// setup Exceptional with the following two lines
// this code must come **after** you set custom error/exception handlers
-require dirname(__FILE__) . "/exceptional.php";
-Exceptional::setup("YOUR-API-KEY", true, true); // use ssl
+require dirname(__FILE__) . "/../exceptional.php";
+Exceptional::setup("YOUR-API-KEY", true); // use ssl
// add context
View
2 example_simple.php → examples/simple.php
@@ -1,7 +1,7 @@
<?php
// setup Exceptional with the following two lines
-require dirname(__FILE__) . "/exceptional.php";
+require dirname(__FILE__) . "/../exceptional.php";
Exceptional::setup("YOUR-API-KEY");
// control which errors are caught with error_reporting
View
78 exceptional.php
@@ -3,31 +3,31 @@
* An interface to getexceptional.com's API.
*
* PHP version 5.1.0+
- *
+ *
* All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
+ *
+ * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
- * - Neither the name of the The PEAR Group nor the names of its contributors
- * may be used to endorse or promote products derived from this software
+ * - Neither the name of the The PEAR Group nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* PHP implementation of Exceptional
@@ -42,32 +42,34 @@
class Exceptional
{
-
+
static $exceptions = array();
static $previous_exception_handler;
static $previous_error_handler;
static $api_key;
- static $send_errors;
static $use_ssl;
-
+
static $host = "plugin.getexceptional.com";
static $client_name = "exceptional-php";
static $version = "1.2";
static $protocol_version = 5;
-
+
static $context = array();
static $debugging = false;
-
+
/*
* Installs Exceptional as the default exception handler
*/
- static function setup($api_key, $send_errors = true, $use_ssl = false)
- {
+ static function setup($api_key, $use_ssl = false)
+ {
+ if ($api_key == "") {
+ $api_key = null;
+ }
+
self::$api_key = $api_key;
- self::$send_errors = $send_errors;
self::$use_ssl = $use_ssl;
// set exception handler & keep old exception handler around
@@ -78,18 +80,18 @@ static function setup($api_key, $send_errors = true, $use_ssl = false)
self::$previous_error_handler = set_error_handler(
array("Exceptional", "handle_error")
);
-
+
register_shutdown_function(
array("Exceptional", "shutdown")
);
}
-
+
static function shutdown()
{
if ($e = error_get_last()) {
self::handle_error($e["type"], $e["message"], $e["file"], $e["line"]);
}
-
+
if (!is_array(self::$exceptions)) {
return;
}
@@ -109,34 +111,34 @@ static function handle_error($errno, $errstr, $errfile, $errline)
// this error code is not included in error_reporting
return;
}
-
+
if (!class_exists("PhpError")) {
require dirname(__FILE__)."/exceptional/php_errors.php";
}
-
+
switch ($errno) {
case E_NOTICE:
case E_USER_NOTICE:
$ex = new PhpNotice($errstr, $errno, $errfile, $errline);
break;
-
+
case E_WARNING:
case E_USER_WARNING:
$ex = new PhpWarning($errstr, $errno, $errfile, $errline);
break;
-
+
case E_STRICT:
$ex = new PhpStrict($errstr, $errno, $errfile, $errline);
break;
-
+
case E_PARSE:
$ex = new PhpParse($errstr, $errno, $errfile, $errline);
break;
-
+
default:
$ex = new PhpError($errstr, $errno, $errfile, $errline);
}
-
+
self::handle_exception($ex, false);
if (self::$previous_error_handler) {
call_user_func(self::$previous_error_handler, $errno, $errstr, $errfile, $errline);
@@ -160,19 +162,19 @@ static function handle_exception($exception, $call_previous = true)
call_user_func(self::$previous_exception_handler, $exception);
}
}
-
+
static function context($data = array()) {
self::$context = array_merge(self::$context, $data);
}
-
+
static function clear() {
self::$context = array();
}
}
-class Http404Error extends Exception {
-
+class Http404Error extends Exception {
+
public function __construct()
{
if (!isset($_SERVER["HTTP_HOST"])) {
@@ -181,5 +183,5 @@ public function __construct()
}
parent::__construct($_SERVER["REQUEST_URI"]." can't be found.");
}
-
+
}
View
22 exceptional/remote.php
@@ -19,22 +19,22 @@ static function send_exception($exception)
*/
static function call_remote($url, $post_data)
{
- if (Exceptional::$send_errors !== true) {
+ if (Exceptional::$api_key == null) {
return false;
}
-
+
if (Exceptional::$use_ssl === true) {
$s = fsockopen("ssl://".Exceptional::$host, 443, $errno, $errstr, 4);
}
else {
$s = fsockopen(Exceptional::$host, 80, $errno, $errstr, 2);
}
-
+
if (!$s) {
echo "[Error $errno] $errstr\n";
return false;
}
-
+
$request = "POST $url HTTP/1.1\r\n";
$request .= "Host: ".Exceptional::$host."\r\n";
$request .= "Accept: */*\r\n";
@@ -45,16 +45,16 @@ static function call_remote($url, $post_data)
$request .= "$post_data\r\n";
fwrite($s, $request);
-
- // don't wait for the response unless debugging
+
+ $response = "";
+ while (!feof($s)) {
+ $response .= fgets($s);
+ }
+
if (Exceptional::$debugging !== false) {
- $response = "";
- while (!feof($s)) {
- $response .= fgets($s);
- }
echo $response;
}
-
+
fclose($s);
}

0 comments on commit 29c9141

Please sign in to comment.
Something went wrong with that request. Please try again.