Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpk committed Jul 17, 2011
1 parent 33bbe21 commit 0dd5903
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
# Introduction # Introduction


This is a simple [Hoptoad](http://hoptoadapp.com) notifier for PHP. It's been used in a few production sites now with success. It's not quite as fully featured as the official Ruby notifier but it works well. This is a simple [Airbrake](http://airbrakeapp.com), (formerly Hoptoad) notifier for PHP. It's been used in a few production sites now with success. It's not quite as fully featured as the official Ruby notifier but it works well.


# Thanks # Thanks


Expand Down
59 changes: 30 additions & 29 deletions Services/Hoptoad.php → Services/Airbrake.php
Original file line number Original file line Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php <?php
/** /**
* Services_Hoptoad * Services_Airbrake
* *
* @category error * @category error
* @package Services_Hoptoad * @package Services_Airbrake
* @author Rich Cavanaugh <no@email> * @author Rich Cavanaugh <no@email>
* @author Till Klampaeckel <till@php.net> * @author Till Klampaeckel <till@php.net>
* @author Aaron Parecki <aaron@parecki.com> * @author Aaron Parecki <aaron@parecki.com>
* @license * @license
* @version GIT: $Id$ * @version GIT: $Id$
* @link http://github.com/geoloqi/php-hoptoad-notifier * @link http://github.com/geoloqi/php-airbrake-notifier
*/ */
class Services_Hoptoad class Services_Airbrake
{ {
const NOTIFIER_NAME = 'php-hoptoad-notifier'; const NOTIFIER_NAME = 'php-airbrake-notifier';
const NOTIFIER_VERSION = '0.2.1'; const NOTIFIER_VERSION = '0.2.2';
const NOTIFIER_URL = 'http://github.com/geoloqi/php-hoptoad-notifier'; const NOTIFIER_URL = 'http://github.com/geoloqi/php-airbrake-notifier';
const NOTIFIER_API_VERSION = '2.0'; const NOTIFIER_API_VERSION = '2.0';


protected $error_class; protected $error_class;
Expand All @@ -38,7 +38,7 @@ class Services_Hoptoad
*/ */
protected $timeout; protected $timeout;


public $client; // pear, curl or zend public $client; // pear, curl, zend or beanstalk


/** /**
* @var mixed $apiKey * @var mixed $apiKey
Expand All @@ -52,15 +52,15 @@ class Services_Hoptoad


/** /**
* Initialize the chosen notifier and install the error * Initialize the chosen notifier and install the error
* and exception handlers that connect to Hoptoad * and exception handlers that connect to Airbrake
* *
* @return void * @return void
* @author Rich Cavanaugh * @author Rich Cavanaugh
*/ */
public static function installHandlers($apiKey=NULL, $environment=NULL, $client=NULL, $class='Services_Hoptoad') public static function installHandlers($apiKey=NULL, $environment=NULL, $client=NULL, $class='Services_Airbrake')
{ {
$hoptoad = new $class($apiKey, $environment, $client); $airbrake = new $class($apiKey, $environment, $client);
$hoptoad->installNotifierHandlers(); $airbrake->installNotifierHandlers();
} }


/** /**
Expand Down Expand Up @@ -145,11 +145,12 @@ public function exceptionHandler($exception)
public function fatalErrorHandler() public function fatalErrorHandler()
{ {
$error = error_get_last(); $error = error_get_last();
$this->notify($error['type'], $error['message'], $error['file'], $error['line'], debug_backtrace()); if($error)
$this->notify($error['type'], $error['message'], $error['file'], $error['line'], debug_backtrace());
} }


/** /**
* Set the values to be used for the next notice sent to Hoptoad * Set the values to be used for the next notice sent to Airbrake
* @return void * @return void
* @author Rich Cavanaugh * @author Rich Cavanaugh
**/ **/
Expand All @@ -164,7 +165,7 @@ public function setParamsForNotify($error_class, $message, $file, $line, $trace,
} }


/** /**
* Pass the error and environment data on to Hoptoad * Pass the error and environment data on to Airbrake
* *
* @param mixed $error_class * @param mixed $error_class
* @param string $message * @param string $message
Expand All @@ -180,7 +181,7 @@ function notify($error_class, $message, $file, $line, $trace, $component=NULL)
{ {
$this->setParamsForNotify($error_class, $message, $file, $line, $trace, $component); $this->setParamsForNotify($error_class, $message, $file, $line, $trace, $component);


$url = "http://hoptoadapp.com/notifier_api/v2/notices"; $url = "http://airbrakeapp.com/notifier_api/v2/notices";
$headers = array( $headers = array(
'Accept' => 'text/xml, application/xml', 'Accept' => 'text/xml, application/xml',
'Content-Type' => 'text/xml' 'Content-Type' => 'text/xml'
Expand All @@ -199,7 +200,7 @@ function notify($error_class, $message, $file, $line, $trace, $component=NULL)


/** /**
* Build up the XML to post according to the documentation at: * Build up the XML to post according to the documentation at:
* http://help.hoptoadapp.com/faqs/api-2/notifier-api-v2 * http://help.airbrakeapp.com/faqs/api-2/notifier-api-v2
* @return string * @return string
* @author Rich Cavanaugh * @author Rich Cavanaugh
**/ **/
Expand Down Expand Up @@ -240,7 +241,7 @@ function buildXmlNotice()
} }


/** /**
* Add a Hoptoad var block to the XML * Add a Airbrake var block to the XML
* @return void * @return void
* @author Rich Cavanaugh * @author Rich Cavanaugh
**/ **/
Expand All @@ -256,7 +257,7 @@ function addXmlVars($parent, $key, $source)
} }


/** /**
* Add a Hoptoad backtrace to the XML * Add a Airbrake backtrace to the XML
* @return void * @return void
* @author Rich Cavanaugh * @author Rich Cavanaugh
**/ **/
Expand All @@ -268,7 +269,7 @@ function addXmlBacktrace($parent)
$line_node->addAttribute('number', $this->line); $line_node->addAttribute('number', $this->line);


foreach ($this->trace as $entry) { foreach ($this->trace as $entry) {
if (isset($entry['class']) && $entry['class'] == 'Services_Hoptoad') continue; if (isset($entry['class']) && $entry['class'] == 'Services_Airbrake') continue;


$line_node = $backtrace->addChild('line'); $line_node = $backtrace->addChild('line');
$line_node->addAttribute('file', $entry['file']); $line_node->addAttribute('file', $entry['file']);
Expand Down Expand Up @@ -380,10 +381,10 @@ function request_uri() {
} }


/** /**
* @param mixed $code The HTTP status code from Hoptoad. * @param mixed $code The HTTP status code from Airbrake.
* *
* @return void * @return void
* @throws RuntimeException Error message from hoptoad, translated to a RuntimeException. * @throws RuntimeException Error message from airbrake, translated to a RuntimeException.
*/ */
protected function handleErrorResponse($code) protected function handleErrorResponse($code)
{ {
Expand All @@ -395,18 +396,18 @@ protected function handleErrorResponse($code)
$msg = 'The submitted notice was invalid - check the notice xml against the schema.'; $msg = 'The submitted notice was invalid - check the notice xml against the schema.';
break; break;
case '500': case '500':
$msg = 'Unexpected errors - submit a bug report at http://help.hoptoadapp.com.'; $msg = 'Unexpected errors - submit a bug report at http://help.airbrakeapp.com.';
break; break;
default: default:
$msg = 'Unknown error code from Hoptoad\'s API: ' . $code; $msg = 'Unknown error code from Airbrake\'s API: ' . $code;
break; break;
} }


throw new RuntimeException($msg, $code); throw new RuntimeException($msg, $code);
} }


/** /**
* Send the request to Hoptoad using PEAR * Send the request to Airbrake using PEAR
* @return integer * @return integer
* @author Rich Cavanaugh * @author Rich Cavanaugh
**/ **/
Expand All @@ -424,7 +425,7 @@ public function pearRequest($url, $headers, $body)
} }


/** /**
* Send the request to Hoptoad using Curl * Send the request to Airbrake using Curl
* @return integer * @return integer
* @author Rich Cavanaugh * @author Rich Cavanaugh
**/ **/
Expand All @@ -451,7 +452,7 @@ public function curlRequest($url, $headers, $body)


/** /**
* Put the error on a beanstalk queue so a separate process can * Put the error on a beanstalk queue so a separate process can
* send it off to Hoptoad via HTTP. Requires the pheanstalk client. * send it off to Airbrake via HTTP. Requires the pheanstalk client.
* Config: * Config:
* $BEANSTALK_SERVERS[] = array( * $BEANSTALK_SERVERS[] = array(
* 'host' => 'example.com', * 'host' => 'example.com',
Expand All @@ -467,12 +468,12 @@ public function beanstalkRequest($url, $headers, $body)
$k = array_rand($BEANSTALK_SERVERS); $k = array_rand($BEANSTALK_SERVERS);
$pheanstalk = new Pheanstalk($BEANSTALK_SERVERS[$k]['host'], $BEANSTALK_SERVERS[$k]['port']); $pheanstalk = new Pheanstalk($BEANSTALK_SERVERS[$k]['host'], $BEANSTALK_SERVERS[$k]['port']);


$pheanstalk->useTube('hoptoad'); $pheanstalk->useTube('airbrake');
$pheanstalk->put(json_encode(array('url'=>$url, 'headers'=>$headers, 'body'=>$body))); $pheanstalk->put(json_encode(array('url'=>$url, 'headers'=>$headers, 'body'=>$body)));
} }


/** /**
* Send the request to Hoptoad using Zend * Send the request to Airbrake using Zend
* @return integer * @return integer
* @author Rich Cavanaugh * @author Rich Cavanaugh
**/ **/
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php <?php
class Services_Hoptoad_CodeIgniter extends Services_Hoptoad { class Services_Airbrake_CodeIgniter extends Services_Airbrake {
var $ci; var $ci;


function setup() { function setup() {
Expand Down
8 changes: 4 additions & 4 deletions beanstalk-daemon.php
Original file line number Original file line Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php <?php
require_once('php-hoptoad-notifier/Services/Hoptoad.php'); require_once('php-airbrake-notifier/Services/Airbrake.php');
require_once('pheanstalk/pheanstalk_init.php'); require_once('pheanstalk/pheanstalk_init.php');


$hoptoad = new Services_Hoptoad(HOPTOAD_API_KEY); $airbrake = new Services_Airbrake(HOPTOAD_API_KEY);


$k = array_rand($BEANSTALK_SERVERS); $k = array_rand($BEANSTALK_SERVERS);
$pheanstalk = new Pheanstalk($BEANSTALK_SERVERS[$k]['host'], $BEANSTALK_SERVERS[$k]['port']); $pheanstalk = new Pheanstalk($BEANSTALK_SERVERS[$k]['host'], $BEANSTALK_SERVERS[$k]['port']);


$pheanstalk->watch('hoptoad')->ignore('default'); $pheanstalk->watch('airbrake')->ignore('default');
while($job = $pheanstalk->reserve()) while($job = $pheanstalk->reserve())
{ {
$data = json_decode($job->getData()); $data = json_decode($job->getData());


echo date('Y-m-d H:i:s') . ' Processing Error: ' . $data->url . "\n"; echo date('Y-m-d H:i:s') . ' Processing Error: ' . $data->url . "\n";
$hoptoad->curlRequest($data->url, $data->headers, $data->body); $airbrake->curlRequest($data->url, $data->headers, $data->body);


$pheanstalk->delete($job); $pheanstalk->delete($job);
} }
32 changes: 16 additions & 16 deletions doc/example.php
Original file line number Original file line Diff line number Diff line change
@@ -1,41 +1,41 @@
<?php <?php
// register Services_Hoptoad for php errors and raised exceptions // register Services_Airbrake for php errors and raised exceptions
require_once 'Services/Hoptoad.php'; require_once 'Services/Airbrake.php';
Services_Hoptoad::installHandlers("YOUR_HOPTOAD_API_KEY"); Services_Airbrake::installHandlers("YOUR_AIRBRAKE_API_KEY");
?> ?>


<?php <?php
// register Services_Hoptoad for php errors and raised exceptions // register Services_Airbrake for php errors and raised exceptions
// when used in your staging environment // when used in your staging environment
require_once 'Services/Hoptoad.php'; require_once 'Services/Airbrake.php';
Services_Hoptoad::installHandlers("YOUR_HOPTOAD_API_KEY", 'staging'); Services_Airbrake::installHandlers("YOUR_AIRBRAKE_API_KEY", 'staging');
?> ?>


<?php <?php
// register Services_Hoptoad for php errors and raised exceptions // register Services_Airbrake for php errors and raised exceptions
// when used in production and using the Curl transport // when used in production and using the Curl transport
require_once 'Services/Hoptoad.php'; require_once 'Services/Airbrake.php';
Services_Hoptoad::installHandlers("YOUR_HOPTOAD_API_KEY", 'production', 'curl'); Services_Airbrake::installHandlers("YOUR_AIRBRAKE_API_KEY", 'production', 'curl');
?> ?>


<?php <?php
// standalone // standalone
require_once 'Services/Hoptoad.php'; require_once 'Services/Airbrake.php';


Services_Hoptoad::$apiKey = "YOUR_HOPTOAD_API_KEY"; Services_Airbrake::$apiKey = "YOUR_AIRBRAKE_API_KEY";


$exception = new Custom_Exception('foobar'); $exception = new Custom_Exception('foobar');
Services_Hoptoad::handleException($exception); Services_Airbrake::handleException($exception);
?> ?>


<?php <?php
// use Zend_Http_Client // use Zend_Http_Client
require_once 'Services/Hoptoad.php'; require_once 'Services/Airbrake.php';


Services_Hoptoad::$apiKey = "YOUR_HOPTOAD_API_KEY"; Services_Airbrake::$apiKey = "YOUR_AIRBRAKE_API_KEY";
Services_Hoptoad::$client = "zend"; Services_Airbrake::$client = "zend";


$exception = new Custom_Exception('foobar'); $exception = new Custom_Exception('foobar');
Services_Hoptoad::handleException($exception); Services_Airbrake::handleException($exception);
?> ?>


File renamed without changes.
24 changes: 12 additions & 12 deletions test/test.php
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@


<?php <?php
require_once 'PHPUnit/Framework.php'; require_once 'PHPUnit/Framework.php';
require_once 'Services/Hoptoad.php'; require_once 'Services/Airbrake.php';


$_SERVER = array( $_SERVER = array(
'HTTP_HOST' => 'localhost', 'HTTP_HOST' => 'localhost',
Expand All @@ -26,15 +26,15 @@


$_REQUEST = array_merge($_GET, $_POST); $_REQUEST = array_merge($_GET, $_POST);


class HoptoadTest extends PHPUnit_Framework_TestCase class AirbrakeTest extends PHPUnit_Framework_TestCase
{ {
protected function setUp() protected function setUp()
{ {
$this->hoptoad = new Services_Hoptoad('myAPIKey', 'production', 'pear', false, 2); $this->airbrake = new Services_Airbrake('myAPIKey', 'production', 'pear', false, 2);


$trace = array( $trace = array(
array( array(
'class' => 'Hoptoad', 'class' => 'Airbrake',
'file' => 'file.php', 'file' => 'file.php',
'line' => 23, 'line' => 23,
'function' => 'foo', 'function' => 'foo',
Expand All @@ -52,20 +52,20 @@ protected function setUp()
'function' => 'bar', 'function' => 'bar',
), ),
); );
$this->hoptoad->setParamsForNotify('ERROR', 'Something went wrong', 'foo', 23, $trace); $this->airbrake->setParamsForNotify('ERROR', 'Something went wrong', 'foo', 23, $trace);
} }


public function testRequestURI() public function testRequestURI()
{ {
// check protocol support // check protocol support
$this->assertEquals('http://localhost/example.php', $this->hoptoad->request_uri()); $this->assertEquals('http://localhost/example.php', $this->airbrake->request_uri());
$_SERVER['SERVER_PORT'] = 443; $_SERVER['SERVER_PORT'] = 443;
$this->assertEquals('https://localhost/example.php', $this->hoptoad->request_uri()); $this->assertEquals('https://localhost/example.php', $this->airbrake->request_uri());
$_SERVER['SERVER_PORT'] = 80; $_SERVER['SERVER_PORT'] = 80;


// Check query string support. // Check query string support.
$_SERVER['QUERY_STRING'] = 'commit=true'; $_SERVER['QUERY_STRING'] = 'commit=true';
$this->assertEquals('http://localhost/example.php?commit=true', $this->hoptoad->request_uri()); $this->assertEquals('http://localhost/example.php?commit=true', $this->airbrake->request_uri());
$_SERVER['QUERY_STRING'] = ''; $_SERVER['QUERY_STRING'] = '';
} }


Expand All @@ -82,7 +82,7 @@ public function testXMLBacktrace()
</root> </root>
XML; XML;
$doc = new SimpleXMLElement('<root />'); $doc = new SimpleXMLElement('<root />');
$this->hoptoad->addXmlBacktrace($doc); $this->airbrake->addXmlBacktrace($doc);
$this->assertXmlStringEqualsXmlString($expected_xml, $doc->asXML()); $this->assertXmlStringEqualsXmlString($expected_xml, $doc->asXML());
} }


Expand All @@ -99,15 +99,15 @@ public function testXMLParams()
</root> </root>
XML; XML;
$doc = new SimpleXMLElement('<root />'); $doc = new SimpleXMLElement('<root />');
$this->hoptoad->addXmlVars($doc, 'params', $_REQUEST); $this->airbrake->addXmlVars($doc, 'params', $_REQUEST);
$this->assertXmlStringEqualsXmlString($expected_xml, $doc->asXML()); $this->assertXmlStringEqualsXmlString($expected_xml, $doc->asXML());
} }


public function testNotificationBody() public function testNotificationBody()
{ {
$xmllint = popen('xmllint --noout --schema test/hoptoad_2_0.xsd - 2> /dev/null', 'w'); $xmllint = popen('xmllint --noout --schema test/airbrake_2_0.xsd - 2> /dev/null', 'w');
if ($xmllint) { if ($xmllint) {
fwrite($xmllint, $this->hoptoad->buildXmlNotice()); fwrite($xmllint, $this->airbrake->buildXmlNotice());
$status = pclose($xmllint); $status = pclose($xmllint);
$this->assertEquals(0, $status, "XML output did not validate against schema."); $this->assertEquals(0, $status, "XML output did not validate against schema.");
} else { } else {
Expand Down

0 comments on commit 0dd5903

Please sign in to comment.