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 ffd535c commit f94f004
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 60 deletions.
26 changes: 0 additions & 26 deletions Chevere/Components/Screen/ConsoleScreen.php

This file was deleted.

14 changes: 14 additions & 0 deletions Chevere/Components/Screen/Container.php
Expand Up @@ -29,6 +29,20 @@ public function __construct(ScreenInterface $runtime)
];
}

public function withDebugScreen(ScreenInterface $screen)
{
$new = clone $this;

return $new->withAddedScreen(static::DEBUG, $screen);
}

public function withConsoleScreen(ScreenInterface $screen)
{
$new = clone $this;

return $new->withAddedScreen(static::CONSOLE, $screen);
}

public function withAddedScreen(string $name, ScreenInterface $screen): ContainerInterface
{
$new = clone $this;
Expand Down
32 changes: 32 additions & 0 deletions Chevere/Components/Screen/Formatters/ConsoleFormatter.php
@@ -0,0 +1,32 @@
<?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\Screen\Formatters;

use Chevere\Components\Screen\Interfaces\FormatterInterface;
use JakubOnderka\PhpConsoleColor\ConsoleColor;

final class ConsoleFormatter implements FormatterInterface
{
private string $char;

public function __construct()
{
$this->char = (new ConsoleColor)->apply('reverse', '%');
}

public function wrap(string $display): string
{
return $this->char . $display . $this->char;
}
}
Expand Up @@ -11,11 +11,13 @@

declare(strict_types=1);

namespace Chevere\Components\Screen;
namespace Chevere\Components\Screen\Formatters;

final class DebugScreen extends AbstractScreen
use Chevere\Components\Screen\Interfaces\FormatterInterface;

final class DebugFormatter implements FormatterInterface
{
protected function wrap(string $display): string
public function wrap(string $display): string
{
return '🐞 ▶' . $display . '🔚 ';
}
Expand Down
Expand Up @@ -11,11 +11,13 @@

declare(strict_types=1);

namespace Chevere\Components\Screen;
namespace Chevere\Components\Screen\Formatters;

final class RuntimeScreen extends AbstractScreen
use Chevere\Components\Screen\Interfaces\FormatterInterface;

final class RuntimeFormatter implements FormatterInterface
{
protected function wrap(string $display): string
public function wrap(string $display): string
{
return $display;
}
Expand Down
24 changes: 24 additions & 0 deletions Chevere/Components/Screen/Formatters/SilentFormatter.php
@@ -0,0 +1,24 @@
<?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\Screen\Formatters;

use Chevere\Components\Screen\Interfaces\FormatterInterface;

final class SilentFormatter implements FormatterInterface
{
public function wrap(string $display): string
{
return '';
}
}
4 changes: 4 additions & 0 deletions Chevere/Components/Screen/Interfaces/ContainerInterface.php
Expand Up @@ -21,6 +21,10 @@ interface ContainerInterface

public function __construct(ScreenInterface $runtime);

public function withDebugScreen(ScreenInterface $screen);

public function withConsoleScreen(ScreenInterface $screen);

public function withAddedScreen(string $name, ScreenInterface $screen): ContainerInterface;

public function has(string $name): bool;
Expand Down
4 changes: 1 addition & 3 deletions Chevere/Components/Screen/Interfaces/FormatterInterface.php
Expand Up @@ -15,7 +15,5 @@

interface FormatterInterface
{
public function before(string $display): string;

public function after(string $display): string;
public function wrap(string $display): string;
}
2 changes: 2 additions & 0 deletions Chevere/Components/Screen/Interfaces/ScreenInterface.php
Expand Up @@ -19,6 +19,8 @@ interface ScreenInterface
{
public function traceability(): bool;

public function formatter(): FormatterInterface;

public function trace(): array;

/**
Expand Down
Expand Up @@ -13,55 +13,64 @@

namespace Chevere\Components\Screen;

use Chevere\Components\Screen\Interfaces\FormatterInterface;
use Chevere\Components\Screen\Interfaces\ScreenInterface;
use Psr\Http\Message\StreamInterface;
use function GuzzleHttp\Psr7\stream_for;

abstract class AbstractScreen implements ScreenInterface
final class Screen implements ScreenInterface
{
private bool $traceability;

private FormatterInterface $formatter;

private array $trace = [];

/** @var StreamInterface[] */
private array $queue = [];

public function __construct(bool $traceability)
public function __construct(bool $traceability, FormatterInterface $formatter)
{
$this->traceability = $traceability;
$this->formatter = $formatter;
}

public function traceability(): bool
{
return $this->traceability;
}

public function formatter(): FormatterInterface
{
return $this->formatter;
}

public function trace(): array
{
return $this->trace;
}

final public function attach(string $display): ScreenInterface
public function attach(string $display): ScreenInterface
{
$this->handleTrace();
$this->queue[] = stream_for($this->wrap($display));
$this->queue[] = stream_for($this->formatter->wrap($display));

return $this;
}

final public function attachNl(string $display): ScreenInterface
public function attachNl(string $display): ScreenInterface
{
$this->handleTrace();

return $this->attach($display . "\n");
}

final public function queue(): array
public function queue(): array
{
return $this->queue;
}

final public function show(): ScreenInterface
public function show(): ScreenInterface
{
$this->handleTrace();
// TODO YIELD HERE
Expand Down Expand Up @@ -93,6 +102,4 @@ private function handleTrace(): void
'arguments' => $caller['args'],
];
}

abstract protected function wrap(string $display): string;
}
5 changes: 5 additions & 0 deletions Chevere/Components/Screen/ScreenContainer.php
Expand Up @@ -44,6 +44,11 @@ public function console(): ScreenInterface
return $this->container->get($this->container::CONSOLE);
}

public function get(string $name): ScreenInterface
{
return $this->container->get($name);
}

public function getAll(): array
{
return $this->container->getAll();
Expand Down
12 changes: 12 additions & 0 deletions Chevere/Components/Screen/SilentScreen.php
Expand Up @@ -13,18 +13,30 @@

namespace Chevere\Components\Screen;

use Chevere\Components\Screen\Formatters\SilentFormatter;
use Chevere\Components\Screen\Interfaces\FormatterInterface;
use Chevere\Components\Screen\Interfaces\ScreenInterface;

/**
* A completely silent screen.
*/
final class SilentScreen implements ScreenInterface
{
public function __construct()
{
$this->formatter = new SilentFormatter;
}

public function traceability(): bool
{
return false;
}

public function formatter(): FormatterInterface
{
return $this->formatter;
}

public function trace(): array
{
return [];
Expand Down
2 changes: 1 addition & 1 deletion Chevere/Components/Screen/Tests/ScreenTest.php
Expand Up @@ -22,7 +22,7 @@ public function testConstruct(): void
$this->expectNotToPerformAssertions();
// xdump($this);
// xdd(screen()->debug()->attach('Fatal error at: ' . __FILE__));
screens()->debug()->attach('Fatal error at: ' . __FILE__)->show();
screens()->console()->attach('Fatal error at: ' . __FILE__)->show();
// xdd(screens()->debug()->trace());
// screens()->console()->attach('Fatal error at: ' . __FILE__)->show();
// screen()->runtime()->attachNl(varInfo($this))->display();
Expand Down
33 changes: 18 additions & 15 deletions phpunit.php
Expand Up @@ -16,15 +16,14 @@
use Chevere\Components\App\Instances\BootstrapInstance;
use Chevere\Components\App\Instances\ScreenContainerInstance;
use Chevere\Components\Bootstrap\Bootstrap;
use Chevere\Components\Screen\ConsoleScreen;
use Chevere\Components\Screen\Container;
use Chevere\Components\Screen\DebugScreen;
use Chevere\Components\Screen\Formatters\ConsoleFormatter;
use Chevere\Components\Screen\Formatters\DebugFormatter;
use Chevere\Components\Screen\Formatters\RuntimeFormatter;
use Chevere\Components\Screen\Interfaces\ContainerInterface;
use Chevere\Components\Screen\RuntimeScreen;
use Chevere\Components\Screen\Screen;
use Chevere\Components\Screen\ScreenContainer;
use Chevere\Components\Screen\SilentScreen;
use JakubOnderka\PhpConsoleColor\ConsoleColor;

require 'vendor/autoload.php';

Expand All @@ -37,20 +36,24 @@

new ScreenContainerInstance(
new ScreenContainer(
(new Container(new RuntimeScreen(true)))
->withAddedScreen(
ContainerInterface::DEBUG,
new DebugScreen(true),
(new Container(
new Screen(false, new RuntimeFormatter)
))
->withDebugScreen(
new Screen(false, new DebugFormatter)
)
->withConsoleScreen(
new Screen(false, new DebugFormatter)
)
->withAddedScreen(
ContainerInterface::CONSOLE,
new ConsoleScreen(true),
'rodo',
new Screen(false, new RuntimeFormatter)
)
)
);

register_shutdown_function(function () {
foreach (ScreenContainerInstance::get()->getAll() as $screen) {
xdump($screen->trace());
}
});
// register_shutdown_function(function () {
// foreach (ScreenContainerInstance::get()->getAll() as $screen) {
// xdump($screen->trace());
// }
// });

0 comments on commit f94f004

Please sign in to comment.