Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output Layer Implementation #6

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"require": {
"php": "^8.1",
"ext-readline": "*",
"filp/whoops": "^2.15",
"minicli/mini-plugin": "dev-main"
},
Expand Down
46 changes: 45 additions & 1 deletion src/Configuration/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@

namespace Minicli\Framework\Configuration;

use Minicli\Framework\Contracts\Output\EngineContract;
use Minicli\Framework\Contracts\Output\PrinterContract;
use Minicli\Framework\Contracts\Theme\ThemeContract;

final class Config
{
/**
* @param string $path
* @param class-string<ThemeContract>|null $theme
* @param class-string<PrinterContract>|null $printer
* @param class-string<EngineContract>|null $engine
* @param bool $debug
*/
public function __construct(
private readonly string $path,
private readonly ?string $theme = null,
private readonly ?string $printer = null,
private readonly ?string $engine = null,
private readonly bool $debug = false,
) {
}
Expand All @@ -28,20 +34,58 @@ public function toArray(): array
return [
'path' => $this->path,
'theme' => $this->theme,
'printer' => $this->printer,
'engine' => $this->engine,
'debug' => $this->debug,
];
}

/**
* @param array{path:string,theme:class-string<ThemeContract>|null,debug:bool|null} $data
* @param array{path:string,theme:class-string<ThemeContract>|null,printer:class-string<PrinterContract>|null,engine:class-string<EngineContract>|null,debug:bool|null} $data
* @return Config
*/
public static function make(array $data): Config
{
return new Config(
path: $data['path'],
theme: $data['theme'],
printer: $data['printer'],
engine: $data['engine'],
debug: $data['debug'] ?? false,
);
}

public function path(): string
{
return $this->path;
}

/**
* @return class-string<ThemeContract>|null
*/
public function theme(): ?string
{
return $this->theme;
}

/**
* @return class-string<PrinterContract>|null
*/
public function printer(): ?string
{
return $this->printer;
}

/**
* @return class-string<EngineContract>|null
*/
public function engine(): ?string
{
return $this->engine;
}

public function debug(): bool
{
return $this->debug;
}
}
48 changes: 48 additions & 0 deletions src/Contracts/Output/EngineContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Minicli\Framework\Contracts\Output;

use Minicli\Framework\Output\Table;

interface EngineContract
{
public function print(string $message): void;

public function lineBreak(): void;

public function table(Table $table): void;

public function ask(string $question, string $method = 'default'): string;

public function default(string $message): void;

public function alt(string $message): void;

public function error(string $message): void;

public function errorAlt(string $message): void;

public function warning(string $message): void;

public function warningAlt(string $message): void;

public function success(string $message): void;

public function successAlt(string $message): void;

public function info(string $message): void;

public function infoAlt(string $message): void;

public function bold(string $message): void;

public function dim(string $message): void;

public function italic(string $message): void;

public function underline(string $message): void;

public function invert(string $message): void;
}
48 changes: 48 additions & 0 deletions src/Input/AskInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Minicli\Framework\Input;

final class AskInput
{
/**
* @param string $prompt
* @param array<int,string> $inputHistory
*/
public function __construct(
private string $prompt = '> ',
private array $inputHistory = [],
) {
}

public static function make(string $prompt = '> ', array $inputHistory = []): AskInput
{
return new self($prompt, $inputHistory);
}

public function read(): string
{
$input = (string) readline($this->prompt);
$this->inputHistory[] = $input;

return $input;
}

public function prompt(): string
{
return $this->prompt;
}

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

public function setPrompt(string $prompt): AskInput
{
$this->prompt = $prompt;

return $this;
}
}
30 changes: 30 additions & 0 deletions src/Minicli.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
use Minicli\Framework\Commands\AbstractCommand;
use Minicli\Framework\Commands\DefaultCommand;
use Minicli\Framework\Configuration\Config;
use Minicli\Framework\Contracts\Output\EngineContract;
use Minicli\Framework\Contracts\Output\PrinterContract;
use Minicli\Framework\Contracts\Theme\ThemeContract;
use Minicli\Framework\DI\Container;
use Minicli\Framework\Exceptions\MissingParametersException;
use Minicli\Framework\Input\Input;
use Minicli\Framework\Output\Engine\DefaultEngine;
use Minicli\Framework\Output\Printer\DefaultPrinter;
use Minicli\Framework\Theme\Catalog\DefaultTheme;

final class Minicli extends Container
{
Expand All @@ -21,23 +26,30 @@ public function __construct(
) {
parent::__construct();

$this->loadEngine();
$this->loadCommands();
}

/**
* @param string $path
* @param class-string<ThemeContract>|null $theme
* @param class-string<PrinterContract>|null $printer
* @param class-string<EngineContract>|null $engine
* @param bool $debug
* @return Minicli
*/
public static function boot(
string $path,
?string $theme,
?string $printer,
?string $engine,
bool $debug = false,
): Minicli {
$config = new Config(
path: $path,
theme: $theme,
printer: $printer,
engine: $engine,
debug: $debug,
);

Expand All @@ -63,6 +75,24 @@ public function run(array $argv = []): void
$command->teardown();
}

private function loadEngine(): void
{
$this->singleton(
abstract: ThemeContract::class,
concrete: $this->config->theme() ?? DefaultTheme::class,
);

$this->singleton(
abstract: PrinterContract::class,
concrete: $this->config->printer() ?? DefaultPrinter::class,
);

$this->singleton(
abstract: EngineContract::class,
concrete: $this->config->engine() ?? DefaultEngine::class,
);
}

/**
* Loads commands into the DI container.
*/
Expand Down
25 changes: 0 additions & 25 deletions src/Output/Engine.php

This file was deleted.

Loading