Created
August 18, 2021 11:22
-
-
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.
This file contains 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 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