Skip to content

Commit

Permalink
Merge pull request #121 from MindscapeHQ/sh/prep-for-release
Browse files Browse the repository at this point in the history
Prep for v2 release
  • Loading branch information
Samuel Holt committed Jan 27, 2020
2 parents 3a37375 + 1f320eb commit 45ebc02
Show file tree
Hide file tree
Showing 19 changed files with 572 additions and 152 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Expand Up @@ -20,3 +20,6 @@
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain

# Exclude from release
sample-apps export-ignore
319 changes: 178 additions & 141 deletions README.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions UsageExampleV2.md
@@ -1,12 +1,12 @@
## Usage

```php

<?php
require_once './vendor/autoload.php';

$apiUrl = 'https://api.raygun.io';
$apiUrl = 'https://api.raygun.com';
$proxy = 'some://proxy';
$apiKey = 'your-api-key';
$apiKey = 'INSERT_API_KEY_HERE';

$httpClient = new GuzzleHttp\Client([
'base_uri' => $apiUrl,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -8,7 +8,7 @@
"exceptions",
"logging"
],
"homepage": "http://raygun.io",
"homepage": "http://raygun.com",
"license": "MIT",
"authors": [
{
Expand Down
2 changes: 2 additions & 0 deletions sample-apps/.gitignore
@@ -0,0 +1,2 @@
debug.log
config.php
6 changes: 6 additions & 0 deletions 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
6 changes: 6 additions & 0 deletions sample-apps/async-example/composer.json
@@ -0,0 +1,6 @@
{
"require": {
"mindscape/raygun4php": "2.x-dev",
"monolog/monolog": "2.0.2"
}
}
2 changes: 2 additions & 0 deletions sample-apps/async-example/config-sample.php
@@ -0,0 +1,2 @@
<?php
const API_KEY = 'INSERT_YOUR_API_KEY_HERE';
46 changes: 46 additions & 0 deletions sample-apps/async-example/index.php
@@ -0,0 +1,46 @@
<?php
require_once 'raygunSetup.php';
require_once 'viewData.php';

$viewData = new ViewData();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<?php include 'partials/head.php' ?>
</head>
<body>

<div class="grid-container">
<div class="grid-x">
<h2>Runner's pace calculator</h2>
<div class="cell">
<form method="post" action="index.php">
<div class="grid-x grid-margin-x">
<div class="cell medium-6">
<label for="time">Time (minutes)</label>
<input id="time" name="time" type="number" value="<?php echo $viewData->getTime(); ?>"/>
</div>
<div class="cell medium-6 grid-margin-x">
<label for="distance">Distance (km) <strong>DANGER: DO NOT SET TO ZERO!!!</strong></label>
<input id="distance" name="distance" type="number" step="0.1" value="<?php echo $viewData->getDistance(); ?>"/>
</div>
</div>

<button type="submit" class="primary button">Calculate</button>
</form>

<?php if ($viewData->hasSentData()) : ?>
<div class="callout success">
<p><strong>Average pace:</strong> <?php echo $viewData->getAveragePace(); ?>mins/km</p>
<p><strong>Average speed:</strong> <?php echo $viewData->getAverageSpeed(); ?>km/hr</p>
</div>
<?php endif; ?>
</div>
</div>
</div>

</body>
</html>

15 changes: 15 additions & 0 deletions sample-apps/async-example/partials/head.php
@@ -0,0 +1,15 @@
<meta charset="UTF-8">
<title>Runner's pace calculator</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/foundation-sites@6.4.3/dist/css/foundation.min.css"
integrity="sha256-GSio8qamaXapM8Fq9JYdGNTvk/dgs+cMLgPeevOYEx0= sha384-wAweiGTn38CY2DSwAaEffed6iMeflc0FMiuptanbN4J+ib+342gKGpvYRWubPd/+ sha512-QHEb6jOC8SaGTmYmGU19u2FhIfeG+t/hSacIWPpDzOp5yygnthL3JwnilM7LM1dOAbJv62R+/FICfsrKUqv4Gg=="
crossorigin="anonymous">
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/foundation-sites@6.4.3/dist/js/foundation.min.js"
integrity="sha256-mRYlCu5EG+ouD07WxLF8v4ZAZYCA6WrmdIXyn1Bv9Vk= sha384-KzKofw4qqetd3kvuQ5AdapWPqV1ZI+CnfyfEwZQgPk8poOLWaabfgJOfmW7uI+AV sha512-0gHfaMkY+Do568TgjJC2iMAV0dQlY4NqbeZ4pr9lVUTXQzKu8qceyd6wg/3Uql9qA2+3X5NHv3IMb05wb387rA=="
crossorigin="anonymous"></script>
132 changes: 132 additions & 0 deletions sample-apps/async-example/raygunSetup.php
@@ -0,0 +1,132 @@
<?php

namespace {
require_once 'vendor/autoload.php';
require_once 'config.php';

use GuzzleHttp\Client;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
use Raygun4php\RaygunClient;
use Raygun4php\Transports\GuzzleAsync;
use Raygun4php\Transports\GuzzleSync;

/**
* Class RaygunSetup
*/
class RaygunSetup
{
const RAYGUN_BASE_URI = 'https://api.raygun.com';
const HTTP_CLIENT_TIMEOUT = 2.0;
const LOGGER_NAME = 'example_logger';
const LOG_FILE_PATH = __DIR__ . '/debug.log';

/**
* @var GuzzleAsync|GuzzleSync
*/
private $transport;

/**
* @var bool
*/
private $useAsync;

/**
* @var Logger
*/
private $logger;

/**
* @var Client
*/
private $httpClient;

/**
* @var RaygunClient
*/
private $raygunClient;

/**
* @var array
*/
private $tags;

/**
* RaygunSetup constructor.
* @param array $tags
* @param bool $useAsync
*/
public function __construct($tags = [], $useAsync = true)
{
$this->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']);
}
76 changes: 76 additions & 0 deletions sample-apps/async-example/viewData.php
@@ -0,0 +1,76 @@
<?php

class ViewData
{
/**
* @var int $time
*/
private $time;

/**
* @var float $distance
*/
private $distance;

const SPEED_PRECISION = 2;

public function __construct()
{
$this->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}";
}
}
6 changes: 6 additions & 0 deletions 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
6 changes: 6 additions & 0 deletions sample-apps/sync-example/composer.json
@@ -0,0 +1,6 @@
{
"require": {
"mindscape/raygun4php": "2.x-dev",
"monolog/monolog": "2.0.2"
}
}
2 changes: 2 additions & 0 deletions sample-apps/sync-example/config-sample.php
@@ -0,0 +1,2 @@
<?php
const API_KEY = 'INSERT_YOUR_API_KEY_HERE';
27 changes: 27 additions & 0 deletions sample-apps/sync-example/index.php
@@ -0,0 +1,27 @@
<?php
require_once 'raygunSetup.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Synchronous example</title>
</head>
<body>

<h1>Synchronous example app</h1>

<form method="post" action="index.php">
<fieldset>
<label for="num">Number to divide 42 by</label>
<input type="number" id="num" name="num" value="<?php echo $_POST['num'] ?? 0; ?>"/>
</fieldset>

<button type="submit">Submit</button>

<div>Result: <?php echo 42 / $_POST['num']; ?></div>
</form>
</body>
</html>

0 comments on commit 45ebc02

Please sign in to comment.