From 487094c77a68240efc935aca53f4b60ca2b43909 Mon Sep 17 00:00:00 2001 From: Rodolfo Berrios Date: Fri, 24 Jan 2020 16:06:46 -0300 Subject: [PATCH] wip --- .../Screen/Interfaces/ScreenInterface.php | 4 +- Chevere/Components/Screen/Screen.php | 21 +++---- Chevere/Components/Screen/SilentScreen.php | 2 +- .../Components/Screen/Tests/ScreenTest.php | 10 ++-- Chevere/Components/VarDump/Dumper.php | 5 +- .../VarDump/Dumpers/AbstractDumper.php | 31 +++++----- .../VarDump/Dumpers/ConsoleDumper.php | 8 +-- .../Components/VarDump/Dumpers/HtmlDumper.php | 8 +-- .../VarDump/Dumpers/PlainDumper.php | 8 +-- .../VarDump/Interfaces/DumperInterface.php | 4 -- .../VarDump/Interfaces/PalleteInterface.php | 59 ------------------- .../Components/VarDump/Tests/DumperTest.php | 6 +- .../VarDump/Wrappers/ConsoleWrapper.php | 33 +++++++++-- .../VarDump/Wrappers/HtmlWrapper.php | 32 +++++++++- .../AssertKeyTrait.php} | 14 ++--- resources/functions/dump.php | 12 ++-- 16 files changed, 122 insertions(+), 135 deletions(-) rename Chevere/Components/VarDump/Wrappers/{AbstractWrapper.php => Traits/AssertKeyTrait.php} (72%) diff --git a/Chevere/Components/Screen/Interfaces/ScreenInterface.php b/Chevere/Components/Screen/Interfaces/ScreenInterface.php index d003f680e..5137d0b04 100644 --- a/Chevere/Components/Screen/Interfaces/ScreenInterface.php +++ b/Chevere/Components/Screen/Interfaces/ScreenInterface.php @@ -41,7 +41,7 @@ public function attachNl(string $display): ScreenInterface; public function queue(): array; /** - * Show the screen queue contents. + * Emit the screen queue contents. */ - public function show(): ScreenInterface; + public function emit(): ScreenInterface; } diff --git a/Chevere/Components/Screen/Screen.php b/Chevere/Components/Screen/Screen.php index 0185c5e3c..2c5f18740 100644 --- a/Chevere/Components/Screen/Screen.php +++ b/Chevere/Components/Screen/Screen.php @@ -15,6 +15,8 @@ use Chevere\Components\Screen\Interfaces\FormatterInterface; use Chevere\Components\Screen\Interfaces\ScreenInterface; +use Generator; +use GuzzleHttp\Psr7\AppendStream; use Psr\Http\Message\StreamInterface; use function GuzzleHttp\Psr7\stream_for; @@ -70,18 +72,17 @@ public function queue(): array return $this->queue; } - public function show(): ScreenInterface + public function emit(): ScreenInterface { $this->handleTrace(); - // TODO YIELD HERE foreach ($this->queue as $stream) { - // if ($stream->isSeekable()) { - // $stream->rewind(); - // } - // while (!$stream->eof()) { - // echo $stream->read(1024 * 8); - // } - echo $stream; + if ($stream->isSeekable()) { + $stream->rewind(); + } + while (!$stream->eof()) { + echo $stream->read(1024 * 8); + } + $stream->detach(); } $this->queue = []; @@ -91,7 +92,7 @@ public function show(): ScreenInterface private function handleTrace(): void { if (!$this->traceability) { - // return; + return; } $bt = debug_backtrace(0, 2); $caller = $bt[1]; diff --git a/Chevere/Components/Screen/SilentScreen.php b/Chevere/Components/Screen/SilentScreen.php index f575124eb..7afe11a3a 100644 --- a/Chevere/Components/Screen/SilentScreen.php +++ b/Chevere/Components/Screen/SilentScreen.php @@ -57,7 +57,7 @@ public function queue(): array return []; } - public function show(): ScreenInterface + public function emit(): ScreenInterface { return $this; } diff --git a/Chevere/Components/Screen/Tests/ScreenTest.php b/Chevere/Components/Screen/Tests/ScreenTest.php index bd89810f5..cd3bbd187 100644 --- a/Chevere/Components/Screen/Tests/ScreenTest.php +++ b/Chevere/Components/Screen/Tests/ScreenTest.php @@ -14,6 +14,7 @@ namespace Chevere\Components\Screen\Tests; use PHPUnit\Framework\TestCase; +use function GuzzleHttp\Psr7\stream_for; final class ScreenTest extends TestCase { @@ -21,12 +22,11 @@ public function testConstruct(): void { $this->expectNotToPerformAssertions(); // xdump($this); - // xdd(screen()->debug()->attach('Fatal error at: ' . __FILE__)); - screens()->console()->attach('Fatal error at: ' . __FILE__)->show(); + // xdump(screens()->debug()->attach('Fatal error at: ' . __FILE__)); // xdd(screens()->debug()->trace()); - // screens()->console()->attach('Fatal error at: ' . __FILE__)->show(); - // screen()->runtime()->attachNl(varInfo($this))->display(); - // screen()->runtime()->attachNl('Everything is OK! Keep going...')->display(); + screens()->console()->attach('Fatal error at: ' . __FILE__)->emit(); + screens()->runtime()->attachNl(varInfo(screens()->console()))->emit(); + // screens()->runtime()->attachNl('Everything is OK! Keep going...')->emit(); // $screen = new Screen; // $screen diff --git a/Chevere/Components/VarDump/Dumper.php b/Chevere/Components/VarDump/Dumper.php index 41d8097a4..463b527b4 100644 --- a/Chevere/Components/VarDump/Dumper.php +++ b/Chevere/Components/VarDump/Dumper.php @@ -15,6 +15,7 @@ use Chevere\Components\App\Instances\BootstrapInstance; use Chevere\Components\Common\Interfaces\ToStringInterface; +use Chevere\Components\Screen\Interfaces\ScreenInterface; use Chevere\Components\VarDump\Dumpers\ConsoleDumper; use Chevere\Components\VarDump\Dumpers\HtmlDumper; @@ -38,8 +39,8 @@ public function toString(): string return $this->dumped; } - public function toScreen(): void + public function toScreen(ScreenInterface $screen): void { - screens()->runtime()->attachNl($this->dumped)->show(); + $screen->attachNl($this->dumped)->emit(); } } diff --git a/Chevere/Components/VarDump/Dumpers/AbstractDumper.php b/Chevere/Components/VarDump/Dumpers/AbstractDumper.php index 2cf748843..8829f06e6 100644 --- a/Chevere/Components/VarDump/Dumpers/AbstractDumper.php +++ b/Chevere/Components/VarDump/Dumpers/AbstractDumper.php @@ -38,35 +38,35 @@ abstract class AbstractDumper implements DumperInterface */ final public function __construct() { - $this->formatter = $this->getFormatter(); - $this->outputter = $this->getOutputter(); + $this->formatter = $this->formatter(); + $this->outputter = $this->outputter(); } /** * {@inheritdoc} */ - abstract public function getFormatter(): FormatterInterface; + abstract public function formatter(): FormatterInterface; /** * {@inheritdoc} */ - abstract public function getOutputter(): OutputterInterface; + abstract public function outputter(): OutputterInterface; /** * {@inheritdoc} */ - public function formatter(): FormatterInterface - { - return $this->formatter; - } + // public function formatter(): FormatterInterface + // { + // return $this->formatter; + // } /** * {@inheritdoc} */ - public function outputter(): OutputterInterface - { - return $this->outputter; - } + // public function outputter(): OutputterInterface + // { + // return $this->outputter; + // } /** * {@inheritdoc} @@ -79,9 +79,6 @@ final public function withVars(...$vars): DumperInterface return $new; } $new->setDebugBacktrace(); - $new->output = $new->outputter - ->withDumper($new) - ->toString(); return $new; } @@ -99,7 +96,9 @@ final public function vars(): array */ final public function toString(): string { - return $this->output; + return $this->outputter + ->withDumper($this) + ->toString(); } /** diff --git a/Chevere/Components/VarDump/Dumpers/ConsoleDumper.php b/Chevere/Components/VarDump/Dumpers/ConsoleDumper.php index c21787c56..5f9496b6f 100644 --- a/Chevere/Components/VarDump/Dumpers/ConsoleDumper.php +++ b/Chevere/Components/VarDump/Dumpers/ConsoleDumper.php @@ -20,13 +20,13 @@ final class ConsoleDumper extends AbstractDumper { - public function getFormatter(): FormatterInterface + public function formatter(): FormatterInterface { - return new ConsoleFormatter(); + return $this->formatter ??= new ConsoleFormatter; } - public function getOutputter(): OutputterInterface + public function outputter(): OutputterInterface { - return new ConsoleOutputter(); + return $this->outputter ??= new ConsoleOutputter; } } diff --git a/Chevere/Components/VarDump/Dumpers/HtmlDumper.php b/Chevere/Components/VarDump/Dumpers/HtmlDumper.php index 83b4b9130..f4506a635 100644 --- a/Chevere/Components/VarDump/Dumpers/HtmlDumper.php +++ b/Chevere/Components/VarDump/Dumpers/HtmlDumper.php @@ -20,13 +20,13 @@ final class HtmlDumper extends AbstractDumper { - public function getFormatter(): FormatterInterface + public function formatter(): FormatterInterface { - return new HtmlFormatter(); + return $this->formatter ??= new HtmlFormatter; } - public function getOutputter(): OutputterInterface + public function outputter(): OutputterInterface { - return new HtmlOutputter(); + return $this->outputter ??= new HtmlOutputter; } } diff --git a/Chevere/Components/VarDump/Dumpers/PlainDumper.php b/Chevere/Components/VarDump/Dumpers/PlainDumper.php index 2ff9d6a09..794b69935 100644 --- a/Chevere/Components/VarDump/Dumpers/PlainDumper.php +++ b/Chevere/Components/VarDump/Dumpers/PlainDumper.php @@ -20,13 +20,13 @@ final class PlainDumper extends AbstractDumper { - public function getFormatter(): FormatterInterface + public function formatter(): FormatterInterface { - return new PlainFormatter(); + return $this->formatter ??= new PlainFormatter; } - public function getOutputter(): OutputterInterface + public function outputter(): OutputterInterface { - return new PlainOutputter(); + return $this->outputter ??= new PlainOutputter; } } diff --git a/Chevere/Components/VarDump/Interfaces/DumperInterface.php b/Chevere/Components/VarDump/Interfaces/DumperInterface.php index ebfcd2fca..7b727e821 100644 --- a/Chevere/Components/VarDump/Interfaces/DumperInterface.php +++ b/Chevere/Components/VarDump/Interfaces/DumperInterface.php @@ -27,12 +27,8 @@ interface DumperInterface public function formatter(): FormatterInterface; - public function getFormatter(): FormatterInterface; - public function outputter(): OutputterInterface; - public function getOutputter(): OutputterInterface; - /** * Return an instance with the specified vars. * diff --git a/Chevere/Components/VarDump/Interfaces/PalleteInterface.php b/Chevere/Components/VarDump/Interfaces/PalleteInterface.php index 18e210a5f..e69de29bb 100644 --- a/Chevere/Components/VarDump/Interfaces/PalleteInterface.php +++ b/Chevere/Components/VarDump/Interfaces/PalleteInterface.php @@ -1,59 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Chevere\Components\VarDump\Interfaces; - -use Chevere\Components\Type\Interfaces\TypeInterface; - -interface PalleteInterface -{ - /** - * Color palette used in HTML. - */ - const HTML = [ - TypeInterface::STRING => '#e67e22', // orange - TypeInterface::FLOAT => '#f1c40f', // yellow - TypeInterface::INTEGER => '#f1c40f', // yellow - TypeInterface::BOOLEAN => '#9b59b6', // purple - TypeInterface::NULL => '#7f8c8d', // grey - TypeInterface::OBJECT => '#e74c3c', // red - TypeInterface::ARRAY => '#2ecc71', // green - VarDumpInterface::_FILE => 'inherith', - VarDumpInterface::_CLASS => '#3498db', // blue - VarDumpInterface::_OPERATOR => '#7f8c8d', // grey - VarDumpInterface::_FUNCTION => '#9b59b6', // purple - VarDumpInterface::_PRIVACY => '#9b59b6', // purple - VarDumpInterface::_VARIABLE => '#e67e22', // orange - VarDumpInterface::_EMPHASIS => '#7f8c8d', - ]; - - /** - * Color palette used in CLI. - */ - const CONSOLE = [ - TypeInterface::STRING => 'color_11', - TypeInterface::FLOAT => 'color_11', - TypeInterface::INTEGER => 'color_11', - TypeInterface::BOOLEAN => 'color_163', // purple - TypeInterface::NULL => 'color_245', // grey - TypeInterface::OBJECT => 'color_39', - TypeInterface::ARRAY => 'color_41', // green - VarDumpInterface::_FILE => 'default', - VarDumpInterface::_CLASS => 'color_147', // blue - VarDumpInterface::_OPERATOR => 'color_245', // grey - VarDumpInterface::_FUNCTION => 'color_39', - VarDumpInterface::_PRIVACY => 'color_133', - VarDumpInterface::_VARIABLE => 'color_208', - VarDumpInterface::_EMPHASIS => ['color_245', 'italic'] - ]; -} diff --git a/Chevere/Components/VarDump/Tests/DumperTest.php b/Chevere/Components/VarDump/Tests/DumperTest.php index dd3b2975f..1a616b74c 100644 --- a/Chevere/Components/VarDump/Tests/DumperTest.php +++ b/Chevere/Components/VarDump/Tests/DumperTest.php @@ -36,9 +36,9 @@ private function getVars(): array private function getDumpers(): array { return [ - 'PlainDumper' => new PlainDumper(), + // 'PlainDumper' => new PlainDumper(), 'ConsoleDumper' => new ConsoleDumper(), - 'HtmlDumper' => new HtmlDumper(), + // 'HtmlDumper' => new HtmlDumper(), ]; } @@ -78,7 +78,7 @@ public function testDumpers(): void $disk = include sprintf('resources/%s-dumped.php', $shortName); $line = $dumper->debugBacktrace()[0]['line']; $fixed = str_replace('%fileLine%', __FILE__ . ':' . $line, $disk); - $this->assertSame($fixed, $dumper->outputter()->toString()); + $this->assertSame($fixed, $dumper->toString()); } // Note: Console dumper can't be tested here } diff --git a/Chevere/Components/VarDump/Wrappers/ConsoleWrapper.php b/Chevere/Components/VarDump/Wrappers/ConsoleWrapper.php index e8bd06be4..9da2a6f1c 100644 --- a/Chevere/Components/VarDump/Wrappers/ConsoleWrapper.php +++ b/Chevere/Components/VarDump/Wrappers/ConsoleWrapper.php @@ -13,12 +13,17 @@ namespace Chevere\Components\VarDump\Wrappers; +use Chevere\Components\Type\Interfaces\TypeInterface; use InvalidArgumentException; -use Chevere\Components\VarDump\Interfaces\PalleteInterface; +use Chevere\Components\VarDump\Interfaces\VarDumpInterface; +use Chevere\Components\VarDump\Interfaces\WrapperInterface; +use Chevere\Components\VarDump\Wrappers\Traits\AssertKeyTrait; use JakubOnderka\PhpConsoleColor\ConsoleColor; -final class ConsoleWrapper extends AbstractWrapper +final class ConsoleWrapper implements WrapperInterface { + use AssertKeyTrait; + private ConsoleColor $consoleColor; private $color; @@ -26,13 +31,33 @@ final class ConsoleWrapper extends AbstractWrapper public function __construct(string $key) { $this->key = $key; - $this->pallete = PalleteInterface::CONSOLE; $this->assertKey(); $this->consoleColor = new ConsoleColor(); - $this->color = $this->pallete[$this->key]; + $this->color = $this->pallete()[$this->key]; $this->assertColor(); } + public function pallete(): array + { + return [ + TypeInterface::STRING => 'color_11', + TypeInterface::FLOAT => 'color_11', + TypeInterface::INTEGER => 'color_11', + TypeInterface::BOOLEAN => 'color_163', // purple + TypeInterface::NULL => 'color_245', // grey + TypeInterface::OBJECT => 'color_39', + TypeInterface::ARRAY => 'color_41', // green + TypeInterface::RESOURCE => 'color_147', // blue + VarDumpInterface::_FILE => 'default', + VarDumpInterface::_CLASS => 'color_147', // blue + VarDumpInterface::_OPERATOR => 'color_245', // grey + VarDumpInterface::_FUNCTION => 'color_39', + VarDumpInterface::_PRIVACY => 'color_133', + VarDumpInterface::_VARIABLE => 'color_208', + VarDumpInterface::_EMPHASIS => ['color_245', 'italic'] + ]; + } + public function wrap(string $dump): string { return $this->consoleColor diff --git a/Chevere/Components/VarDump/Wrappers/HtmlWrapper.php b/Chevere/Components/VarDump/Wrappers/HtmlWrapper.php index 5aca48260..789105eb7 100644 --- a/Chevere/Components/VarDump/Wrappers/HtmlWrapper.php +++ b/Chevere/Components/VarDump/Wrappers/HtmlWrapper.php @@ -13,18 +13,44 @@ namespace Chevere\Components\VarDump\Wrappers; +use Chevere\Components\Type\Interfaces\TypeInterface; use Chevere\Components\VarDump\Interfaces\PalleteInterface; +use Chevere\Components\VarDump\Interfaces\VarDumpInterface; +use Chevere\Components\VarDump\Interfaces\WrapperInterface; +use Chevere\Components\VarDump\Wrappers\Traits\AssertKeyTrait; -final class HtmlWrapper extends AbstractWrapper +final class HtmlWrapper implements WrapperInterface { + use AssertKeyTrait; + private string $color; public function __construct(string $key) { $this->key = $key; - $this->pallete = PalleteInterface::HTML; $this->assertKey(); - $this->color = $this->pallete[$this->key]; + $this->color = $this->pallete()[$this->key]; + } + + public function pallete(): array + { + return [ + TypeInterface::STRING => '#e67e22', // orange + TypeInterface::FLOAT => '#f1c40f', // yellow + TypeInterface::INTEGER => '#f1c40f', // yellow + TypeInterface::BOOLEAN => '#9b59b6', // purple + TypeInterface::NULL => '#7f8c8d', // grey + TypeInterface::OBJECT => '#e74c3c', // red + TypeInterface::ARRAY => '#2ecc71', // green + TypeInterface::RESOURCE => '#3498db', // blue + VarDumpInterface::_FILE => 'inherith', + VarDumpInterface::_CLASS => '#3498db', // blue + VarDumpInterface::_OPERATOR => '#7f8c8d', // grey + VarDumpInterface::_FUNCTION => '#9b59b6', // purple + VarDumpInterface::_PRIVACY => '#9b59b6', // purple + VarDumpInterface::_VARIABLE => '#e67e22', // orange + VarDumpInterface::_EMPHASIS => '#7f8c8d', + ]; } public function wrap(string $dump): string diff --git a/Chevere/Components/VarDump/Wrappers/AbstractWrapper.php b/Chevere/Components/VarDump/Wrappers/Traits/AssertKeyTrait.php similarity index 72% rename from Chevere/Components/VarDump/Wrappers/AbstractWrapper.php rename to Chevere/Components/VarDump/Wrappers/Traits/AssertKeyTrait.php index e8563b6bc..f78748c5f 100644 --- a/Chevere/Components/VarDump/Wrappers/AbstractWrapper.php +++ b/Chevere/Components/VarDump/Wrappers/Traits/AssertKeyTrait.php @@ -11,17 +11,14 @@ declare(strict_types=1); -namespace Chevere\Components\VarDump\Wrappers; +namespace Chevere\Components\VarDump\Wrappers\Traits; use Chevere\Components\Message\Message; -use Chevere\Components\VarDump\Interfaces\WrapperInterface; use InvalidArgumentException; -abstract class AbstractWrapper implements WrapperInterface +trait AssertKeyTrait { - protected string $key; - - protected array $pallete; + private string $key; protected function assertKey(): void { @@ -35,8 +32,5 @@ protected function assertKey(): void } } - public function pallete(): array - { - return $this->pallete; - } + abstract public function pallete(): array; } diff --git a/resources/functions/dump.php b/resources/functions/dump.php index bfcdde7c7..83268d964 100644 --- a/resources/functions/dump.php +++ b/resources/functions/dump.php @@ -27,23 +27,27 @@ function varInfo(...$vars) if (!function_exists('xdump')) { /** - * Dumps information about one or more variables to the screen. + * Dumps information about one or more variables to the runtime screen. */ function xdump(...$vars) { (new Dumper(...$vars)) - ->toScreen(); + ->toScreen( + screens()->runtime() + ); } } if (!function_exists('xdd')) { /** - * Dumps information about one or more variables to the screen and die(). + * Dumps information about one or more variables to the runtime screen and die(). */ function xdd(...$vars) { (new Dumper(...$vars)) - ->toScreen(); + ->toScreen( + screens()->runtime() + ); die(0); } }