Select Git revision
SymfonyStyleLogger.php
Laurent Lecluse authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
SymfonyStyleLogger.php 1.98 KiB
<?php
namespace Unicaen\BddAdmin\Logger;
use Symfony\Component\Console\Style\SymfonyStyle;
class SymfonyStyleLogger implements LoggerInterface
{
protected ?string $lastMessage = null;
protected bool $lastRewrite = false;
protected SymfonyStyle $symfonyStyle;
public function __construct(SymfonyStyle $symfonyStyle)
{
$this->symfonyStyle = $symfonyStyle;
}
public function print($text, $color = null, $bgColor = null)
{
$this->symfonyStyle->write($text);
}
public function println($text, $color = null, $bgColor = null)
{
$this->symfonyStyle->writeln($text);
}
public function msg($message, bool $rewrite = false)
{
if ($rewrite) {
if ($this->lastMessage) {
$m = $message . str_pad('', strlen($this->lastMessage) - strlen($message) + 2) . "\r";
} else {
$m = $message . "\r";
}
$this->print($m);
} else {
$this->println($message);
}
$this->lastMessage = $message;
$this->lastRewrite = $rewrite;
}
public function error(Throwable|string $e)
{
if ($e instanceof \Throwable) {
$e = $e->getMessage();
}
if ($this->lastRewrite) $this->println('');
$this->symfonyStyle->error($e);
}
public function begin(string $title)
{
if ($this->lastMessage) {
$title .= str_pad('', strlen($this->lastMessage) - strlen($title) + 2);
}
$this->lastRewrite = false;
$this->lastMessage = null;
$this->symfonyStyle->title($title);
}
public function end(?string $msg = null): void
{
if ($this->lastMessage && $this->lastRewrite) {
$msg .= str_pad('', strlen($this->lastMessage) - strlen($msg ?? '') + 2);
}
if (trim($msg)) {
$this->symfonyStyle->comment($msg);
} else {
$this->println('');
}
}
}