1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace uuf6429\ElderBrother\Action; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
|
9
|
|
|
class ExecuteProgram extends ActionAbstract |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $description; |
15
|
|
|
|
16
|
|
|
protected $command; |
17
|
|
|
protected $breakOnFailure; |
18
|
|
|
protected $environment; |
19
|
|
|
protected $currentDir; |
20
|
|
|
protected $timeout; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Executes an external program. |
24
|
|
|
* |
25
|
|
|
* @param string $description Description of the intention of the program |
26
|
|
|
* @param string $command Program command line (with parameters) |
27
|
|
|
* @param bool $breakOnFailure (Optional, default is true) Stop execution if program returns non-0 exit code |
28
|
|
|
* @param array|null $environment (Optional, default is null / current vars) Environment variables to pass to program |
29
|
|
|
* @param string|null $currentDir The current directory to use for program |
30
|
|
|
* @param int $timeout (Optional, default is 60) The time to wait for program to finish (in seconds) |
31
|
|
|
*/ |
32
|
2 |
|
public function __construct($description, $command, $breakOnFailure = true, $environment = null, $currentDir = null, $timeout = 60) |
33
|
|
|
{ |
34
|
2 |
|
$this->description = $description; |
35
|
2 |
|
$this->command = $command; |
36
|
2 |
|
$this->breakOnFailure = $breakOnFailure; |
37
|
2 |
|
$this->environment = $environment; |
38
|
2 |
|
$this->currentDir = $currentDir; |
39
|
2 |
|
$this->timeout = $timeout; |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
1 |
|
public function getName() |
46
|
|
|
{ |
47
|
1 |
|
return ($this->description ?: 'Execute External Program') . ' (ExecuteProgram)'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function isSupported() |
54
|
|
|
{ |
55
|
|
|
return true; // no special dependencies |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
2 |
|
public function execute(InputInterface $input, OutputInterface $output) |
62
|
|
|
{ |
63
|
2 |
|
$process = new Process( |
64
|
2 |
|
$this->command, |
65
|
2 |
|
$this->currentDir, |
66
|
2 |
|
$this->environment, |
67
|
2 |
|
null, |
68
|
2 |
|
$this->timeout |
69
|
|
|
); |
70
|
|
|
|
71
|
2 |
|
if ($this->breakOnFailure) { |
72
|
2 |
|
$process->mustRun(); |
73
|
|
|
} else { |
74
|
|
|
$process->run(); |
75
|
|
|
} |
76
|
1 |
|
} |
77
|
|
|
} |
78
|
|
|
|