diff --git a/.gitattributes b/.gitattributes index 412eeda..38ea924 100644 --- a/.gitattributes +++ b/.gitattributes @@ -20,3 +20,6 @@ *.PDF diff=astextplain *.rtf diff=astextplain *.RTF diff=astextplain + +# Exclude from release +sample-apps export-ignore diff --git a/README.md b/README.md index 8d242a3..3e89e51 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ Raygun4PHP [Raygun.com](http://raygun.com) provider for PHP 7.1+ +_See [v1.8 documentation](https://github.com/MindscapeHQ/raygun4php/blob/1.8/README.md) for PHP v5.3+ support_ + [![Build Status](https://secure.travis-ci.org/MindscapeHQ/raygun4php.png?branch=master)](http://travis-ci.org/MindscapeHQ/raygun4php) @@ -14,19 +16,21 @@ Firstly, ensure that **curl** is installed and enabled in your server's php.ini Composer is a package management tool for PHP which automatically fetches dependencies and also supports autoloading - this is a low-impact way to get Raygun4PHP into your site. -1. If you use a *nix environment, [follow the instructions](http://getcomposer.org/doc/01-basic-usage.md#installation) to install Composer. Windows users can run [this installer](https://github.com/johnstevenson/composer-setup) to automatically add it to the Path. +1\. If you use a *nix environment, [follow the instructions](http://getcomposer.org/doc/01-basic-usage.md#installation) to install Composer. Windows users can run [this installer](https://github.com/johnstevenson/composer-setup) to automatically add it to the Path. + +2\. Inside your project's root directory create a composer.json file, containing: -2. Inside your project's root directory create a composer.json file, containing: ```json { - "require": { - "mindscape/raygun4php": "^1.0" - } + "require": { + "mindscape/raygun4php": "^2.0" + } } ``` -3. From your shell run `php composer.phar install` (*nix) or `composer install` (Windows). This will download Raygun4Php and create the appropriate autoload data. -4. Then in a PHP file just add: +3\. From your shell run `php composer.phar install` (*nix) or `composer install` (Windows). This will download Raygun4PHP and create the appropriate autoload data. + +4\. Then in a PHP file just add: ```php require_once 'vendor/autoload.php'; ``` @@ -34,181 +38,219 @@ and the library will be imported ready for use. ## Usage -You can send both PHP errors and object-oriented exceptions to Raygun. An easy way to accomplish this is to create a file containing exception and error handlers which make calls to the appropriate Raygun4PHP functions. As above, import Raygun4PHP - if you're using Composer, just add `require_once 'vendor/autoload.php'`, or if not manually import RaygunClient.php. +You can automatically send both PHP errors and object-oriented exceptions to Raygun. The RaygunClient requires an HTTP transport (e.g. [Guzzle](http://docs.guzzlephp.org/) or other [PSR-7](https://www.php-fig.org/psr/psr-7/) compatible interface). -Then, create handlers that look something like this: +There are Guzzle-based asynchronous and synchronous transport classes in the provider, or you can use your own. -```php -namespace -{ - // paste your 'requires' statement +### Asynchronous transport - $client = new \Raygun4php\RaygunClient("apikey for your application"); +This is an example of basic usage for asynchronous sending of errors and exceptions: - function error_handler($errno, $errstr, $errfile, $errline ) { - global $client; - $client->SendError($errno, $errstr, $errfile, $errline); - } +```php +SendException($exception); - } + use GuzzleHttp\Client; + use Raygun4php\RaygunClient; + use Raygun4php\Transports\GuzzleAsync; - function fatal_error() - { - global $client; - $last_error = error_get_last(); - - if (!is_null($last_error)) { - $errno = $last_error['type']; - $errstr = $last_error['message']; - $errfile = $last_error['file']; - $errline = $last_error['line']; - $client->SendError($errno, $errstr, $errfile, $errline); - } - } + $httpClient = new Client([ + 'base_uri' => 'https://api.raygun.com', + 'timeout' => 2.0, + 'headers' => [ + 'X-ApiKey' => 'INSERT_API_KEY_HERE' + ] + ]); - set_exception_handler('exception_handler'); - set_error_handler("error_handler"); - register_shutdown_function("fatal_error"); -} -``` + $transport = new GuzzleAsync($httpClient); -Note that if you are placing in inside a file with a namespace of your choosing, the above code should be declared to be within the global namespace (thus the `namespace { }` is required). You will also need whichever `requires` statement as above (autoload or manual) before the `$client` instantiation. + $raygunClient = new RaygunClient($transport); -Copy your application's API key from the Raygun.io dashboard, and place it in the constructor call as above (do not include the curly brackets). + set_error_handler(function($errno, $errstr, $errfile, $errline) use ($raygunClient) { + $raygunClient->SendError($errno, $errstr, $errfile, $errline); + }); -If the handlers reside in their own file, just import it in every file where you'd like exceptions and errors to be sent, and they will be delivered to Raygun.io. + set_exception_handler(function($exception) use ($raygunClient) { + $raygunClient->SendException($exception); + }); -## Configuration + register_shutdown_function(function() use ($raygunClient) { + $lastError = error_get_last(); -### Sending method - async/sync + if (!is_null($lastError)) { + [$type, $message, $file, $line] = $lastError; + $raygunClient->SendError($type, $message, $file, $line); + } + }); -Raygun4PHP has two algorithms which it can use to send your errors: + register_shutdown_function([$transport, 'wait']); +} +``` -* **Asynchronous**: POSTs the message and returns to your script immediately without waiting for the response from the Raygun API. +### Synchronous transport -* **Synchronous**: POSTs the message, blocks and receives the HTTP response from the Raygun API. This uses a socket connection which is still reasonably fast. This also allows the use of the debug mode to receive the HTTP response code; see below. +For synchronous transport, use the snippet above but replace `use Raygun4php\Transports\GuzzleAsync;` with: +```php +use Raygun4php\Transports\GuzzleSync; +``` -This can be set by passing in a boolean as the 2nd parameter to the constructor: +And replace `$transport = new GuzzleAsync($httpClient);` with: ```php -$client = new \Raygun4php\RaygunClient("apiKey", $useAsyncSending); +$transport = new GuzzleSync($httpClient); ``` -#### $useAsyncSending options -Type: *boolean* +Remove this line: -Linux/OS X default: *true* +```php +register_shutdown_function([$transport, 'wait']); +``` -Windows default: *false* +Note that if you are placing it inside a file with a namespace of your choosing, the above code should be declared to be within the global namespace (thus the `namespace { }` is required). You will also need whichever `require` statement as above (autoload or manual) before the `$raygunClient` instantiation. -* If **$useAsyncSending** is *true*, and the script is running on a *nix platform, the message will be delivered asynchronously. SendError() and SendException() will return 0 if all went well. +Copy your application's API key from the [Raygun app](https://app.raygun.com), and paste it into the `X-ApiKey` header field of the HTTP client. -* If **$useAsyncSending** is *false*, the script will block and receive the HTTP response. +If the handlers reside in their own file, just import it in every file where you'd like exceptions and errors to be sent, and they will be delivered to Raygun. -*false* is the only effective option on Windows due to platform and library limitations within the supported versions. +## Configuration ### Proxies -A HTTP proxy can be set if your environment can't connect out through PHP or the `curl` binrary natively: +A URL can be set as the `proxy` property on the HTTP Client: ```php -$client = new \Raygun4php\RaygunClient("apiKey"); -$client->setProxy('http://someproxy:8080'); +// ... + +$httpClient = new Client([ + 'base_uri' => 'https://api.raygun.com', + 'proxy' => 'http://someproxy:8080', + 'headers' => [ + 'X-ApiKey' => 'INSERT_API_KEY_HERE' + ] +]); ``` -### Debug mode +See Guzzle's [proxy documentation](http://docs.guzzlephp.org/en/stable/request-options.html#proxy) for more options. + +### Debugging with a logger -The client offers a debug mode in which the HTTP response code can be returned after a POST attempt. This can be useful when adding Raygun to your site. This is accessed by passing in *true* as the third parameter in the client constructor: +We recommend using a logger which is compatible with the [PSR-3 LoggerInterface](https://www.php-fig.org/psr/psr-3/) (e.g. [Monolog](https://github.com/Seldaek/monolog)). + +Expanding on the _Asynchronous transport_ example above, you can set a logger to be used by the transport like so: ```php -$client = new \Raygun4php\RaygunClient("apiKey", $useAsyncSending, $debugMode); -``` +// ... + +use Monolog\Handler\FirePHPHandler; +use Monolog\Handler\StreamHandler; +use Monolog\Logger; -#### $debugMode options +// ... -*Default: false* +$logger = new Logger('my_logger'); +$logger->pushHandler(new StreamHandler(__DIR__ . '/debug.log')); +$logger->pushHandler(new FirePHPHandler()); -If true is passed in, and **$useAsyncSending** is set to *false*, client->SendException() or SendError() will return the HTTP status code of the POST attempt. +// Create $httpClient ... -**Note:** If $useAsyncSending is *true*, $debugMode is not available. +$transport = new GuzzleAsync($httpClient); +$transport->setLogger($logger); + +$raygunClient = new RaygunClient($transport, false, $logger); + +// ... +``` #### Response codes * **202**: Message received by Raygun API correctly -* **403**: Invalid API key. Copy it from your Raygun Application Settings, it should be of the form `new RaygunClient("A+nUc2dLh27vbh8abls7==")` +* **403**: Invalid API key. Copy the API key from the [Raygun app](https://app.raygun.com) setup instructions or application settings ### Version numbers You can transmit the version number of your PHP project along with the message by calling `SetVersion()` on your RaygunClient after it is instantiated - this is optional but recommended as the version number is considered to be first-class data for a message. +```php +$raygunClient = new RaygunClient($transport); +$raygunClient->SetVersion('1.0.0.0'); +``` + ### Adding Tags Tags can be added to error data to provide extra information and to help filtering errors within Raygun. They are provided as an array of strings or numbers passed as the `5th argument to the SendError function` and as the `2nd argument to the SendException function`. -The declaration of the exception and error handlers using tags could look something like this: +The declaration of the exception and error handlers for adding tags to each payload could look something like this: ```php -$tags = array("testing-enviroment", "machine-4"); +SendError($errno, $errstr, $errfile, $errline, $tags); -} +$raygunClient = new RaygunClient($transport); -function exception_handler($exception) { - global $client, $tags; - $client->SendException($exception, $tags); -} +$tags = ['staging-environment', 'machine-4']; -function fatal_error() -{ - global $client; - $last_error = error_get_last(); - - if (!is_null($last_error)) { - $errno = $last_error['type']; - $errstr = $last_error['message']; - $errfile = $last_error['file']; - $errline = $last_error['line']; - $client->SendError($errno, $errstr, $errfile, $errline, $tags); - } +set_error_handler(function($errno, $errstr, $errfile, $errline) use ($raygunClient, $tags) { + $raygunClient->SendError($errno, $errstr, $errfile, $errline, $tags); +}); + +set_exception_handler(function($exception) use ($raygunClient, $tags) { + $raygunClient->SendException($exception, $tags); +}); + +register_shutdown_function(function() use ($raygunClient, $tags) { + $lastError = error_get_last(); + + if (!is_null($lastError)) { + [$type, $message, $file, $line] = $lastError; + $raygunClient->SendError($type, $message, $file, $line, $tags); + } +}); + +// ... +``` + +You can add tags when manually sending a handled exception like so: + +```php +try { + // Do something questionable... +} catch(Exception $exception) { + $raygunClient->SendException($exception, ['handled-exception']); } ``` ### Affected user tracking -**New in 1.5: additional data support** - -You can call $client->SetUser, passing in some or all of the following data, which will be used to provide an affected user count and reports: +You can call `$raygunClient->SetUser`, passing in some or all of the following data, which will be used to provide an affected user count and reports: ```php -SetUser($user = null, $firstName = null, $fullName = null, $email = null, $isAnonymous = null, $uuid = null) +$raygunClient->SetUser($user = 'email_or_unique_identifier', $firstName = 'Example', $fullName = 'Example User', $emailAddress = 'test@example.com', $isAnonymous = false, $uuid = 'abc123'); ``` -`$user` should be a unique identifier which is used to identify your users. If you set this to their email address, be sure to also set the $email parameter too. +`$user` should be a unique identifier which is used to identify your users. If you set this to their email address, be sure to also set the `$email` parameter too. + +This feature and values are optional if you wish to disable it for privacy concerns. To do so, pass `true` in as the second parameter to the RaygunClient constructor. -This feature and values are optional if you wish to disable it for privacy concerns. To do so, pass `true` in as the third parameter to the RaygunClient constructor. +```php +// Disable user tracking: +$raygunClient = new RaygunClient($transport, true); +``` Note that this data is stored as cookies. If you do not call SetUser the default is to store a random UUID to represent the user. -This feature can be used in CLI mode by calling SetUser() at the start of your session. +This feature can be used in CLI mode by calling `SetUser()` at the start of your session. ### Custom error grouping -Control of how error instances are grouped together can achieved by passing a callback to the `SetGroupingKey` method on the client. If the callback returns a string, ideally 100 characters or less, errors matching that key will grouped together. Overriding the default automatic grouping. If the callback returns a non-string value then that error will be grouped automatically. +Control of how error instances are grouped together can be achieved by passing a callback to the `SetGroupingKey` method on the client. If the callback returns a string, ideally 100 characters or less, errors matching that key will be grouped together, overriding the default automatic grouping. If the callback returns a non-string value then that error will be grouped automatically. ```php -$client = new \Raygun4php\RaygunClient("apiKey"); -$client->SetGroupingKey(function($payload, $stackTrace) { - // Inspect the above parameters and return a hash from the properties - - return $payload->Details->Error->Message; // Naive message-based grouping only +$raygunClient->SetGroupingKey(function($payload, $stackTrace) { + // Inspect the above parameters and return a hash from the properties ... + return $payload->Details->Error->Message; // Naive message-based grouping only }); ``` @@ -217,89 +259,84 @@ $client->SetGroupingKey(function($payload, $stackTrace) { Some error data will be too sensitive to transmit to an external service, such as credit card details or passwords. Since this data is very application specific, Raygun doesn't filter out anything by default. You can configure to either replace or otherwise transform specific values based on their keys. These transformations apply to form data (`$_POST`), custom user data, HTTP headers, and environment data (`$_SERVER`). It does not filter the URL or its `$_GET` parameters, or custom message strings. Since Raygun doesn't log method arguments in stack traces, those don't need filtering. All key comparisons are case insensitive. ```php -$client = new \Raygun4php\RaygunClient("apiKey"); -$client->setFilterParams(array( - 'password' => true, - 'creditcardnumber' => true, - 'ccv' => true, - 'php_auth_pw' => true, // filters basic auth from $_SERVER -)); -// Example input: array('Username' => 'myuser','Password' => 'secret') -// Example output: array('Username' => 'myuser','Password' => '[filtered]') +$raygunClient->setFilterParams([ + 'password' => true, + 'creditcardnumber' => true, + 'ccv' => true, + 'php_auth_pw' => true, // filters basic auth from $_SERVER +]); +// Example input: ['Username' => 'myuser','Password' => 'secret'] +// Example output: ['Username' => 'myuser','Password' => '[filtered]'] ``` You can also define keys as regular expressions: ```php -$client = new \Raygun4php\RaygunClient("apiKey"); -$client->setFilterParams(array( - '/^credit/i' => true, -)); -// Example input: array('CreditCardNumber' => '4111111111111111','CreditCardCcv' => '123') -// Example output: array('CreditCardNumber' => '[filtered]','CreditCardCcv' => '[filtered]') +$raygunClient->setFilterParams([ + '/^credit/i' => true, +]); +// Example input: ['CreditCardNumber' => '4111111111111111','CreditCardCcv' => '123'] +// Example output: ['CreditCardNumber' => '[filtered]','CreditCardCcv' => '[filtered]'] ``` In case you want to retain some hints on the data rather than removing it completely, you can also apply custom transformations through PHP's anonymous functions. The following example truncates all keys starting with "address". ```php -$client = new \Raygun4php\RaygunClient("apiKey"); -$client->setFilterParams(array( - 'Email' => function($key, $val) {return substr($val, 0, 5) . '...';} -)); -// Example input: array('Email' => 'test@test.com') -// Example output: array('Email' => 'test@...') +$raygunClient->setFilterParams([ + 'Email' => function($key, $val) {return substr($val, 0, 5) . '...';} +]); +// Example input: ['Email' => 'test@test.com'] +// Example output: ['Email' => 'test@...'] ``` Note that when any filters are defined, the Raygun error will no longer contain the raw HTTP data, since there's no effective way to filter it. ### Updating Cookie options -Cookies are used for the user tracking functionality of the Raygun4Php provider. In version 1.8 of the provider, the options passed to the `setcookie` method can now be customized to your needs. +Cookies are used for the user tracking functionality of the Raygun4PHP provider. In version 1.8 of the provider, the options passed to the `setcookie` method can now be customized to your needs. ```php -$client = new \Raygun4php\RaygunClient("apiKey"); -$client->SetCookieOptions(array( +$raygunClient->SetCookieOptions([ 'expire' => 2592000, // 30 * 24 * 60 * 60 'path' => '/', 'domain' => '', 'secure' => false, 'httponly' => false -)); +]); ``` ## Troubleshooting -As above, enable debug mode by instantiating the client with +As mentioned above, you can specify a logger to the HTTP transport: ```php -$client = new \Raygun4php\RaygunClient("apiKey", FALSE, TRUE); +$transport->setLogger($logger); ``` -This will echo the HTTP response code. Check the list above, and create an issue or contact us if you continue to have problems. +This will log out exceptions occurring while transmitting data to Raygun. [Create an issue](https://raygun.com/forums/forum/3) or [contact us](https://raygun.com/about/contact) if you continue to have problems. ### 400 from command-line Posix environments -If, when running a PHP script from the command line on *nix operating systems, you receive a '400 Bad Request' error (when debug mode is enabled), check to see if you have any LESS_TERMCAP environment variables set. These are not compatible with the current version of Raygun4PHP. As a workaround, unset these variables before your script runs, then reset them afterwards. +If, when running a PHP script from the command line on *nix operating systems, you receive a '400 Bad Request' error (in your debug log), check to see if you have any `LESS_TERMCAP` environment variables set. These are not compatible with the current version of Raygun4PHP. As a workaround, unset these variables before your script runs, then reset them afterwards. ### Error Control Operators (@) -If you are using the setup as described above errors will be send to Raygun regardless of any lines prepended with an error control operator (the @ symbol). To stop these errors from being sent to Raygun you can call PHP's [error_reporting](http://php.net/manual/en/function.error-reporting.php) method which return 0 if the triggered error was preceded by an @ symbol. +If you are using the setup as described above errors will be sent to Raygun regardless of any lines prepended with an error control operator (the @ symbol). To stop these errors from being sent to Raygun you can call PHP's [error_reporting](http://php.net/manual/en/function.error-reporting.php) method which return 0 if the triggered error was preceded by an @ symbol. _Error handler example:_ ```php -function error_handler($errno, $errstr, $errfile, $errline ) { - global $client; - if(error_reporting() !== 0) { - $client->SendError($errno, $errstr, $errfile, $errline); +function ($errno, $errstr, $errfile, $errline) use ($raygunClient) { + if (error_reporting() !== 0) { + $raygunClient->SendError($errno, $errstr, $errfile, $errline); } } ``` -See the [Error Control Operators section on PHP.net](http://php.net/manual/en/language.operators.errorcontrol.php) for more information +See the [Error Control Operators section on PHP.net](http://php.net/manual/en/language.operators.errorcontrol.php) for more information. ## Changelog -- 2.0.0 (unreleased): New major version +- 2.0.0: New major version - Increased minimum PHP version to 7.1 - Added PSR-4 autoloader - Removes `toJsonRemoveUnicodeSequences()` and `removeNullBytes()` methods from the RaygunClient class - use `toJson()` instead diff --git a/UsageExampleV2.md b/UsageExampleV2.md index 75eea07..91b9c01 100644 --- a/UsageExampleV2.md +++ b/UsageExampleV2.md @@ -1,12 +1,12 @@ ## Usage ```php - + $apiUrl, diff --git a/composer.json b/composer.json index e5b649f..d6c0f60 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "exceptions", "logging" ], - "homepage": "http://raygun.io", + "homepage": "http://raygun.com", "license": "MIT", "authors": [ { diff --git a/sample-apps/.gitignore b/sample-apps/.gitignore new file mode 100644 index 0000000..75122a5 --- /dev/null +++ b/sample-apps/.gitignore @@ -0,0 +1,2 @@ +debug.log +config.php diff --git a/sample-apps/async-example/README.md b/sample-apps/async-example/README.md new file mode 100644 index 0000000..da5ad61 --- /dev/null +++ b/sample-apps/async-example/README.md @@ -0,0 +1,6 @@ +## Setup instructions + +1. Install composer dependencies +2. Copy `config-sample.php` and rename to `config.php` +3. Insert your API key from Raygun into the `API_KEY` field +4. Submit the form with empty/zero values to trigger errors diff --git a/sample-apps/async-example/composer.json b/sample-apps/async-example/composer.json new file mode 100644 index 0000000..141b0cf --- /dev/null +++ b/sample-apps/async-example/composer.json @@ -0,0 +1,6 @@ +{ + "require": { + "mindscape/raygun4php": "2.x-dev", + "monolog/monolog": "2.0.2" + } +} diff --git a/sample-apps/async-example/config-sample.php b/sample-apps/async-example/config-sample.php new file mode 100644 index 0000000..c9e082a --- /dev/null +++ b/sample-apps/async-example/config-sample.php @@ -0,0 +1,2 @@ + + + + + + + + + +
+
+

Runner's pace calculator

+
+
+
+
+ + +
+
+ + +
+
+ + +
+ + hasSentData()) : ?> +
+

Average pace: getAveragePace(); ?>mins/km

+

Average speed: getAverageSpeed(); ?>km/hr

+
+ +
+
+
+ + + + diff --git a/sample-apps/async-example/partials/head.php b/sample-apps/async-example/partials/head.php new file mode 100644 index 0000000..c1d6c42 --- /dev/null +++ b/sample-apps/async-example/partials/head.php @@ -0,0 +1,15 @@ + +Runner's pace calculator + + + + + + diff --git a/sample-apps/async-example/raygunSetup.php b/sample-apps/async-example/raygunSetup.php new file mode 100644 index 0000000..62da951 --- /dev/null +++ b/sample-apps/async-example/raygunSetup.php @@ -0,0 +1,132 @@ +useAsync = $useAsync; + $this->logger = new Logger(self::LOGGER_NAME); + $this->logger->pushHandler(new StreamHandler(self::LOG_FILE_PATH)); + $this->logger->pushHandler(new FirePHPHandler()); + + $this->httpClient = new Client([ + 'base_uri' => self::RAYGUN_BASE_URI, + 'timeout' => self::HTTP_CLIENT_TIMEOUT, + 'headers' => [ + 'X-ApiKey' => API_KEY + ] + ]); + + if ($this->useAsync) { + $this->transport = new GuzzleAsync($this->httpClient); + } else { + $this->transport = new GuzzleSync($this->httpClient); + } + + $this->transport->setLogger($this->logger); + + $raygunClient = new RaygunClient($this->transport); + + // Get the logged-in user info to track affected user + $raygunClient->SetUser("test@example.com", "Test", "Test User", "test@example.com"); + + $this->tags = $tags; + + $this->raygunClient = $raygunClient; + } + + public function getRaygunClient(): RaygunClient + { + return $this->raygunClient; + } + + public function sendError($errno, $errstr, $errfile, $errline): void { + $this->raygunClient->SendError($errno, $errstr, $errfile, $errline, $this->tags); + } + + public function sendException($exception): void { + $this->raygunClient->SendException($exception, $this->tags); + } + + public function handleFatalError(): void + { + $lastError = error_get_last(); + + if (!is_null($lastError)) { + [$type, $message, $file, $line] = $lastError; + + $tags = array_merge($this->tags, ['fatal-error']); + $this->raygunClient->SendError($type, $message, $file, $line, $tags); + } + } + + public function flushAsyncPromises(): void + { + if ($this->useAsync) { + $this->transport->wait(); + } + } + } + + $raygunSetup = new RaygunSetup(['local']); + + set_error_handler([$raygunSetup, 'sendError']); + set_exception_handler([$raygunSetup, 'sendException']); + register_shutdown_function([$raygunSetup, 'handleFatalError']); + register_shutdown_function([$raygunSetup, 'flushAsyncPromises']); +} diff --git a/sample-apps/async-example/viewData.php b/sample-apps/async-example/viewData.php new file mode 100644 index 0000000..70c8eda --- /dev/null +++ b/sample-apps/async-example/viewData.php @@ -0,0 +1,76 @@ +time = $_POST['time']; + $this->distance = $_POST['distance']; + } + + /** + * @return int + */ + public function getTime(): ?int + { + return $this->time; + } + + /** + * @return float + */ + public function getDistance(): ?float + { + return $this->distance; + } + + /** + * @return bool + */ + public function hasSentData(): bool + { + return isset($this->time) && isset($this->distance); + } + + /** + * @return string + */ + public function getAveragePace(): string + { + return $this->decimalToMinuteString($this->time / $this->distance); + } + + /** + * @return string + */ + public function getAverageSpeed(): string + { + return round($this->distance / ($this->time / 60), self::SPEED_PRECISION); + } + + /** + * @param float $timeAsDecimal + * @return string + */ + private function decimalToMinuteString(float $timeAsDecimal): string + { + $whole = floor($timeAsDecimal); + $decimal = $timeAsDecimal - $whole; + $roundedMinutes = round($decimal * 60, 0); + $minutes = str_pad($roundedMinutes, 2, "0", STR_PAD_LEFT); + + return "{$whole}:{$minutes}"; + } +} diff --git a/sample-apps/sync-example/README.md b/sample-apps/sync-example/README.md new file mode 100644 index 0000000..ccb2e5f --- /dev/null +++ b/sample-apps/sync-example/README.md @@ -0,0 +1,6 @@ +## Setup instructions + +1. Install composer dependencies +2. Copy `config-sample.php` and rename to `config.php` +3. Insert your API key from Raygun into the `API_KEY` field +4. Submit the form with a zero value to trigger an error diff --git a/sample-apps/sync-example/composer.json b/sample-apps/sync-example/composer.json new file mode 100644 index 0000000..141b0cf --- /dev/null +++ b/sample-apps/sync-example/composer.json @@ -0,0 +1,6 @@ +{ + "require": { + "mindscape/raygun4php": "2.x-dev", + "monolog/monolog": "2.0.2" + } +} diff --git a/sample-apps/sync-example/config-sample.php b/sample-apps/sync-example/config-sample.php new file mode 100644 index 0000000..c9e082a --- /dev/null +++ b/sample-apps/sync-example/config-sample.php @@ -0,0 +1,2 @@ + + + + + + + Synchronous example + + + +

Synchronous example app

+ +
+
+ + +
+ + + +
Result:
+
+ + + diff --git a/sample-apps/sync-example/raygunSetup.php b/sample-apps/sync-example/raygunSetup.php new file mode 100644 index 0000000..7b57378 --- /dev/null +++ b/sample-apps/sync-example/raygunSetup.php @@ -0,0 +1,55 @@ +pushHandler(new StreamHandler(LOG_FILE_PATH)); + $logger->pushHandler(new FirePHPHandler()); + + $httpClient = new Client([ + 'base_uri' => RAYGUN_BASE_URI, + 'timeout' => HTTP_CLIENT_TIMEOUT, + 'headers' => [ + 'X-ApiKey' => API_KEY + ] + ]); + + $transport = new GuzzleSync($httpClient); + $transport->setLogger($logger); + + $raygunClient = new RaygunClient($transport, false, $logger); + $tags = ['local-environment', 'machine-4']; + + set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($raygunClient, $tags) { + $raygunClient->SendError($errno, $errstr, $errfile, $errline, $tags); + }); + + set_exception_handler(function ($exception) use ($raygunClient, $tags) { + $raygunClient->SendException($exception, $tags); + }); + + register_shutdown_function(function () use ($raygunClient, $tags) { + $lastError = error_get_last(); + + if (!is_null($lastError)) { + [$type, $message, $file, $line] = $lastError; + $_tags = array_merge($tags, ['fatal-error']); + $raygunClient->SendError($type, $message, $file, $line, $_tags); + } + }); +} + diff --git a/src/Raygun4php/RaygunClient.php b/src/Raygun4php/RaygunClient.php index bb29db1..7c813b7 100755 --- a/src/Raygun4php/RaygunClient.php +++ b/src/Raygun4php/RaygunClient.php @@ -4,7 +4,6 @@ use Raygun4php\Rhumsaa\Uuid\Uuid; use Raygun4php\Interfaces\TransportInterface; -use Raygun4php\Interfaces\RaygunMessageInterface; class RaygunClient { @@ -74,7 +73,7 @@ public function getDisableUserTracking() } /** - * Transmits an error to the Raygun.io API + * Transmits an error to the Raygun API * * @param int $errno The error number * @param string $errstr The error string @@ -84,7 +83,7 @@ public function getDisableUserTracking() * @param array $userCustomData An optional associative array that can be used to place custom key-value * @param int $timestamp Current Unix timestamp in the local timezone, used to set when an error occurred. * data in the message payload - * @return int The HTTP status code of the result when transmitting the message to Raygun.io + * @return int The HTTP status code of the result when transmitting the message to Raygun */ public function SendError( $errno, @@ -111,7 +110,7 @@ public function SendError( } /** - * Transmits an exception to the Raygun.io API + * Transmits an exception to the Raygun API * * @param \Throwable $throwable An exception object to transmit * @param array $tags An optional array of string tags used to provide metadata for the message @@ -119,7 +118,7 @@ public function SendError( * data in the message payload * @param int $timestamp Current Unix timestamp in the local timezone, used to set when an exception * occurred. - * @return int The HTTP status code of the result when transmitting the message to Raygun.io + * @return int The HTTP status code of the result when transmitting the message to Raygun */ public function SendException($throwable, $tags = null, $userCustomData = null, $timestamp = null) { @@ -356,7 +355,7 @@ private function is_assoc($array) } /** - * Transmits a RaygunMessage to the Raygun.io API. + * Transmits a RaygunMessage to the Raygun API. * This is a lower level function used by SendException and SendError and one of those should be used preferably. * * @param \Raygun4php\RaygunMessage $message A populated message to be posted to the Raygun API diff --git a/src/Raygun4php/RaygunClientMessage.php b/src/Raygun4php/RaygunClientMessage.php index 4ccccf5..0078273 100644 --- a/src/Raygun4php/RaygunClientMessage.php +++ b/src/Raygun4php/RaygunClientMessage.php @@ -11,7 +11,7 @@ class RaygunClientMessage public function __construct() { $this->Name = "Raygun4php"; - $this->Version = "1.8.3"; + $this->Version = "2.0.0"; $this->ClientUrl = "https://github.com/MindscapeHQ/raygun4php"; } }