First commit !

This commit is contained in:
mcndave committed 2019-07-03 13:28:35 +00:00
commit 755961039c
8 files changed
+140

No files matched your search

+26
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);
}
}
+62
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
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);