mirror of
https://github.com/laravel/valet.git
synced 2026-02-04 08:10:07 +01:00
79 lines
2.1 KiB
PHP
79 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Valet;
|
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class CommandLine
|
|
{
|
|
/**
|
|
* Simple global function to run commands quietly.
|
|
*/
|
|
public function quietly(string $command): void
|
|
{
|
|
$this->runCommand($command.' > /dev/null 2>&1');
|
|
}
|
|
|
|
/**
|
|
* Simple global function to run commands.
|
|
*/
|
|
public function quietlyAsUser(string $command): void
|
|
{
|
|
$this->quietly('sudo -u "'.user().'" '.$command.' > /dev/null 2>&1');
|
|
}
|
|
|
|
/**
|
|
* Pass the command to the command line and display the output.
|
|
*/
|
|
public function passthru(string $command): void
|
|
{
|
|
passthru($command);
|
|
}
|
|
|
|
/**
|
|
* Run the given command as the non-root user.
|
|
*/
|
|
public function run(string $command, ?callable $onError = null): string
|
|
{
|
|
return $this->runCommand($command, $onError);
|
|
}
|
|
|
|
/**
|
|
* Run the given command.
|
|
*/
|
|
public function runAsUser(string $command, ?callable $onError = null): string
|
|
{
|
|
return $this->runCommand('sudo -u "'.user().'" '.$command, $onError);
|
|
}
|
|
|
|
/**
|
|
* Run the given command.
|
|
*/
|
|
public function runCommand(string $command, ?callable $onError = null): string
|
|
{
|
|
$onError = $onError ?: function () {
|
|
};
|
|
|
|
// Symfony's 4.x Process component has deprecated passing a command string
|
|
// to the constructor, but older versions (which Valet's Composer
|
|
// constraints allow) don't have the fromShellCommandLine method.
|
|
// For more information, see: https://github.com/laravel/valet/pull/761
|
|
if (method_exists(Process::class, 'fromShellCommandline')) {
|
|
$process = Process::fromShellCommandline($command);
|
|
} else {
|
|
$process = new Process($command);
|
|
}
|
|
|
|
$processOutput = '';
|
|
$process->setTimeout(null)->run(function ($type, $line) use (&$processOutput) {
|
|
$processOutput .= $line;
|
|
});
|
|
|
|
if ($process->getExitCode() > 0) {
|
|
$onError($process->getExitCode(), $processOutput);
|
|
}
|
|
|
|
return $processOutput;
|
|
}
|
|
}
|