Skip to content

Instantly share code, notes, and snippets.

@carestad
Last active July 2, 2024 12:41
Show Gist options
  • 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/timeable trait. This will always output how long the execution of a Laravel command takes after it finishes.
<?php
namespace App\Console\Commands\Traits;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
trait Timeable
{
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
/**
* @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