commit 6d1d15421d4b752650b98b9f14b6ea13fac16d91 Author: Dave Mc Nicoll Date: Wed Oct 14 14:33:26 2020 -0400 - Dump middleware diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..81d7660 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0e7e2bf --- /dev/null +++ b/composer.json @@ -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/" + } + } +} diff --git a/src/Dump.php b/src/Dump.php new file mode 100644 index 0000000..feae2a7 --- /dev/null +++ b/src/Dump.php @@ -0,0 +1,50 @@ +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 << +
[ file: {$this->trace['file']}:{$this->trace['line']} ]
+$data + +HTML; + } + + protected function getDumpContent() { + ob_start(); + var_dump(...($this->content !== [] ? $this->content : [null])); + return ob_get_clean(); + } +} diff --git a/src/DumpMiddleware.php b/src/DumpMiddleware.php new file mode 100644 index 0000000..7cd6201 --- /dev/null +++ b/src/DumpMiddleware.php @@ -0,0 +1,81 @@ +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); + } + } +}