Skip to content
Snippets Groups Projects
Select Git revision
  • 5e7648ba955898ad42fdd839bbf3d21467b4037f
  • main default
  • detached3
  • detached2
  • modernisation-gestion-donnees
  • detached
  • 1.5.0
  • 1.4.3
  • 1.4.2
  • 1.4.1
  • 1.4.0
  • 1.3.0
  • 1.2.0
  • 1.1.2
  • 1.1.1
  • 1.1.0
  • 1.0.6
  • 1.0.5
  • 1.0.4
  • 1.0.3
  • 1.0.2
  • 1.0.1
  • 1.0.0
  • 0.9.10
  • 0.9.9
  • 0.9.8
26 results

SymfonyStyleLogger.php

Blame
  • 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('');
            }
        }
    
    }