|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Qu1eeeOJ\LaravelLogger; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\App; |
| 6 | +use Qu1eeeOJ\LaravelLogger\Exceptions\StyleNotFoundException; |
| 7 | +use Qu1eeeOJ\LaravelLogger\Formatters\ConsoleMessageFormatter; |
| 8 | +use Qu1eeeOJ\LaravelLogger\Interfaces\FormatterWithStyleInterface; |
| 9 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 10 | + |
| 11 | +/** |
| 12 | + * @method void info(string $message) |
| 13 | + * @method void error(string $message) |
| 14 | + * @method void warning(string $message) |
| 15 | + * @method void debug(string $message) |
| 16 | + */ |
| 17 | +class LoggerConsoleService |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var ConsoleOutput |
| 21 | + */ |
| 22 | + private readonly ConsoleOutput $console; |
| 23 | + |
| 24 | + /** |
| 25 | + * We are casting the style of operation from laravel logger for console output |
| 26 | + * |
| 27 | + * @var array<string, string> |
| 28 | + */ |
| 29 | + private array $styles = [ |
| 30 | + 'info' => 'info', |
| 31 | + 'error' => 'error', |
| 32 | + 'warning' => 'warn', |
| 33 | + 'debug' => 'info' |
| 34 | + ]; |
| 35 | + |
| 36 | + /** |
| 37 | + * LoggerConsoleService constructor |
| 38 | + * |
| 39 | + * @param string|array|null $prefix |
| 40 | + * @param FormatterWithStyleInterface $formatter |
| 41 | + */ |
| 42 | + public function __construct( |
| 43 | + protected readonly string|array|null $prefix = null, |
| 44 | + public readonly FormatterWithStyleInterface $formatter = new ConsoleMessageFormatter() |
| 45 | + ) |
| 46 | + { |
| 47 | + $this->console = App::make(ConsoleOutput::class); |
| 48 | + |
| 49 | + // if the prefix is not null, then we put the prefix |
| 50 | + if (! is_null($this->prefix)) { |
| 51 | + $this->formatter->setPrefix($this->prefix); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Magic method |
| 57 | + * |
| 58 | + * @param string $style |
| 59 | + * @param array $arguments |
| 60 | + * |
| 61 | + * @return void |
| 62 | + * |
| 63 | + * @throws StyleNotFoundException |
| 64 | + */ |
| 65 | + public function __call(string $style, array $arguments): void |
| 66 | + { |
| 67 | + $this->writeln($style, $arguments[0]); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Writing to console output with new line |
| 72 | + * |
| 73 | + * @param string $style |
| 74 | + * @param string $message |
| 75 | + * |
| 76 | + * @return void |
| 77 | + * |
| 78 | + * @throws StyleNotFoundException |
| 79 | + */ |
| 80 | + public function writeln(string $style, string $message): void |
| 81 | + { |
| 82 | + $this->write($style, $message, true); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Writing to console output |
| 87 | + * |
| 88 | + * @param string $style |
| 89 | + * @param string $message |
| 90 | + * @param bool $newLine |
| 91 | + * |
| 92 | + * @return void |
| 93 | + * |
| 94 | + * @throws StyleNotFoundException |
| 95 | + */ |
| 96 | + public function write(string $style, string $message, bool $newLine = false): void |
| 97 | + { |
| 98 | + // Check style for console output |
| 99 | + if (! array_key_exists($style, $this->styles)) { |
| 100 | + throw new StyleNotFoundException(sprintf("Style [%s] not found in console styles", $style)); |
| 101 | + } |
| 102 | + |
| 103 | + // Setting the style to the formatter |
| 104 | + $this->formatter->setStyle($this->styles[$style]); |
| 105 | + |
| 106 | + $this->console->write($this->formatter->getFormattedMessage($message), $newLine); |
| 107 | + } |
| 108 | +} |
0 commit comments