Compare commits

...
19 Commits
Author SHA1 Message Date
Dev ef5da7bb7e - Bugfix for json_encode() which do not result in an array 2026-06-09 19:23:23 +00:00
Dave Mc Nicoll ff4ade51fb - Refractoring 2026-05-21 15:21:03 +00:00
mcndave b9416c580b - Fixed missing whitespaces 2025-11-06 17:19:40 +00:00
Dave Mc Nicoll 49148e87d4 - Added details tag in HTML output for better visual and accessibility 2025-09-17 18:44:52 +00:00
Dave Mc Nicoll 9526cb8e2e - Added JSON type from variables 2024-12-02 15:25:29 -05:00
mcndave a768d45a45 - Autoload was not properly configured 2024-11-04 14:57:10 +00:00
mcndave 401fe15c18 - Added a Picea extension 2024-11-04 09:39:29 -05:00
Dave Mc Nicoll eb378eb21e - WIP on Dump() encoder 2024-11-01 16:13:18 -04:00
Dave Mc Nicoll b99ff24d26 - Added time output 2024-04-24 11:25:08 -04:00
mcndave 6c074a249e Merge branch 'master' of https://git.mcnd.ca/mcndave/dump 2024-02-01 14:16:51 +00:00
mcndave 8b85beb942 - Nothing to change... 2024-02-01 14:16:40 +00:00
mcndave 10dac37e43 - fixed a bug where json was not appended into dimp 2023-11-08 06:53:49 -05:00
Dave Mc Nicoll db3c7eb411 - Reworked a bit of logic into the code to skip rewriting content 2023-11-01 11:33:03 -04:00
mcndave d16c55d0f8 - Added renderText() again 2023-08-21 18:47:44 +00:00
Dave Mc Nicoll bfd4edad5e - Removed the current env display, to be reworked ... 2023-05-30 15:41:55 -04:00
mcndave abb0ad3a6a - Fixed Whoops error messages not showing 2023-02-01 18:51:47 +00:00
mcndave 6af3c33b7d - Added a new DEV env tag on HTML pages 2023-01-31 19:05:06 +00:00
mcndave 7d1bfb6bd3 Merge branch 'master' of https://git.mcnd.ca/mcndave/dump 2022-11-29 19:55:45 +00:00
mcndave 452ac383b4 - Added a '?' whenever we're missing an information 2022-11-29 19:55:26 +00:00
5 changed files with 138 additions and 19 deletions

No files matched your search

+9
View File
@@ -13,5 +13,14 @@
"psr-4": {
"Dump\\": "src/"
}
},
"extra": {
"lean": {
"autoload": {
"definitions" : [
"meta/definitions.php"
]
}
}
}
}
+9
View File
@@ -0,0 +1,9 @@
<?php
use function DI\autowire, DI\create, DI\get;
use Dump\Picea\DumpExtension;
return [
DumpExtension::class => autowire(DumpExtension::class),
];
+82 -9
View File
@@ -15,36 +15,109 @@ class Dump {
protected $content = null;
public function __construct(...$content) {
$this->trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2] ?? [
$this->trace = (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2] ?? [
"line" => -1,
"file" => "unknown",
];
]) + [ 'time' => round(1000 * (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]), 4) ];
$this->content = $content;
}
public function renderArray() {
return [
'content' => $this->content,
'content' => $this->arrayKeysToTypes($this->content),
'file' => $this->trace['file'] ?? '?',
'line' => $this->trace['line'] ?? '?',
'time' => $this->trace['time'],
];
}
public function renderHtml() {
$data = $this->getDumpContent();
$data = implode('', array_map(fn($e) => "<details style='padding-left:5px' open><summary></summary><p style='margin-top: -1.1rem;margin-left: 15px;'>$e</p></details>", $this->getDumpContent(true)));
$this->trace['file'] ??= "?";
$this->trace['line'] ??= "?";
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;position: relative;z-index:9000000000;'>
<div style='color:#9f9f9f;font-size:10px;' title='{$this->trace['file']}'>[ file: {$this->trace['file']}:{$this->trace['line']} ]</div>
$data
<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-wrap;margin:0.33rem 0;position: relative;z-index:9999999999;'>
<details open><!--
--><summary><span style='color:#9f9f9f;font-size:10px;' title='{$this->trace['file']}'>[ file: {$this->trace['file']}:{$this->trace['line']} ] <small>timestamp: {$this->trace['time']}</small></span> </summary><!--
--><p>$data</p>
</details>
</pre>
HTML;
}
protected function getDumpContent() {
public function renderText() {
$data = $this->getDumpContent();
$this->trace['file'] ??= "?";
$this->trace['line'] ??= "?";
$header = "File: {$this->trace['file']} | line : {$this->trace['line']} | time : {$this->trace['time']}";
return str_repeat('-', strlen($header)) . PHP_EOL . $header . PHP_EOL . str_repeat('-', strlen($header)) . PHP_EOL . $data . PHP_EOL;
}
protected function getDumpContent(bool $split = false) : string|array {
if ($split) {
return array_map(fn($e) => $this->renderDump([ $e ]), $this->content ?: [ null ]);
}
return $this->renderDump($this->content !== [] ? $this->content : [null]);
}
protected function renderDump(mixed $any) : string
{
ob_start();
var_dump(...($this->content !== [] ? $this->content : [null]));
var_dump(...$any);
return ob_get_clean();
}
protected function arrayKeysToTypes(array $content) : array
{
$newKeys = [];
foreach($content as $key => $obj) {
$newKeys[] = sprintf("%s-%s", $key, $this->typeAsKey($obj));
}
return array_combine($newKeys, $content);
}
protected function typeAsKey(mixed $obj) : string
{
if (is_object($obj)) {
return $obj::class;
}
elseif (is_array($obj)) {
return "array";
}
elseif (is_string($obj)) {
return "string";
}
elseif (is_int($obj)) {
return "integer";
}
elseif (is_float($obj)) {
return "float";
}
elseif(is_callable($obj)) {
return "callable";
}
elseif(is_bool($obj)) {
return "boolean";
}
elseif(is_double($obj)){
return "double";
}
elseif(is_long($obj)){
return "long";
}
elseif(is_iterable($obj)) {
return "iterable";
}
return gettype($obj);
}
}
+23 -10
View File
@@ -1,7 +1,6 @@
<?php declare(strict_types=1);
namespace Dump {
use Psr\Http\Server\MiddlewareInterface,
Psr\Http\Message\ServerRequestInterface,
Psr\Http\Server\RequestHandlerInterface,
@@ -28,20 +27,24 @@ namespace Dump {
{
$response = $handler->handle($request);
if ( empty(static::$dump_stack) ) {
return $response;
$stream = $this->createStream($response);
return $response->withBody($stream);
}
public function createStream(ResponseInterface $response) : Stream
{
$stream = new class("php://memory", "rw") extends Stream {
public function append_resource($resource) {
stream_copy_to_stream($resource, $this->resource);
}
};
$body = $response->getBody();
$body->rewind();
switch (true) {
case $response instanceof TextResponse:
$body = $response->getBody();
foreach(static::$dump_stack as $item) {
$stream->write($item->renderText());
}
@@ -50,17 +53,27 @@ namespace Dump {
break;
case $response instanceof JsonResponse:
foreach(static::$dump_stack as $item) {
foreach(static::$dump_stack as $key => $item) {
$dump[] = $item->renderArray();
}
$stream->write(json_encode(array_merge([ "_dump" => $dump ?? [] ], json_decode($response->getBody()->getContents() ?? "{}", true)), JsonResponse::DEFAULT_JSON_FLAGS));
if (isset($dump)) {
$json = json_encode(array_merge(["_dump" => $dump ], (array) json_decode($body->getContents() ?? "{}", true)), JsonResponse::DEFAULT_JSON_FLAGS);
$stream->write(! is_string($json) ? json_encode($json, JsonResponse::DEFAULT_JSON_FLAGS) : $json);
}
$stream->append_resource($body->detach());
break;
case $response instanceof HtmlResponse:
#$stream->write(<<<HTML
# <span style="position:fixed;right:0;bottom:0;color:#4b4b4b;padding:2px 6px;z-index:99999999999;pointer-events:none;opacity:0.6;font-size:0.8em">env: DEV</span>
#HTML);
# no break;
default:
case $response instanceof Response:
$body = $response->getBody();
foreach(static::$dump_stack as $item) {
$stream->write($item->renderHtml());
}
@@ -69,7 +82,7 @@ namespace Dump {
break;
}
return $response->withBody($stream);
return $stream;
}
public static function dump(...$what)
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace Dump\Picea;
use Picea\{ Extension\Extension, Extension\FunctionExtension };
class DumpExtension implements Extension {
public array $token = [ "dump" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
return "<?php \dump($arguments) ?>";
}
}