-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcess.php
48 lines (38 loc) · 1.12 KB
/
Process.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace ZhenMu\Support\Utils;
use Symfony\Component\Process\Process as SymfonyProcess;
use Symfony\Component\Console\Output\OutputInterface;
class Process
{
public static function run(string $cmd, mixed $output = null, ?string $cwd = null, array $env = []): SymfonyProcess
{
$cwd = $cwd ?? base_path();
$process = SymfonyProcess::fromShellCommandline($cmd, $cwd);
$process->setTimeout(900);
if ($process->isTty()) {
$process->setTty(true);
}
try {
if ($output !== false) {
$output = app(OutputInterface::class);
}
} catch (\Throwable $e) {
$output = $output ?? null;
}
$envs = [
'PATH' => rtrim(`echo \$PATH`),
] + $env;
if ($output) {
$output->write("\n");
$process->run(
function ($type, $line) use ($output) {
$output->write($line);
},
$envs,
);
} else {
$process->run(null, $envs);
}
return $process;
}
}