Skip to content

Instantly share code, notes, and snippets.

@mallardduck
Last active October 18, 2018 20:56
Show Gist options
  • Save mallardduck/97971ea2fc178dc5e4bd744a5b50ccb4 to your computer and use it in GitHub Desktop.
Save mallardduck/97971ea2fc178dc5e4bd744a5b50ccb4 to your computer and use it in GitHub Desktop.
A PHP trait for use in Laravel console commands to enable output prefixing via a command flag. Uses the `time()` a command starts at to add a unique identifier for distinguishing each invocation in logs.
<?php
namespace App\Console\Commands;
trait OutputPrefixTrait
{
/**
* The string used to prefix command output.
*
* @var string
*/
protected $outputPrefix;
/**
* The config flag to dictate how and when to apply the prefix.
*
* @var int|bool
*/
protected $prefixConfig = 0;
/**
* The timestamp used to ID groupped output.
*
* @var int
*/
protected $runStart;
/**
* Called from a command constructor to bootstrap Output Prefixing.
*
* @param string $prefix
* @param int|bool $config
*
* @return void
*/
private function bootOutputPrefix(string $prefix, $config = null)
{
$this->outputPrefix = $prefix;
$this->prefixConfig = $config;
// Append the flag this trait needs to the signature.
$this->signature .= " {--C|cron : Run the command in cron mode. [Prefixes output]}";
// Use the start time as a unique identifier for each command instance.
$this->runStart = time();
}
/**
* Initilizes output prefixing when a command is envoked.
*
* To be called within the handle() method on the command.
*
* @return void
*/
private function initOutputPrefix()
{
$this->prefixConfig = $this->option('cron');
}
/**
* Returns the rendered output prefix string.
*
* @return string
*/
private function getOutputPrefix()
{
return "[{$this->outputPrefix} ID:{$this->runStart}] ";
}
/**
* Returns the needed output string - with or without the prefix.
*
* @param string $string
* @return string
*/
private function renderOutputString($string)
{
if (true == $this->prefixConfig) {
return $this->getOutputPrefix() . $string;
}
return $string;
}
/**
* Write a string as standard output.
*
* Overridden by the trait to accomplish output prefixing.
*
* @param string $string
* @param null|string $style
* @param null|int|string $verbosity
* @return void
*/
public function line($string, $style = null, $verbosity = null)
{
$string = $this->renderOutputString($string);
$styled = $style ? "<$style>$string</$style>" : $string;
$this->output->writeln($styled, $this->parseVerbosity($verbosity));
}
}
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Console\Commands\OutputPrefixTrait;
class ExampleCommand extends Command
{
use OutputPrefixTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'example:command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Provides an example that uses the output prefixing trait.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
$this->bootOutputPrefix("Example Command"); // Renders as: "Example Command ID:1539725236"
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->initOutputPrefix();
// Do your command things!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment