diff --git a/src/Actions/RenderConsoleCommand.php b/src/Actions/RenderConsoleCommand.php index fe25354..1603b37 100644 --- a/src/Actions/RenderConsoleCommand.php +++ b/src/Actions/RenderConsoleCommand.php @@ -6,11 +6,14 @@ use ReflectionParameter; use Tempest\Console\ConsoleCommand; +use Tempest\Console\ConsoleOutput; use Tempest\Console\ConsoleStyle; final readonly class RenderConsoleCommand { - public function __invoke(ConsoleCommand $consoleCommand): string + public function __construct(private ConsoleOutput $output) {} + + public function __invoke(ConsoleCommand $consoleCommand): void { $parts = [ConsoleStyle::FG_DARK_BLUE($consoleCommand->getName())]; @@ -22,7 +25,7 @@ public function __invoke(ConsoleCommand $consoleCommand): string $parts[] = "- {$consoleCommand->getDescription()}"; } - return implode(' ', $parts); + $this->output->writeln(' ' . implode(' ', $parts)); } private function renderParameter(ReflectionParameter $parameter): string diff --git a/src/Actions/RenderConsoleCommandOverview.php b/src/Actions/RenderConsoleCommandOverview.php index 0ea217f..4805455 100644 --- a/src/Actions/RenderConsoleCommandOverview.php +++ b/src/Actions/RenderConsoleCommandOverview.php @@ -6,27 +6,26 @@ use Tempest\AppConfig; use Tempest\Console\ConsoleConfig; -use Tempest\Console\ConsoleStyle; +use Tempest\Console\ConsoleOutput; +use Tempest\Console\ConsoleOutputType; final readonly class RenderConsoleCommandOverview { public function __construct( + private ConsoleOutput $output, private AppConfig $appConfig, private ConsoleConfig $consoleConfig, ) { } - public function __invoke(): string + public function __invoke(): void { - $lines = [ - ConsoleStyle::BOLD(ConsoleStyle::BG_DARK_BLUE(" {$this->consoleConfig->name} ")), - ]; - - if ($this->appConfig->discoveryCache) { - $lines[] = ConsoleStyle::BG_RED(' Discovery cache is enabled! '); - } - - $lines[] = ''; + $this->output + ->writeln($this->consoleConfig->name, ConsoleOutputType::H1) + ->when( + expression: $this->appConfig->discoveryCache, + callback: fn (ConsoleOutput $output) => $output->error('Discovery cache is enabled!') + ); /** @var \Tempest\Console\ConsoleCommand[][] $commands */ $commands = []; @@ -42,16 +41,13 @@ public function __invoke(): string ksort($commands); foreach ($commands as $group => $commandsForGroup) { - $lines[] = ConsoleStyle::BOLD(ConsoleStyle::BG_BLUE(' ' . ucfirst($group) . ' ')); + $this->output + ->writeln() + ->writeln(ucfirst($group), ConsoleOutputType::H2); foreach ($commandsForGroup as $consoleCommand) { - $renderedConsoleCommand = (new RenderConsoleCommand())($consoleCommand); - $lines[] = " {$renderedConsoleCommand}"; + (new RenderConsoleCommand($this->output))($consoleCommand); } - - $lines[] = ''; } - - return implode(PHP_EOL, $lines); } } diff --git a/src/ConsoleApplication.php b/src/ConsoleApplication.php index af5e54c..fdf4d41 100644 --- a/src/ConsoleApplication.php +++ b/src/ConsoleApplication.php @@ -57,10 +57,8 @@ public function run(): void try { $commandName = $this->args[1] ?? null; - $output = $this->container->get(ConsoleOutput::class); - if (! $commandName) { - $output->writeln($this->container->get(RenderConsoleCommandOverview::class)()); + $this->container->get(RenderConsoleCommandOverview::class)(); return; } diff --git a/src/ConsoleOutput.php b/src/ConsoleOutput.php index 08f92e6..8ac7084 100644 --- a/src/ConsoleOutput.php +++ b/src/ConsoleOutput.php @@ -6,13 +6,17 @@ interface ConsoleOutput { - public function write(string $line): void; + public function delimiter(string $delimiter): ConsoleOutput; - public function writeln(string $line): void; + public function write(string $line, ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput; - public function info(string $line): void; + public function writeln(string $line = '', ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput; - public function error(string $line): void; + public function info(string $line): ConsoleOutput; - public function success(string $line): void; + public function error(string $line): ConsoleOutput; + + public function success(string $line): ConsoleOutput; + + public function when(mixed $expression, callable $callback): ConsoleOutput; } diff --git a/src/ConsoleOutputType.php b/src/ConsoleOutputType.php new file mode 100644 index 0000000..d0a0560 --- /dev/null +++ b/src/ConsoleOutputType.php @@ -0,0 +1,52 @@ + ConsoleStyle::FG_DARK_BLUE($input), + self::ERROR => ConsoleStyle::BOLD(ConsoleStyle::FG_WHITE(ConsoleStyle::BG_RED(ConsoleStyle::FG_WHITE($this->error($input))))), + self::SUCCESS => ConsoleStyle::BOLD(ConsoleStyle::FG_DARK_GREEN($input)), + self::H1 => ConsoleStyle::BOLD(ConsoleStyle::FG_WHITE(ConsoleStyle::BG_DARK_BLUE($this->header($input)))), + self::H2 => ConsoleStyle::BG_BLUE($this->header($input)), + self::COMMENT => ConsoleStyle::FG_GRAY($this->comment($input)), + default => $input, + }; + } + + private function header(string $input): string + { + return " {$input} "; + } + + private function error(string $input): string + { + return " {$input} "; + } + + private function comment(string $input): string + { + return implode( + PHP_EOL, + [ + '/*', + ...array_map( + fn (string $line) => "* {$line}", + explode(PHP_EOL, $input), + ), + '*/' + ], + ); + } +} \ No newline at end of file diff --git a/src/GenericConsole.php b/src/GenericConsole.php index 3c97789..09d705f 100644 --- a/src/GenericConsole.php +++ b/src/GenericConsole.php @@ -4,14 +4,25 @@ namespace Tempest\Console; -readonly class GenericConsole implements Console +final class GenericConsole implements Console { + private string $delimiter = PHP_EOL; + public function __construct( - private ConsoleInput $input, - private ConsoleOutput $output, + private readonly ConsoleInput $input, + private readonly ConsoleOutput $output, ) { } + public function delimiter(string $delimiter): ConsoleOutput + { + $clone = clone $this; + + $this->delimiter = $delimiter; + + return $clone; + } + public function readln(): string { return $this->input->readln(); @@ -27,28 +38,33 @@ public function confirm(string $question, bool $default = false): bool return $this->input->confirm($question, $default); } - public function write(string $line): void + public function write(string $line, ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput + { + return $this->output->write($line, $type); + } + + public function writeln(string $line = '', ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput { - $this->output->write($line); + return $this->output->writeln($line, $type); } - public function writeln(string $line): void + public function info(string $line): ConsoleOutput { - $this->output->writeln($line); + return $this->output->info($line); } - public function info(string $line): void + public function error(string $line): ConsoleOutput { - $this->output->info($line); + return $this->output->error($line); } - public function error(string $line): void + public function success(string $line): ConsoleOutput { - $this->output->error($line); + return $this->output->success($line); } - public function success(string $line): void + public function when(mixed $expression, callable $callback): ConsoleOutput { - $this->output->success($line); + return $this->output->when($expression, $callback); } } diff --git a/src/GenericConsoleOutput.php b/src/GenericConsoleOutput.php index 57f0784..e317d0a 100644 --- a/src/GenericConsoleOutput.php +++ b/src/GenericConsoleOutput.php @@ -4,7 +4,72 @@ namespace Tempest\Console; -final readonly class GenericConsoleOutput implements ConsoleOutput +final class GenericConsoleOutput implements ConsoleOutput { - use HandlesConsoleOutput; + public string $delimiter = PHP_EOL; + + public function delimiter(string $delimiter): ConsoleOutput + { + $clone = clone $this; + + $this->delimiter = $delimiter; + + return $clone; + } + + public function write(string $line, ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput + { + $this->writeToStdOut($type->style($line)); + + return $this; + } + + public function writeln(string $line = '', ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput + { + $this->writeToStdOut($type->style($line) . $this->delimiter); + + return $this; + } + + public function info(string $line): ConsoleOutput + { + $this->writeln($line, ConsoleOutputType::INFO); + + return $this; + } + + public function error(string $line): ConsoleOutput + { + $this->writeln($line, ConsoleOutputType::ERROR); + + return $this; + } + + public function success(string $line): ConsoleOutput + { + $this->writeln($line, ConsoleOutputType::SUCCESS); + + return $this; + } + + public function when(mixed $expression, callable $callback): ConsoleOutput + { + if ($expression) { + $callback($this); + } + + return $this; + } + + private function writeToStdOut(string $content): void + { + $stdout = fopen('php://stdout', 'w'); + + fwrite( + $stdout, + ConsoleStyle::RESET($content), + ); + + fclose($stdout); + } } diff --git a/src/HandlesConsoleOutput.php b/src/HandlesConsoleOutput.php deleted file mode 100644 index 7ad678a..0000000 --- a/src/HandlesConsoleOutput.php +++ /dev/null @@ -1,46 +0,0 @@ -write($line . PHP_EOL); - } - - public function info(string $line): void - { - $this->writeln( - ConsoleStyle::FG_DARK_BLUE($line) - ); - } - - public function error(string $line): void - { - $this->writeln( - ConsoleStyle::BOLD(ConsoleStyle::FG_RED($line)), - ); - } - - public function success(string $line): void - { - $this->writeln( - ConsoleStyle::BOLD(ConsoleStyle::FG_GREEN($line)) - ); - } -} diff --git a/src/NullConsoleOutput.php b/src/NullConsoleOutput.php index 2990fa8..7c885df 100644 --- a/src/NullConsoleOutput.php +++ b/src/NullConsoleOutput.php @@ -4,30 +4,46 @@ namespace Tempest\Console; -final readonly class NullConsoleOutput implements ConsoleOutput +final class NullConsoleOutput implements ConsoleOutput { - public function write(string $line): void + public string $delimiter = PHP_EOL; + + public function delimiter(string $delimiter): ConsoleOutput + { + $clone = clone $this; + + $this->delimiter = $delimiter; + + return $clone; + } + + public function write(string $line, ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput + { + return $this; + } + + public function writeln(string $line = '', ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput { - return; + return $this; } - public function writeln(string $line): void + public function info(string $line): ConsoleOutput { - return; + return $this; } - public function info(string $line): void + public function error(string $line): ConsoleOutput { - return; + return $this; } - public function error(string $line): void + public function success(string $line): ConsoleOutput { - return; + return $this; } - public function success(string $line): void + public function when(mixed $expression, callable $callback): ConsoleOutput { - return; + return $this; } } diff --git a/src/Testing/Console/TestConsoleOutput.php b/src/Testing/Console/TestConsoleOutput.php index ac28e73..cee545e 100644 --- a/src/Testing/Console/TestConsoleOutput.php +++ b/src/Testing/Console/TestConsoleOutput.php @@ -5,6 +5,7 @@ namespace Tempest\Console\Testing\Console; use Tempest\Console\ConsoleOutput; +use Tempest\Console\ConsoleOutputType; use Tempest\Console\ConsoleStyle; final class TestConsoleOutput implements ConsoleOutput @@ -13,30 +14,40 @@ final class TestConsoleOutput implements ConsoleOutput private array $errorLines = []; private array $infoLines = []; private array $successLines = []; + public string $delimiter = PHP_EOL; - public function write(string $line): void + public function delimiter(string $delimiter): ConsoleOutput + { + $clone = clone $this; + + $this->delimiter = $delimiter; + + return $clone; + } + + public function write(string $line, ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput { $this->lines[] = $line; } - public function writeln(string $line): void + public function writeln(string $line = '', ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput { $this->lines[] = $line; } - public function info(string $line): void + public function info(string $line): ConsoleOutput { $this->writeln($line); $this->infoLines[] = $line; } - public function error(string $line): void + public function error(string $line): ConsoleOutput { $this->writeln($line); $this->errorLines[] = $line; } - public function success(string $line): void + public function success(string $line): ConsoleOutput { $this->writeln($line); $this->successLines[] = $line; @@ -62,12 +73,12 @@ public function getLinesWithoutFormatting(): array public function getTextWithFormatting(): string { - return implode(PHP_EOL, $this->getLinesWithFormatting()); + return implode($this->delimiter, $this->getLinesWithFormatting()); } public function getTextWithoutFormatting(): string { - return implode(PHP_EOL, $this->getLinesWithoutFormatting()); + return implode($this->delimiter, $this->getLinesWithoutFormatting()); } public function getInfoLines(): array @@ -84,4 +95,11 @@ public function getSuccessLines(): array { return $this->successLines; } + + public function when(mixed $expression, callable $callback): ConsoleOutput + { + if ($expression) { + return $callback($this); + } + } }