First commit !

This commit is contained in:
Dave M. 2019-07-03 13:22:42 +00:00
commit 755961039c
8 changed files with 140 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
composer.lock
/vendor/

0
CHANGELOG.md Normal file
View File

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2019 Dave Mc Nicoll
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0
README.md Normal file
View File

14
composer.json Normal file
View File

@ -0,0 +1,14 @@
{
"name": "mcnd/thebugs",
"description": "A PSR-15 compliant library helping to trace errors in development and production environment",
"keywords" : ["debug","error","bug"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Dave Mc Nicoll",
"email": "info@mcnd.ca"
}
],
"require": {}
}

View File

@ -0,0 +1,26 @@
<?php
namespace TheBugs\Exception;
class JavascriptException extends \Exception {
protected $url;
protected $stacktrace;
public function __construct(string $message, int $code, Exception $previous = null, string $file, int $line, string $url, array $trace)
{
$this->message = $message;
$this->code = $code;
$this->previous = $previous;
$this->file = $file;
$this->line = $line;
$this->url = $url;
$this->stacktrace = $trace;
}
public function __toString() {
return __CLASS__ . ": [{$this->url}:{$this->code}]: {$this->message}\n" . json_encode($this->stacktrace);
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace TheBugs;
use Psr\Http\Message\ResponseFactoryInterface,
Psr\Container\ContainerInterface,
Psr\Http\Message\ResponseInterface,
Psr\Http\Message\ServerRequestInterface,
Psr\Http\Server\MiddlewareInterface,
Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response,
Zend\Diactoros\ServerRequest,
Zend\Diactoros\Stream,
Zend\Diactoros\Uri;
class JavascriptMiddleware implements MiddlewareInterface
{
const FILTERS = [
"code" => null,
"headers" => [
"User-Agent" => "TheBugs/1.0",
],
];
/**
* Filter list, allows custom invokable
* @var array
*/
protected $filters = [];
public function __construct($options = [])
{
foreach(static::FILTERS as $key => $default) {
$this->filters[$key] = ( $options[$key] ?? false ) ? $options[$key] : $default;
}
}
public function throwError($errorDetails)
{
throw new Exception\JavascriptException($errorDetails['message'], 0, null, $errorDetails['location'], $errorDetails['line'], $errorDetails['url'], (array) $errorDetails['stack']);
}
/**
* Process a server request and return a response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ( ( $this->filters['code'] ?? false ) && ($this->filters['code'] !== $request->getCode())) {
return $handler->handle($request);
}
foreach($this->filters['headers'] as $key => $value) {
if ( ! in_array($value, $request->getHeader($key)) ) {
return $handler->handle($request);
}
}
$this->throwError(json_decode($request->getBody(), true));
}
}

15
src/WhoopsHandler.php Normal file
View File

@ -0,0 +1,15 @@
<?php
use Whoops\Run as Whoops;
use Whoops\Handler\PrettyPageHandler as Handler;
$whoops = new Whoops();
$whoops->allowQuit(false);
$whoops->writeToOutput(false);
$whoops->pushHandler(new Handler());
IF CLI {
$handler->handleUnconditionally(true);
}
$body = $whoops->handleException($this->exception);