Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel console command timed trait. This will always output how long the execution of a Laravel command takes.
<?php
namespace App\Console\Commands\Traits;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
trait TimedTrait
{
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/**
* @var \Illuminate\Console\Command $this
*/
$startTime = microtime(true);
$callStatus = parent::execute($input, $output);
$endTime = microtime(true);
$totalTime = $endTime-$startTime;
$commandName = $this->getName() ?? get_class($this);
$this->line("Command <info>{$commandName}</info> completed in {$totalTime} seconds");
return $callStatus;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment