Last active
July 2, 2024 12:41
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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