CaiWuXiTong/app/Utilities/Console.php
WuYongJie 9595267ee7
Some checks failed
Tests / PHP 8.1 (push) Has been cancelled
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
chore: 导入 Akaunting 初始源码与团队协作规则
2026-07-20 18:20:25 +08:00

57 lines
1.3 KiB
PHP

<?php
namespace App\Utilities;
use Illuminate\Console\Application;
use Illuminate\Support\Str;
use Symfony\Component\Process\Process;
use Throwable;
class Console
{
public static function run($string, $timeout = 0)
{
$command = sprintf('%s %s %s', escapeshellarg(PHP_BINARY), Application::artisanBinary(), $string);
logger('Console command:: ' . $command);
try {
$process = Process::fromShellCommandline($command, base_path());
$process->setTimeout($timeout);
$process->mustRun();
$output = $process->getOutput();
if (static::isValidOutput($output)) {
return true;
}
} catch (Throwable $e) {
$output = $e->getMessage();
}
logger('Console output:: ' . $output);
return static::formatOutput($output);
}
public static function formatOutput($output)
{
$output = nl2br($output);
$output = str_replace(['"', "'"], '', $output);
$output = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $output);
return $output;
}
public static function isValidOutput($output)
{
$errors = [
'Content-Type: application/json',
'CSRF token mismatch',
];
return !Str::contains($output, $errors);
}
}