Skip to content

Instantly share code, notes, and snippets.

@leMaur
Last active June 29, 2018 13:59
Show Gist options
  • Select an option

  • Save leMaur/9f12d2c9cace5d9a448dd6964f7a71b8 to your computer and use it in GitHub Desktop.

Select an option

Save leMaur/9f12d2c9cace5d9a448dd6964f7a71b8 to your computer and use it in GitHub Desktop.
Save `Progressable.php` in `app/Console`. Then use it in your Artisan Commands.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Console\Progressable;
/**
* .: ATTENTION :.
*
* This is just an example
* to show you how to use
* the `Progressable` trait.
*/
class ImportSomething extends Command
{
use Progressable;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:something';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->init();
/** Set progressbar and configure it */
$this->initProgressbar(20);
/** Show info about what you are going to do */
$this->echo("I'm going to import 20 records...");
/** Show info about 1st step */
$this->echo("I start importing 10 records...");
for ($i=0; $i < 10; $i++) {
$this->advanceProgressbar();
sleep(1);
}
$this->echo("Well done, 10 records are correctly imported!");
/** Show info about 2nd step */
$this->echo("I start importing 10 records...");
for ($i=0; $i < 10; $i++) {
$this->advanceProgressbar();
sleep(1);
}
$this->echo("Well done, 10 records are correctly imported!");
/** Show final info */
$this->echo("All records were imported!");
/** Stop the progressbar */
$this->clearProgressbar();
}
}
<?php
namespace App\Console;
trait Progressable
{
/**
* User may don't interact with the script.
*
* @var bool
*/
protected $canInteract;
/**
* The progress bar running...
*
* @var bool
*/
protected $inProgress;
/**
* Initialize the progress bar.
*
* @param int $total
* @param string $mode
* @return void
*/
protected function initProgressbar(int $total, string $mode = 'verbose'): void
{
if ($this->canInteract) {
$this->progressbar = $this->output->createProgressBar($total);
$this->progressbar->setFormat($mode);
}
}
/**
* Advance the progress bar value.
*
* @return void
*/
protected function advanceProgressbar(): void
{
if ($this->canInteract) {
if ($this->inProgress) {
$this->progressbar->display();
$this->inProgress = false;
}
$this->progressbar->advance();
$this->inProgress = true;
}
}
/**
* Clear the progress bar.
*
* @return void
*/
protected function clearProgressbar(): void
{
if ($this->canInteract) {
$this->progressbar->finish();
}
}
/**
* Write the message on the console.
*
* @param string $message
* @param string $style
* @param null|int|string $verbosity
* @return void
*/
protected function echo(string $message, $style = 'info', $verbosity = null): void
{
if ($this->canInteract) {
if ($this->inProgress) {
$this->progressbar->clear();
}
$this->line($message, $style, $verbosity);
$this->line('');
}
}
/**
* Initialize all the stuff.
*
* @return void
*/
protected function init(): void
{
$this->canInteract = ! $this->option('no-interaction');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment