1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/* |
4
|
|
|
* This file is part of the asm\php-ansible package. |
5
|
|
|
* |
6
|
|
|
* (c) Marc Aschmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Asm\Ansible\Process; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Process\Process; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Wrapper for symfony process component to allow for command option/argument collection before execute |
18
|
|
|
* |
19
|
|
|
* @package Asm\Ansible\Process |
20
|
|
|
* @author Marc Aschmann <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class ProcessBuilder implements ProcessBuilderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $arguments; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $timeout; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $path; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $envVars; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* ProcessBuilder constructor. |
46
|
|
|
* |
47
|
|
|
* @param string $prefix |
48
|
|
|
* @param string $path |
49
|
|
|
*/ |
50
|
|
|
public function __construct(string $prefix, string $path) |
51
|
|
|
{ |
52
|
|
|
$this->arguments = [$prefix]; |
53
|
|
|
$this->path = $path; |
54
|
|
|
$this->timeout = 900; |
55
|
|
|
$this->envVars = []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $arguments arguments for process generation |
60
|
|
|
* @return ProcessBuilderInterface |
61
|
|
|
*/ |
62
|
|
|
public function setArguments(array $arguments): ProcessBuilderInterface |
63
|
|
|
{ |
64
|
|
|
$this->arguments = $arguments; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param int $timeout |
71
|
|
|
* @return ProcessBuilderInterface |
72
|
|
|
*/ |
73
|
|
|
public function setTimeout(int $timeout): ProcessBuilderInterface |
74
|
|
|
{ |
75
|
|
|
$this->timeout = $timeout; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $name name of ENV VAR |
82
|
|
|
* @param string $value |
83
|
|
|
* @return ProcessBuilderInterface |
84
|
|
|
*/ |
85
|
|
|
public function setEnv(string $name, string $value): ProcessBuilderInterface |
86
|
|
|
{ |
87
|
|
|
$this->envVars[$name] = $value; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return Process |
94
|
|
|
*/ |
95
|
|
|
public function getProcess(): Process |
96
|
|
|
{ |
97
|
|
|
return ( |
98
|
|
|
new Process( |
99
|
|
|
$this->arguments, |
100
|
|
|
$this->path |
101
|
|
|
) |
102
|
|
|
) |
103
|
|
|
->setTimeout($this->timeout) |
104
|
|
|
->setEnv($this->envVars); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|