Skip to content

Commit

Permalink
Merge pull request #13 from codex-team/upgrade
Browse files Browse the repository at this point in the history
Enabling handlers by yourself
  • Loading branch information
talyguryn authored Sep 28, 2017
2 parents 9e74dc6 + 81b55d3 commit e5a9ca4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 36 deletions.
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,61 @@ You can download this repository and require `Hawk.php` file in your project.
require './hawk.php/src/Hawk.php';
```

### Add namespaces
### Init HawkCatcher

Add this line at the top of your PHP script. [Why?](http://php.net/manual/en/language.namespaces.importing.php)
Create an instance with token to the entry point of your project (usually `index.php` or `bootstrap.php`).

```php
use \Hawk\HawkCatcher;
\Hawk\HawkCatcher::instance('abcd1234-1234-abcd-1234-123456abcdef');
```

### Enable Catcher

Create an instance and pass project token.
You can store token in the environment file

```php
HawkCatcher::instance('abcd1234-1234-abcd-1234-123456abcdef');
\Hawk\HawkCatcher::instance($_SERVER['HAWK_TOKEN']);
```

#### Custom Hawk server

If you want to use custom Hawk server then pass a url to this catcher.

```php
HawkCatcher::instance(
\Hawk\HawkCatcher::instance(
'abcd1234-1234-abcd-1234-123456abcdef',
'http://myownhawk.com/catcher/php'
);
```

### Enable handlers

If you want to catch error automatically run the following command with boolean params to enable some handlers.

```php
\Hawk\HawkCatcher::enableHandlers(
TRUE, // exceptions
TRUE, // errors
TRUE // shutdown
);
```

By default Hawk will catch everything. You can run function with no params.

```php
\Hawk\HawkCatcher::enableHandlers();
```

### Catch exception

You can catch exceptions by yourself without enabling handlers.

```php
try {
throw new Exception("Error Processing Request", 1);
} catch (Exception $e) {
\Hawk\HawkCatcher::catchException($e);
}
```

## Links

Repository: https://github.com/codex-team/hawk.php
Expand Down
68 changes: 40 additions & 28 deletions src/Hawk.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@
*
* @copyright Codex Team
* @example https://hawk.so/docs#add-server-handler
*
* Use namespaces
* > use \Hawk\HawkCatcher;
*
* Initialize Hawk this way
* > HawkCatcher::instance('abcd1234-1234-abcd-1234-123456abcdef');
*
* Or this way if you want to use custom Hawk server
* > HawkCatcher::instance(
* > 'abcd1234-1234-abcd-1234-123456abcdef',
* > 'http://myownhawk.coms/catcher/php'
* > );
*
*/
class HawkCatcher
{
Expand All @@ -29,11 +16,6 @@ class HawkCatcher
*/
private function __construct ($accessToken) {
self::$_accessToken = $accessToken;

register_shutdown_function(array('\Hawk\HawkCatcher', 'checkForFatal'));
set_error_handler(array('\Hawk\HawkCatcher', 'Log'), E_ALL);
set_exception_handler(array('\Hawk\HawkCatcher', 'LogException'));
error_reporting(E_ALL | E_STRICT);
}

/**
Expand Down Expand Up @@ -74,27 +56,56 @@ public static function instance ($accessToken, $url = '') {
}

/**
* Fatal errors catch method
* Enable Hawk handlers functions for Exceptions, Error and Shutdown.
*
* @param $exceptions (bool) (TRUE) enable catching exceptions
* @param $errors (bool) (TRUE) enable catching errors
* @param $shutdown (bool) (TRUE) enable catching shutdown
*/
static public function checkForFatal () {
$error = error_get_last();
static public function enableHandlers($exceptions = TRUE, $errors = TRUE, $shutdown = TRUE) {

if ( $error['type'] == E_ERROR ) {
self::Log($error['type'], $error['message'], $error['file'], $error['line'], []);
if ($exceptions) {
set_exception_handler(array('\Hawk\HawkCatcher', 'catchException'));
}

if ($errors) {
set_error_handler(array('\Hawk\HawkCatcher', 'catchError'), E_ALL);
}

if ($shutdown) {
register_shutdown_function(array('\Hawk\HawkCatcher', 'catchFatal'));
}
}

/**
* Construct Exceptions and send them to Logs
*/
static public function LogException ($exception) {
self::Log(E_ERROR, $exception->getMessage(), $exception->getFile(), $exception->getLine(), []);
static public function catchException ($exception) {
return self::prepare($exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), []);
}

/**
* Works automatically. PHP would call this function on error by himself.
*/
static public function catchError ($errno, $errstr, $errfile, $errline, $errcontext) {
return self::prepare($errno, $errstr, $errfile, $errline, $errcontext);
}

/**
* Fatal errors catch method
*/
static public function catchFatal () {
$error = error_get_last();

if ( $error['type'] ) {
return self::prepare($error['type'], $error['message'], $error['file'], $error['line'], []);
}
}

/**
* Construct logs package and send them to service with access token
*/
public static function Log ($errno, $errstr, $errfile, $errline, $errcontext) {
private static function prepare ($errno, $errstr, $errfile, $errline, $errcontext) {
$data = array(
"error_type" => $errno,
"error_description" => $errstr,
Expand All @@ -108,7 +119,7 @@ public static function Log ($errno, $errstr, $errfile, $errline, $errcontext) {
"POST" => $_POST
);

self::send($data);
return self::send($data);
}

/**
Expand All @@ -123,6 +134,7 @@ private static function send ($package) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
}

return $server_output;
}
}

0 comments on commit e5a9ca4

Please sign in to comment.