Skip to content

Commit

Permalink
[6.x] Speed up runningInConsole method (#30922)
Browse files Browse the repository at this point in the history
* Speed up runningInConsole method

* Skip looking up the env variable on each call

* Update Application.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
GrahamCampbell and taylorotwell committed Dec 25, 2019
1 parent 225e043 commit 2bde5b7
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Illuminate/Foundation/Application.php
Expand Up @@ -131,6 +131,13 @@ class Application extends Container implements ApplicationContract, HttpKernelIn
*/
protected $environmentFile = '.env';

/**
* Indicates if the application is running in the console.
*
* @var bool|null
*/
protected $isRunningInConsole;

/**
* The application namespace.
*
Expand Down Expand Up @@ -551,11 +558,11 @@ public function detectEnvironment(Closure $callback)
*/
public function runningInConsole()
{
if (Env::get('APP_RUNNING_IN_CONSOLE') !== null) {
return Env::get('APP_RUNNING_IN_CONSOLE') === true;
if ($this->isRunningInConsole === null) {
$this->isRunningInConsole = Env::get('APP_RUNNING_IN_CONSOLE') ?? (\PHP_SAPI === 'cli' || \PHP_SAPI === 'phpdbg');
}

return php_sapi_name() === 'cli' || php_sapi_name() === 'phpdbg';
return $this->isRunningInConsole;
}

/**
Expand Down

0 comments on commit 2bde5b7

Please sign in to comment.