negundo-client/src/NegundoMiddleware.php

46 lines
1.3 KiB
PHP

<?php
namespace Negundo\Client;
use Laminas\Diactoros\Response\HtmlResponse;
use Psr\Http\Message\ResponseInterface,
Psr\Http\Message\ServerRequestInterface,
Psr\Http\Server\MiddlewareInterface,
Psr\Http\Server\RequestHandlerInterface;
class NegundoMiddleware extends Handler implements MiddlewareInterface {
protected ServerRequestInterface $request;
public $registerExceptionHandler = false;
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
$this->request = $request;
try {
$response = $handler->handle($request);
}
catch (\Throwable $ex)
{
$this->pushData($ex);
if ( $this->callback ?? false ) {
return call_user_func_array($this->callback, [ $ex, $request ] );
}
else {
throw $ex;
}
}
return $response ?? new HtmlResponse("...");
}
public function handleException(\Throwable $ex) : array
{
$server = isset($this->request) ? $this->request->getServerParams() : $_SERVER;
$body = isset($this->request) ? $this->request->getParsedBody() : $_POST;
return $this->exceptionHandler->extractExceptionData($ex, $server, $body);
}
}