Skip to content

Instantly share code, notes, and snippets.

@carestad
Created August 18, 2021 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carestad/b1b55ddb9c19a9242ccdab88f4351d50 to your computer and use it in GitHub Desktop.
Save carestad/b1b55ddb9c19a9242ccdab88f4351d50 to your computer and use it in GitHub Desktop.
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