|
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\Util; |
|
15
|
|
|
|
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Symfony\Component\Process\Exception\RuntimeException; |
|
18
|
|
|
use Symfony\Component\Process\Process; |
|
19
|
|
|
|
|
20
|
|
|
class CommandProcessor |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var LoggerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $logger; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
LoggerInterface $logger |
|
29
|
|
|
) { |
|
30
|
|
|
$this->logger = $logger; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string|array $commandline The command line to run |
|
35
|
|
|
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process |
|
36
|
|
|
* @param array|null $env The environment variables or null to use the same environment as the current PHP process |
|
37
|
|
|
* @param mixed|null $input The input as stream resource, scalar or \Traversable, or null for no input |
|
38
|
|
|
* @param int|float|null $timeout The timeout in seconds or null to disable |
|
39
|
|
|
* |
|
40
|
|
|
* @throws RuntimeException When proc_open is not installed |
|
41
|
|
|
* |
|
42
|
|
|
* @return Process |
|
43
|
|
|
*/ |
|
44
|
|
|
public function create($commandline, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60) |
|
45
|
|
|
{ |
|
46
|
|
|
$process = new Process($commandline, $cwd, $env, $input, $timeout); |
|
47
|
|
|
$this->debug($commandline); |
|
48
|
|
|
|
|
49
|
|
|
return $process; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function debug($message, $context = array()): void |
|
53
|
|
|
{ |
|
54
|
|
|
$message = '<comment>[command]</comment> '.$message; |
|
55
|
|
|
$this->logger->debug($message, $context); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|