|
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\Process; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* ProcessRunner help to create Process so it can be easily |
|
23
|
|
|
* mock later in testing environment. |
|
24
|
|
|
* This class also have a predefined DebugFormatterHelper. |
|
25
|
|
|
*/ |
|
26
|
|
|
class ProcessRunner |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var LoggerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $logger; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var OutputInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $output; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* ProcessRunner constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param LoggerInterface $logger |
|
42
|
|
|
* @param OutputInterface $output |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
LoggerInterface $logger, |
|
46
|
|
|
OutputInterface $output |
|
47
|
|
|
) { |
|
48
|
|
|
$this->logger = $logger; |
|
49
|
|
|
$this->output = $output; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Creates process and run with predefined DebugFormatterHelper. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $commandline |
|
56
|
|
|
* @param callable|null $callback |
|
|
|
|
|
|
57
|
|
|
* @param string|null $cwd |
|
58
|
|
|
* @param array|null $env |
|
59
|
|
|
* @param null $input |
|
60
|
|
|
* @param int|float|null $timeout The timeout in seconds or null to disable |
|
61
|
|
|
* |
|
62
|
|
|
* @see Process |
|
63
|
|
|
* |
|
64
|
|
|
* @return Process |
|
65
|
|
|
*/ |
|
66
|
|
|
public function run($commandline, callable $callback = null, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60) |
|
67
|
|
|
{ |
|
68
|
|
|
$process = new Process( |
|
69
|
|
|
$commandline, |
|
70
|
|
|
$cwd, |
|
71
|
|
|
$env, |
|
72
|
|
|
$input, |
|
73
|
|
|
$timeout |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$helper = new DebugFormatterHelper(); |
|
77
|
|
|
$output = $this->output; |
|
78
|
|
|
$output->writeln($helper->start( |
|
79
|
|
|
spl_object_hash($process), |
|
80
|
|
|
'Executing: '.$commandline, |
|
81
|
|
|
'STARTED' |
|
82
|
|
|
)); |
|
83
|
|
|
$process->run(function ($type, $buffer) use ($helper,$output,$process,$callback) { |
|
84
|
|
|
if (is_callable($callback)) { |
|
85
|
|
|
call_user_func($callback, $type, $buffer); |
|
86
|
|
|
} |
|
87
|
|
|
$contents = $helper->progress( |
|
88
|
|
|
spl_object_hash($process), |
|
89
|
|
|
$buffer, |
|
90
|
|
|
Process::ERR === $type |
|
91
|
|
|
); |
|
92
|
|
|
$output->write($contents); |
|
93
|
|
|
}); |
|
94
|
|
|
|
|
95
|
|
|
return $process; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|