- Dump middleware
This commit is contained in:
commit
6d1d15421d
|
@ -0,0 +1,19 @@
|
||||||
|
# The MIT License
|
||||||
|
|
||||||
|
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,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"name": "mcnd/dump",
|
||||||
|
"description": "A simple dump function which ease debugging quielty even on production server.",
|
||||||
|
"keywords": ["dump","var_dump","dev","debug","psr15","middleware"],
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Dave Mc Nicoll",
|
||||||
|
"email": "info@mcnd.ca"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Dump\\": "src/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Dump;
|
||||||
|
|
||||||
|
class Dump {
|
||||||
|
/**
|
||||||
|
* Latest stack trace found from the debugger
|
||||||
|
* @var
|
||||||
|
*/
|
||||||
|
protected $trace = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
protected $content = null;
|
||||||
|
|
||||||
|
public function __construct(...$content) {
|
||||||
|
$this->trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2] ?? [
|
||||||
|
"line" => -1,
|
||||||
|
"file" => "unknown",
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->content = $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderArray() {
|
||||||
|
return [
|
||||||
|
'content' => $this->content,
|
||||||
|
'file' => $this->trace['file'],
|
||||||
|
'line' => $this->trace['line'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderHtml() {
|
||||||
|
$data = $this->getDumpContent();
|
||||||
|
|
||||||
|
return <<<HTML
|
||||||
|
<pre style='-webkit-overflow-scrolling: touch;background-color: #f5f5f5;color: #4a4a4a;font-size: 12px;line-height:18px;overflow-x: auto;padding: 0.5rem 1.5rem;word-wrap: normal;white-space: pre;margin:0.33rem 0;'>
|
||||||
|
<div style='color:#9f9f9f;font-size:10px;' title='{$this->trace['file']}'>[ file: {$this->trace['file']}:{$this->trace['line']} ]</div>
|
||||||
|
$data
|
||||||
|
</pre>
|
||||||
|
HTML;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getDumpContent() {
|
||||||
|
ob_start();
|
||||||
|
var_dump(...($this->content !== [] ? $this->content : [null]));
|
||||||
|
return ob_get_clean();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,81 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Dump {
|
||||||
|
|
||||||
|
use Psr\Http\Server\MiddlewareInterface,
|
||||||
|
Psr\Http\Message\ServerRequestInterface,
|
||||||
|
Psr\Http\Server\RequestHandlerInterface,
|
||||||
|
Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
use Laminas\Diactoros\Response,
|
||||||
|
Laminas\Diactoros\Stream,
|
||||||
|
Laminas\Diactoros\Response\JsonResponse,
|
||||||
|
Laminas\Diactoros\Response\HtmlResponse;
|
||||||
|
|
||||||
|
use function stream_copy_to_stream;
|
||||||
|
|
||||||
|
class DumpMiddleware implements MiddlewareInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var [class]
|
||||||
|
*/
|
||||||
|
protected static $dump_stack = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processing the dump stack accumulated in the request
|
||||||
|
* @param ServerRequestInterface $request
|
||||||
|
* @param RequestHandlerInterface $handler
|
||||||
|
* @return ResponseInterface
|
||||||
|
*/
|
||||||
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
|
||||||
|
{
|
||||||
|
$response = $handler->handle($request);
|
||||||
|
|
||||||
|
if ( empty(static::$dump_stack) ) {
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
$stream = new class("php://memory", "rw") extends Stream {
|
||||||
|
public function append_resource($resource) {
|
||||||
|
stream_copy_to_stream($resource, $this->resource);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
switch (true) {
|
||||||
|
case $response instanceof HtmlResponse:
|
||||||
|
$body = $response->getBody();
|
||||||
|
|
||||||
|
foreach(static::$dump_stack as $item) {
|
||||||
|
$stream->write($item->renderHtml());
|
||||||
|
}
|
||||||
|
|
||||||
|
$stream->append_resource($body->detach());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case $response instanceof JsonResponse:
|
||||||
|
foreach(static::$dump_stack as $item) {
|
||||||
|
$dump[] = $item->renderArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
$stream->write(json_encode(array_merge([ "_dump" => $dump ?? [] ], json_decode($response->getBody()->getContents() ?? "{}", true)), JsonResponse::DEFAULT_JSON_FLAGS));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response->withBody($stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function dump(...$what)
|
||||||
|
{
|
||||||
|
static::$dump_stack[] = $dump = new Dump(...$what);
|
||||||
|
return $dump;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
if (! function_exists('dump') ) {
|
||||||
|
function dump(...$what) {
|
||||||
|
return \CSLSJ\Dump\DumpMiddleware::dump(...$what);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue