Skip to content

Commit ee2d6a1

Browse files
committed
Expand CLI detection
1 parent 6da0e5b commit ee2d6a1

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

system/Common.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -740,15 +740,36 @@ function helper($filenames)
740740
if (! function_exists('is_cli'))
741741
{
742742
/**
743-
* Is CLI?
744-
*
745-
* Test to see if a request was made from the command line.
743+
* Check if PHP was invoked from the command line.
746744
*
747745
* @return boolean
746+
*
747+
* @codeCoverageIgnore Cannot be tested fully as PHPUnit always run in CLI
748748
*/
749749
function is_cli(): bool
750750
{
751-
return (PHP_SAPI === 'cli' || defined('STDIN'));
751+
if (PHP_SAPI === 'cli')
752+
{
753+
return true;
754+
}
755+
756+
if (defined('STDIN'))
757+
{
758+
return true;
759+
}
760+
761+
if (stristr(PHP_SAPI, 'cgi') && getenv('TERM'))
762+
{
763+
return true;
764+
}
765+
766+
if (! isset($_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']))
767+
{
768+
return true;
769+
}
770+
771+
// if source of request is from CLI, the `$_SERVER` array will not populate this key
772+
return ! isset($_SERVER['REQUEST_METHOD']);
752773
}
753774
}
754775

@@ -1278,14 +1299,16 @@ function view_cell(string $library, $params = null, int $ttl = 0, string $cacheN
12781299
*
12791300
* @see https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/helpers.php
12801301
*/
1281-
// @codeCoverageIgnoreStart
12821302
if (! function_exists('class_basename'))
12831303
{
12841304
/**
12851305
* Get the class "basename" of the given object / class.
12861306
*
1287-
* @param string|object $class
1307+
* @param string|object $class
1308+
*
12881309
* @return string
1310+
*
1311+
* @codeCoverageIgnore
12891312
*/
12901313
function class_basename($class)
12911314
{
@@ -1300,8 +1323,11 @@ function class_basename($class)
13001323
/**
13011324
* Returns all traits used by a class, its parent classes and trait of their traits.
13021325
*
1303-
* @param object|string $class
1326+
* @param object|string $class
1327+
*
13041328
* @return array
1329+
*
1330+
* @codeCoverageIgnore
13051331
*/
13061332
function class_uses_recursive($class)
13071333
{
@@ -1327,8 +1353,11 @@ function class_uses_recursive($class)
13271353
/**
13281354
* Returns all traits used by a trait and its traits.
13291355
*
1330-
* @param string $trait
1356+
* @param string $trait
1357+
*
13311358
* @return array
1359+
*
1360+
* @codeCoverageIgnore
13321361
*/
13331362
function trait_uses_recursive($trait)
13341363
{
@@ -1342,4 +1371,3 @@ function trait_uses_recursive($trait)
13421371
return $traits;
13431372
}
13441373
}
1345-
// @codeCoverageIgnoreEnd

tests/system/CommonFunctionsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,4 +507,10 @@ public function testHelperLoadsOnce()
507507
$this->assertFalse($exception);
508508
Services::reset();
509509
}
510+
511+
public function testIsCli()
512+
{
513+
$this->assertIsBool(is_cli());
514+
$this->assertTrue(is_cli());
515+
}
510516
}

0 commit comments

Comments
 (0)