From 755961039c6880ace4ccf4c3cfa80e7be383a37d Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 3 Jul 2019 13:22:42 +0000 Subject: [PATCH] First commit ! --- .gitignore | 2 + CHANGELOG.md | 0 LICENSE | 21 +++++++++ README.md | 0 composer.json | 14 ++++++ src/Exception/JavascriptException.php | 26 +++++++++++ src/JavascriptMiddleware.php | 62 +++++++++++++++++++++++++++ src/WhoopsHandler.php | 15 +++++++ 8 files changed, 140 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Exception/JavascriptException.php create mode 100644 src/JavascriptMiddleware.php create mode 100644 src/WhoopsHandler.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c55784d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +composer.lock +/vendor/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e69de29 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cb3012e --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0e4da57 --- /dev/null +++ b/composer.json @@ -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": {} +} diff --git a/src/Exception/JavascriptException.php b/src/Exception/JavascriptException.php new file mode 100644 index 0000000..0a5a3c8 --- /dev/null +++ b/src/Exception/JavascriptException.php @@ -0,0 +1,26 @@ +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); + } + +} diff --git a/src/JavascriptMiddleware.php b/src/JavascriptMiddleware.php new file mode 100644 index 0000000..658a6d3 --- /dev/null +++ b/src/JavascriptMiddleware.php @@ -0,0 +1,62 @@ + 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)); + } + +} diff --git a/src/WhoopsHandler.php b/src/WhoopsHandler.php new file mode 100644 index 0000000..ab94481 --- /dev/null +++ b/src/WhoopsHandler.php @@ -0,0 +1,15 @@ +allowQuit(false); +$whoops->writeToOutput(false); +$whoops->pushHandler(new Handler()); + +IF CLI { + $handler->handleUnconditionally(true); +} + +$body = $whoops->handleException($this->exception);