|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the dotfiles project. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Anthonius Munthi <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Dotfiles\Core\Processor; |
|
15
|
|
|
|
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Symfony\Component\Console\Helper\DebugFormatterHelper; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
use Symfony\Component\Process\Exception\RuntimeException; |
|
20
|
|
|
use Symfony\Component\Process\Process; |
|
21
|
|
|
|
|
22
|
|
|
class ProcessRunner |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var LoggerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $logger; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var OutputInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $output; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var float |
|
36
|
|
|
*/ |
|
37
|
|
|
private $timeout = 60; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
LoggerInterface $logger, |
|
41
|
|
|
OutputInterface $output |
|
42
|
|
|
) { |
|
43
|
|
|
$this->logger = $logger; |
|
44
|
|
|
$this->output = $output; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string|array $commandline The command line to run |
|
49
|
|
|
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process |
|
50
|
|
|
* @param array|null $env The environment variables or null to use the same environment as the current PHP process |
|
51
|
|
|
* @param mixed|null $input The input as stream resource, scalar or \Traversable, or null for no input |
|
52
|
|
|
* @param int|float|null $timeout The timeout in seconds or null to disable |
|
53
|
|
|
* |
|
54
|
|
|
* @throws RuntimeException When proc_open is not installed |
|
55
|
|
|
* |
|
56
|
|
|
* @deprecated use run method |
|
57
|
|
|
* |
|
58
|
|
|
* @return Process |
|
59
|
|
|
*/ |
|
60
|
|
|
public function create($commandline, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60) |
|
61
|
|
|
{ |
|
62
|
|
|
$process = new Process($commandline, $cwd, $env, $input, $timeout); |
|
63
|
|
|
$this->debug($commandline); |
|
64
|
|
|
|
|
65
|
|
|
return $process; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function run($commandline, callable $callback = null, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60) |
|
69
|
|
|
{ |
|
70
|
|
|
$process = new Process( |
|
71
|
|
|
$commandline, |
|
72
|
|
|
$cwd, |
|
73
|
|
|
$env, |
|
74
|
|
|
$input, |
|
75
|
|
|
$timeout |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
$helper = new DebugFormatterHelper(); |
|
79
|
|
|
$output = $this->output; |
|
80
|
|
|
|
|
81
|
|
|
$process->run(function ($type, $buffer) use ($helper,$output,$process,$callback) { |
|
82
|
|
|
if (is_callable($callback)) { |
|
83
|
|
|
call_user_func($callback, $type, $buffer); |
|
84
|
|
|
} |
|
85
|
|
|
$contents = $helper->progress( |
|
86
|
|
|
spl_object_hash($process), |
|
87
|
|
|
$buffer, |
|
88
|
|
|
Process::ERR === $type |
|
89
|
|
|
); |
|
90
|
|
|
$output->writeln($contents); |
|
91
|
|
|
}); |
|
92
|
|
|
|
|
93
|
|
|
return $process; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param float $timeout |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setTimeout(float $timeout): void |
|
100
|
|
|
{ |
|
101
|
|
|
$this->timeout = $timeout; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function debug($message, $context = array()): void |
|
105
|
|
|
{ |
|
106
|
|
|
$message = '<comment>[command]</comment> '.$message; |
|
107
|
|
|
$this->logger->debug($message, $context); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|