2019-12-03 12:41:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Utilities;
|
|
|
|
|
|
2020-05-21 22:15:09 +00:00
|
|
|
use Illuminate\Support\ProcessUtils;
|
2020-03-19 14:18:19 +00:00
|
|
|
use Symfony\Component\Process\PhpExecutableFinder;
|
2019-12-03 12:41:56 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
|
|
|
|
|
|
class Console
|
|
|
|
|
{
|
2020-03-19 14:18:19 +00:00
|
|
|
public static function run($string, $all_output = false, $timeout = 0)
|
2019-12-03 12:41:56 +00:00
|
|
|
{
|
2020-03-19 14:18:19 +00:00
|
|
|
$command = static::formatCommandString($string);
|
|
|
|
|
|
2020-05-19 23:20:48 +00:00
|
|
|
logger('Console command:: ' . $command);
|
|
|
|
|
|
2020-03-16 16:35:58 +00:00
|
|
|
$process = Process::fromShellCommandline($command, base_path());
|
2020-02-05 07:24:18 +00:00
|
|
|
$process->setTimeout($timeout);
|
2019-12-03 12:41:56 +00:00
|
|
|
|
|
|
|
|
$process->run();
|
|
|
|
|
|
|
|
|
|
if ($process->isSuccessful()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 14:34:24 +00:00
|
|
|
$output = $all_output ? $process->getOutput() : $process->getErrorOutput();
|
|
|
|
|
|
2020-05-19 23:20:48 +00:00
|
|
|
logger('Console output:: ' . $output);
|
2020-03-19 14:34:24 +00:00
|
|
|
|
|
|
|
|
return $output;
|
2019-12-03 12:41:56 +00:00
|
|
|
}
|
2020-03-19 14:18:19 +00:00
|
|
|
|
|
|
|
|
public static function getPhpBinary()
|
|
|
|
|
{
|
2020-05-21 22:15:09 +00:00
|
|
|
$bin = ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));
|
2020-05-09 13:12:57 +00:00
|
|
|
|
|
|
|
|
return !empty($bin) ? $bin : 'php';
|
2020-03-19 14:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getArtisanBinary()
|
|
|
|
|
{
|
2020-05-21 22:15:09 +00:00
|
|
|
return defined('ARTISAN_BINARY') ? ProcessUtils::escapeArgument(ARTISAN_BINARY) : 'artisan';
|
2020-03-19 14:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function formatCommandString($string)
|
|
|
|
|
{
|
|
|
|
|
return sprintf('%s %s %s', static::getPhpBinary(), static::getArtisanBinary(), $string);
|
|
|
|
|
}
|
2019-12-03 12:41:56 +00:00
|
|
|
}
|