Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 24, 2020
1 parent f94f004 commit 487094c
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 135 deletions.
4 changes: 2 additions & 2 deletions Chevere/Components/Screen/Interfaces/ScreenInterface.php
Expand Up @@ -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;
}
21 changes: 11 additions & 10 deletions Chevere/Components/Screen/Screen.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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 = [];

Expand All @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion Chevere/Components/Screen/SilentScreen.php
Expand Up @@ -57,7 +57,7 @@ public function queue(): array
return [];
}

public function show(): ScreenInterface
public function emit(): ScreenInterface
{
return $this;
}
Expand Down
10 changes: 5 additions & 5 deletions Chevere/Components/Screen/Tests/ScreenTest.php
Expand Up @@ -14,19 +14,19 @@
namespace Chevere\Components\Screen\Tests;

use PHPUnit\Framework\TestCase;
use function GuzzleHttp\Psr7\stream_for;

final class ScreenTest extends TestCase
{
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
Expand Down
5 changes: 3 additions & 2 deletions Chevere/Components/VarDump/Dumper.php
Expand Up @@ -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;

Expand All @@ -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();
}
}
31 changes: 15 additions & 16 deletions Chevere/Components/VarDump/Dumpers/AbstractDumper.php
Expand Up @@ -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}
Expand All @@ -79,9 +79,6 @@ final public function withVars(...$vars): DumperInterface
return $new;
}
$new->setDebugBacktrace();
$new->output = $new->outputter
->withDumper($new)
->toString();

return $new;
}
Expand All @@ -99,7 +96,9 @@ final public function vars(): array
*/
final public function toString(): string
{
return $this->output;
return $this->outputter
->withDumper($this)
->toString();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions Chevere/Components/VarDump/Dumpers/ConsoleDumper.php
Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions Chevere/Components/VarDump/Dumpers/HtmlDumper.php
Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions Chevere/Components/VarDump/Dumpers/PlainDumper.php
Expand Up @@ -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;
}
}
4 changes: 0 additions & 4 deletions Chevere/Components/VarDump/Interfaces/DumperInterface.php
Expand Up @@ -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.
*
Expand Down
59 changes: 0 additions & 59 deletions Chevere/Components/VarDump/Interfaces/PalleteInterface.php
@@ -1,59 +0,0 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* 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']
];
}
6 changes: 3 additions & 3 deletions Chevere/Components/VarDump/Tests/DumperTest.php
Expand Up @@ -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(),
];
}

Expand Down Expand Up @@ -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
}
Expand Down
33 changes: 29 additions & 4 deletions Chevere/Components/VarDump/Wrappers/ConsoleWrapper.php
Expand Up @@ -13,26 +13,51 @@

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;

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
Expand Down

0 comments on commit 487094c

Please sign in to comment.