-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from MindscapeHQ/sh/prep-for-release
Prep for v2 release
- Loading branch information
Showing
19 changed files
with
572 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
debug.log | ||
config.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"require": { | ||
"mindscape/raygun4php": "2.x-dev", | ||
"monolog/monolog": "2.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?php | ||
const API_KEY = 'INSERT_YOUR_API_KEY_HERE'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<meta charset="UTF-8"> | ||
<title>Runner's pace calculator</title> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/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/[email protected]/dist/js/foundation.min.js" | ||
integrity="sha256-mRYlCu5EG+ouD07WxLF8v4ZAZYCA6WrmdIXyn1Bv9Vk= sha384-KzKofw4qqetd3kvuQ5AdapWPqV1ZI+CnfyfEwZQgPk8poOLWaabfgJOfmW7uI+AV sha512-0gHfaMkY+Do568TgjJC2iMAV0dQlY4NqbeZ4pr9lVUTXQzKu8qceyd6wg/3Uql9qA2+3X5NHv3IMb05wb387rA==" | ||
crossorigin="anonymous"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("[email protected]", "Test", "Test User", "[email protected]"); | ||
|
||
$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']); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"require": { | ||
"mindscape/raygun4php": "2.x-dev", | ||
"monolog/monolog": "2.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?php | ||
const API_KEY = 'INSERT_YOUR_API_KEY_HERE'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
|
Oops, something went wrong.