Issues (85)

cli/Valet/CommandLine.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Valet;
4
5
use Symfony\Component\Process\Process;
6
7
class CommandLine
8
{
9
    /**
10
     * Simple global function to run commands.
11
     *
12
     * @param string $command
13
     *
14
     * @return void
15
     */
16
    public function quietly($command)
17
    {
18
        $this->runCommand($command.' > /dev/null 2>&1');
19
    }
20
21
    /**
22
     * Simple global function to run commands.
23
     *
24
     * @param string $command
25
     *
26
     * @return void
27
     */
28
    public function quietlyAsUser($command)
29
    {
30
        $this->quietly('sudo -u '.user().' '.$command.' > /dev/null 2>&1');
31
    }
32
33
    /**
34
     * Pass the command to the command line and display the output.
35
     *
36
     * @param string $command
37
     *
38
     * @return void
39
     */
40
    public function passthru($command)
41
    {
42
        passthru($command);
43
    }
44
45
    /**
46
     * Run the given command as the non-root user.
47
     *
48
     * @param string   $command
49
     * @param callable $onError
50
     *
51
     * @return string
52
     */
53
    public function run($command, callable $onError = null)
54
    {
55
        return $this->runCommand($command, $onError);
56
    }
57
58
    /**
59
     * Run the given command.
60
     *
61
     * @param string   $command
62
     * @param callable $onError
63
     *
64
     * @return string
65
     */
66
    public function runAsUser(string $command, callable $onError = null)
67
    {
68
        return $this->runCommand('sudo -u '.user().' '.$command, $onError);
69
    }
70
71
    /**
72
     * Run the given command.
73
     *
74
     * @param string   $command
75
     * @param callable $onError
76
     *
77
     * @return string
78
     */
79
    protected function runCommand($command, callable $onError = null)
80
    {
81
        $onError = $onError ?: function () {
82
        };
83
84
        // Symfony's 4.x Process component has deprecated passing a command string
85
        // to the constructor, but older versions (which Valet's Composer
86
        // constraints allow) don't have the fromShellCommandLine method.
87
        // For more information, see: https://github.com/laravel/valet/pull/761
88
        if (method_exists(Process::class, 'fromShellCommandline')) {
89
            $process = Process::fromShellCommandline($command);
90
        } else {
91
            $process = new Process($command);
0 ignored issues
show
$command of type string is incompatible with the type array expected by parameter $command of Symfony\Component\Process\Process::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
            $process = new Process(/** @scrutinizer ignore-type */ $command);
Loading history...
92
        }
93
94
        $processOutput = '';
95
        $process->setTimeout(null)->run(function ($type, $line) use (&$processOutput) {
96
            $processOutput .= $line;
97
        });
98
99
        if ($process->getExitCode() > 0) {
100
            $onError($process->getExitCode(), $processOutput);
101
        }
102
103
        return $processOutput;
104
    }
105
}
106