From 08796cefb7279b7b30812db8bf6ad83053ab6d88 Mon Sep 17 00:00:00 2001 From: Taly Date: Thu, 21 Sep 2017 14:23:10 +0300 Subject: [PATCH 1/5] Enabling handlers by yourself --- README.md | 42 ++++++++++++++++++++++++++++++++++++------ src/Hawk.php | 46 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 75 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 372dc9c..ad7c32d 100644 --- a/README.md +++ b/README.md @@ -23,20 +23,26 @@ You can download this repository and require `Hawk.php` file in your project. require './hawk.php/src/Hawk.php'; ``` -### Add namespaces + + +### Init HawkCatcher -### Enable Catcher +Create an instance and pass token at the start of your project. + +```php +\Hawk\HawkCatcher::instance('abcd1234-1234-abcd-1234-123456abcdef'); +``` -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 @@ -44,12 +50,36 @@ HawkCatcher::instance('abcd1234-1234-abcd-1234-123456abcdef'); 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 then run the following command with boolean params to enable some handlers. + +```php +\Hawk\HawkCatcher::enableHandlers( + $exceptions = TRUE, + $errors = TRUE, + $shutdown = TRUE +); +``` + +### Log exceptions + +You can catch exceptions by yourself without enabling handlers. + +```php +try { + throw new Exception("Error Processing Request", 1); +} catch (Exception $e) { + \Hawk\HawkCatcher::LogException($e); +} +``` + ## Links Repository: https://github.com/codex-team/hawk.php diff --git a/src/Hawk.php b/src/Hawk.php index 2ca186b..16a9986 100644 --- a/src/Hawk.php +++ b/src/Hawk.php @@ -9,6 +9,8 @@ * @copyright Codex Team * @example https://hawk.so/docs#add-server-handler * + * Require this file by your self or via composer + * * Use namespaces * > use \Hawk\HawkCatcher; * @@ -21,6 +23,12 @@ * > 'http://myownhawk.coms/catcher/php' * > ); * + * Enable catchers + * > HawkCatcher::enableHandlers( + * > TRUE, // catch exceptions + * > TRUE, // catch errors + * > TRUE, // catch shutdown + * > ); */ class HawkCatcher { @@ -29,11 +37,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); } /** @@ -73,6 +76,28 @@ public static function instance ($accessToken, $url = '') { return self::$_instance; } + /** + * 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 enableHandlers($exceptions = TRUE, $errors = TRUE, $shutdown = TRUE) { + + if ($exceptions) { + set_exception_handler(array('\Hawk\HawkCatcher', 'LogException')); + } + + if ($errors) { + set_error_handler(array('\Hawk\HawkCatcher', 'LogError'), E_ALL); + } + + if ($shutdown) { + register_shutdown_function(array('\Hawk\HawkCatcher', 'checkForFatal')); + } + } + /** * Fatal errors catch method */ @@ -88,13 +113,20 @@ static public function checkForFatal () { * Construct Exceptions and send them to Logs */ static public function LogException ($exception) { - self::Log(E_ERROR, $exception->getMessage(), $exception->getFile(), $exception->getLine(), []); + self::prepare($exception->getCode() ?: E_ERROR, $exception->getMessage(), $exception->getFile(), $exception->getLine(), []); + } + + /** + * Works automacally. PHP would call this function on error by himself. + */ + static public function LogError ($errno, $errstr, $errfile, $errline, $errcontext) { + self::prepare($errno, $errstr, $errfile, $errline, $errcontext); } /** * 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, From 08585101fd18fdf715b38a570952ebd1bba704b2 Mon Sep 17 00:00:00 2001 From: Taly Date: Fri, 22 Sep 2017 14:40:54 +0300 Subject: [PATCH 2/5] Rename functions --- README.md | 16 +++++++++++----- src/Hawk.php | 32 ++++++++++++++++---------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ad7c32d..f43e00e 100644 --- a/README.md +++ b/README.md @@ -62,13 +62,19 @@ If you want to catch error automatically then run the following command with boo ```php \Hawk\HawkCatcher::enableHandlers( - $exceptions = TRUE, - $errors = TRUE, - $shutdown = TRUE + TRUE, // exceptions + TRUE, // errors + TRUE // shutdown ); ``` -### Log exceptions +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. @@ -76,7 +82,7 @@ You can catch exceptions by yourself without enabling handlers. try { throw new Exception("Error Processing Request", 1); } catch (Exception $e) { - \Hawk\HawkCatcher::LogException($e); + \Hawk\HawkCatcher::catchException($e); } ``` diff --git a/src/Hawk.php b/src/Hawk.php index 16a9986..e7ea7f2 100644 --- a/src/Hawk.php +++ b/src/Hawk.php @@ -86,41 +86,41 @@ public static function instance ($accessToken, $url = '') { static public function enableHandlers($exceptions = TRUE, $errors = TRUE, $shutdown = TRUE) { if ($exceptions) { - set_exception_handler(array('\Hawk\HawkCatcher', 'LogException')); + set_exception_handler(array('\Hawk\HawkCatcher', 'catchException')); } if ($errors) { - set_error_handler(array('\Hawk\HawkCatcher', 'LogError'), E_ALL); + set_error_handler(array('\Hawk\HawkCatcher', 'catchError'), E_ALL); } if ($shutdown) { - register_shutdown_function(array('\Hawk\HawkCatcher', 'checkForFatal')); + register_shutdown_function(array('\Hawk\HawkCatcher', 'catchFatal')); } } /** - * Fatal errors catch method + * Construct Exceptions and send them to Logs */ - static public function checkForFatal () { - $error = error_get_last(); - - if ( $error['type'] == E_ERROR ) { - self::Log($error['type'], $error['message'], $error['file'], $error['line'], []); - } + static public function catchException ($exception) { + self::prepare($exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), []); } /** - * Construct Exceptions and send them to Logs + * Works automatically. PHP would call this function on error by himself. */ - static public function LogException ($exception) { - self::prepare($exception->getCode() ?: E_ERROR, $exception->getMessage(), $exception->getFile(), $exception->getLine(), []); + static public function catchError ($errno, $errstr, $errfile, $errline, $errcontext) { + self::prepare($errno, $errstr, $errfile, $errline, $errcontext); } /** - * Works automacally. PHP would call this function on error by himself. + * Fatal errors catch method */ - static public function LogError ($errno, $errstr, $errfile, $errline, $errcontext) { - self::prepare($errno, $errstr, $errfile, $errline, $errcontext); + static public function catchFatal () { + $error = error_get_last(); + + if ( $error['type'] ) { + self::prepare($error['type'], $error['message'], $error['file'], $error['line'], []); + } } /** From 5a08bea02e5b0e74ffe86958c9b3048f1a5f09a8 Mon Sep 17 00:00:00 2001 From: Taly Date: Thu, 28 Sep 2017 23:46:11 +0300 Subject: [PATCH 3/5] Update comments --- README.md | 8 -------- src/Hawk.php | 32 ++++++-------------------------- 2 files changed, 6 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index f43e00e..e40820c 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,6 @@ You can download this repository and require `Hawk.php` file in your project. require './hawk.php/src/Hawk.php'; ``` - - ### Init HawkCatcher Create an instance and pass token at the start of your project. diff --git a/src/Hawk.php b/src/Hawk.php index e7ea7f2..09bf80d 100644 --- a/src/Hawk.php +++ b/src/Hawk.php @@ -8,27 +8,6 @@ * * @copyright Codex Team * @example https://hawk.so/docs#add-server-handler - * - * Require this file by your self or via composer - * - * 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' - * > ); - * - * Enable catchers - * > HawkCatcher::enableHandlers( - * > TRUE, // catch exceptions - * > TRUE, // catch errors - * > TRUE, // catch shutdown - * > ); */ class HawkCatcher { @@ -102,14 +81,14 @@ static public function enableHandlers($exceptions = TRUE, $errors = TRUE, $shutd * Construct Exceptions and send them to Logs */ static public function catchException ($exception) { - self::prepare($exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), []); + 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) { - self::prepare($errno, $errstr, $errfile, $errline, $errcontext); + return self::prepare($errno, $errstr, $errfile, $errline, $errcontext); } /** @@ -119,7 +98,7 @@ static public function catchFatal () { $error = error_get_last(); if ( $error['type'] ) { - self::prepare($error['type'], $error['message'], $error['file'], $error['line'], []); + return self::prepare($error['type'], $error['message'], $error['file'], $error['line'], []); } } @@ -140,7 +119,7 @@ private static function prepare ($errno, $errstr, $errfile, $errline, $errcontex "POST" => $_POST ); - self::send($data); + return self::send($data); } /** @@ -155,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; + } } From 91d7111498c659b6b93a97682589fce90c178bb2 Mon Sep 17 00:00:00 2001 From: Taly Date: Thu, 28 Sep 2017 23:59:20 +0300 Subject: [PATCH 4/5] Update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e40820c..f066060 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ require './hawk.php/src/Hawk.php'; ### Init HawkCatcher -Create an instance and pass token at the start of your project. +Create an instance and pass token in the entry point of your project (usually `index.php` or `bootstrap.php`). ```php \Hawk\HawkCatcher::instance('abcd1234-1234-abcd-1234-123456abcdef'); @@ -50,7 +50,7 @@ If you want to use custom Hawk server then pass a url to this catcher. ### Enable handlers -If you want to catch error automatically then run the following command with boolean params to enable some handlers. +If you want to catch error automatically run the following command with boolean params to enable some handlers. ```php \Hawk\HawkCatcher::enableHandlers( From 81b55d3b2bb694f759c489e9361f4486f395b438 Mon Sep 17 00:00:00 2001 From: Taly Date: Fri, 29 Sep 2017 00:02:15 +0300 Subject: [PATCH 5/5] I SNOVA UPDATE README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f066060..c607f08 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ require './hawk.php/src/Hawk.php'; ### Init HawkCatcher -Create an instance and pass token in the entry point of your project (usually `index.php` or `bootstrap.php`). +Create an instance with token to the entry point of your project (usually `index.php` or `bootstrap.php`). ```php \Hawk\HawkCatcher::instance('abcd1234-1234-abcd-1234-123456abcdef');