2017 © Pedro Peláez
 

library write-to-console

Simple Trait to interact with artisan console from any PHP class

image

code16/write-to-console

Simple Trait to interact with artisan console from any PHP class

  • Tuesday, March 6, 2018
  • by code16
  • Repository
  • 2 Watchers
  • 1 Stars
  • 420 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 16 % Grown

The README.md

WriteToConsole trait

This is a simple trait to allow a third party class to output to an artisan console., (*1)

Useful when implementing heavy procedural scripts which code is shared with other part of the application, or just written in its own class for easier testability., (*2)

It simply redirects the info() , error() , comment(), table() and progressBar() methods to the command object set by calling the public setConsole() method of the trait, or just pass-through if no command object is set., (*3)

Example

Using the trait in a custom script :, (*4)


namespace App\Tools; use Code16\WriteToConsole\WriteToConsole; class DataImporter { use WriteToConsole; public function execute() { $this->info("Import started"); $progress = $this->progressBar(100); for($x=0;$x<100;$x++) { $progress->advance(); } $this->info("Import finished"); } }

Calling the script from an artisan console command :, (*5)



<?php namespace App\Console\Commands; use Illuminate\Console\Command; use App\Tools\DataImporter; class DataImportsCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'data:import'; /** * The console command description. * * @var string */ protected $description = 'Import DATA'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $importer = app(DataImporter::class); $importer->setConsole($this); $importer->execute(); } }

Output text to a logger

WriteToConsole also accepts a Psr-3 compatible logger as additionnal argument. Therefore every methods, excepts table() and progressBar(), will be redirected to the logger., (*6)

    public function handle()
    {   
        $importer = app(DataImporter::class);
        $importer->setConsole($this);

        $logger = app(\Psr\Log\LoggerInterface::class);
        $importer->setLogger($logger);

        $importer->execute();

    }

License

MIT, (*7)

(c) 2018 Code16.fr, (*8)

The Versions

06/03 2018

dev-master

9999999-dev https://code16.fr

Simple Trait to interact with artisan console from any PHP class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémi Collin

laravel console helper