Fix validation runningInConsole issues (#12872)

* test

* Fix cli checks
Add helper function that works in both before Laravel is booted and without the legacy includes
This commit is contained in:
Tony Murray 2021-05-13 13:00:56 -05:00 committed by GitHub
parent 1bf9afb744
commit 5ba3df45eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 6 deletions

View File

@ -24,6 +24,7 @@
namespace LibreNMS\Util;
use App;
use Symfony\Component\HttpFoundation\HeaderBag;
class Laravel
@ -73,6 +74,18 @@ class Laravel
return function_exists('app') && ! empty(app()->isAlias('Illuminate\Foundation\Application')) && app()->isBooted();
}
/**
* Check if running in the command line.
* Safe for code without Laravel running and in Laravel console application.
* @return bool
*/
public static function isCli(): bool
{
return Laravel::isBooted()
? App::runningInConsole()
: (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']));
}
/**
* Add prefix and strip .php to make the url helper work in non-laravel php scripts
*

View File

@ -24,9 +24,9 @@
namespace LibreNMS;
use App;
use Illuminate\Support\Str;
use LibreNMS\Interfaces\ValidationGroup;
use LibreNMS\Util\Laravel;
use ReflectionClass;
class Validator
@ -74,14 +74,14 @@ class Validator
}
if ((empty($validation_groups) && $group->isDefault()) || in_array($group_name, $validation_groups)) {
if ($print_group_status && App::runningInConsole()) {
if ($print_group_status && Laravel::isCli()) {
echo "Checking $group_name:";
}
/** @var ValidationGroup $group */
$group->validate($this);
if (App::runningInConsole()) {
if (Laravel::isCli()) {
if ($print_group_status) {
$status = ValidationResult::getStatusText($this->getGroupStatus($group_name));
c_echo(" $status\n");

View File

@ -22,6 +22,7 @@ use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Util\Debug;
use LibreNMS\Util\Git;
use LibreNMS\Util\IP;
use LibreNMS\Util\Laravel;
use Symfony\Component\Process\Process;
function generate_priority_status($priority)
@ -146,7 +147,7 @@ function shorthost($hostname, $len = 12)
function print_error($text)
{
if (App::runningInConsole()) {
if (Laravel::isCli()) {
c_echo('%r' . $text . "%n\n");
} else {
echo '<div class="alert alert-danger"><i class="fa fa-fw fa-exclamation-circle" aria-hidden="true"></i> ' . $text . '</div>';
@ -155,7 +156,7 @@ function print_error($text)
function print_message($text)
{
if (App::runningInConsole()) {
if (Laravel::isCli()) {
c_echo('%g' . $text . "%n\n");
} else {
echo '<div class="alert alert-success"><i class="fa fa-fw fa-check-circle" aria-hidden="true"></i> ' . $text . '</div>';
@ -429,7 +430,7 @@ function c_echo($string, $enabled = true)
return;
}
if (App::runningInConsole()) {
if (Laravel::isCli()) {
global $console_color;
if ($console_color) {
echo $console_color->convert($string);