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
|
|
|
|
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $envVars; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* ProcessBuilder constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $command The command to run. |
49
|
|
|
* @param string $path The working directory. |
50
|
|
|
*/ |
51
|
|
|
public function __construct(string $command, string $path) |
52
|
|
|
{ |
53
|
|
|
$this->arguments = [$command]; |
54
|
|
|
$this->path = $path; |
55
|
|
|
$this->timeout = 900; |
56
|
|
|
$this->envVars = []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $arguments arguments for process generation |
61
|
|
|
* @return ProcessBuilderInterface |
62
|
|
|
*/ |
63
|
|
|
public function setArguments(array $arguments): ProcessBuilderInterface |
64
|
|
|
{ |
65
|
|
|
if (!empty($this->arguments)) { |
66
|
|
|
$this->arguments = array_merge($this->arguments, $arguments); |
67
|
|
|
} else { |
68
|
|
|
$this->arguments = $arguments; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param int $timeout |
76
|
|
|
* @return ProcessBuilderInterface |
77
|
|
|
*/ |
78
|
|
|
public function setTimeout(int $timeout): ProcessBuilderInterface |
79
|
|
|
{ |
80
|
|
|
$this->timeout = $timeout; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $name name of ENV VAR |
87
|
|
|
* @param string $value |
88
|
|
|
* @return ProcessBuilderInterface |
89
|
|
|
*/ |
90
|
|
|
public function setEnv(string $name, string $value): ProcessBuilderInterface |
91
|
|
|
{ |
92
|
|
|
$this->envVars[$name] = $value; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
|
99
|
|
|
* @return Process |
100
|
|
|
*/ |
101
|
|
|
public function getProcess(): Process |
102
|
|
|
{ |
103
|
|
|
return ( |
104
|
|
|
new Process( |
105
|
|
|
$this->arguments, |
106
|
|
|
$this->path |
107
|
|
|
) |
108
|
|
|
) |
109
|
|
|
->setTimeout($this->timeout) |
110
|
|
|
->setEnv($this->envVars); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|