Vasiliy Bondarenko
Vasiliy Bondarenko

Reputation: 341

Coloured Output To Console Outside Commands in Laravel 5.4

inside Command i can output

$this->error();
$this->info();

but if i instantiate other classes inside Command - how do i make coloured output to console inside that external classes? That other classes does not extend Command class.

I have found only this solution, and i don't like it :)

<?php

use Illuminate\Console\Command;

class External
{
    /** @var Command */
    protected $command;

    public function __construct(Command $command) {
        $this->command = $command;
    }

    protected function error($msg)
    {
        $this->command->error($msg);
    }

    protected function info($msg, $v = null)
    {
        $this->command->info($msg, $v);
    }
}

Upvotes: 2

Views: 9095

Answers (3)

darighteous1
darighteous1

Reputation: 89

Your command is the entry point of your application, not the services. It should handle input and output. Your services should be isolated, do not couple them to the console.

For example, if you need to change your application to work in a different way - instead of outputting to the console, it will have to write to a database. Or, instead of running through the CLI, it should return a JSON with the aggregated messages. You will have to change each and every one of your services.

What I would do, is inject the services in the command, not the other way around. Then, let the command handle all of the output with its pretty colors.

Upvotes: 0

yog_a
yog_a

Reputation: 51

First approach is implemented in nunomaduro / collision package:

<?php

$color = new NunoMaduro\Collision\ConsoleColor;

echo($color->apply("red", "some text in red"));

Upvotes: 5

whoacowboy
whoacowboy

Reputation: 7447

Your existing approach seems pretty reasonable.

You could use this approach which is lighter.

  /*
    Black 0;30
    Blue 0;34
    Green 0;32
    Cyan 0;36
    Red 0;31
    Purple 0;35
    Brown 0;33
    Light Gray 0;37 
    Dark Gray 1;30
    Light Blue 1;34
    Light Green 1;32
    Light Cyan 1;36
    Light Red 1;31
    Light Purple 1;35
    Yellow 1;33
    White 1;37
  */

  echo "\033[31m some colored text \033[0m some white text \n";
  echo "\033[32m some colored text \033[0m some white text \n";

You also have access to the underlying SymfonyCommand so in your existing approach you could do this.

  <?php

  use Illuminate\Console\Command;
  use Symfony\Component\Console\Formatter\OutputFormatterStyle;

  class External
  {
      /** @var Command */
      protected $command;

      public function __construct(Command $command) {
          $this->command = $command;
      }

      protected function error($msg)
      {
          $this->command->error($msg);
      }

      protected function info($msg, $v = null)
      {
          $this->command->info($msg, $v);
      }
      protected function fire($msg)
      {
          // Custom colors
          $style = new OutputFormatterStyle('red', 'yellow', array('bold', 'blink'));
          $this->command->output->getFormatter()->setStyle('fire', $style);

          $this->command->output->writeln('<fire>' . msg . '</fire>');
      }
  }

Upvotes: 12

Related Questions