1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Cli; |
6
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\Lib; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class to abstract command execution in command line interface |
11
|
|
|
* context. |
12
|
|
|
*/ |
13
|
|
|
class Exec |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var null|callable |
17
|
|
|
*/ |
18
|
|
|
private $debugPrinter; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
private $active = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Exec constructor. |
27
|
|
|
* |
28
|
|
|
* @param null|callable $debugPrinter |
29
|
|
|
*/ |
30
|
5 |
|
public function __construct($debugPrinter = null) |
31
|
|
|
{ |
32
|
5 |
|
$this->debugPrinter = $debugPrinter; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* passthru implementation |
37
|
|
|
* |
38
|
|
|
* @param string $command |
39
|
|
|
* @param array $arguments |
40
|
|
|
* |
41
|
|
|
* @return int |
42
|
|
|
*/ |
43
|
2 |
|
public function pass($command, array $arguments) |
44
|
|
|
{ |
45
|
2 |
|
$buffer = Lib::cmd($command, $arguments); |
46
|
2 |
|
$this->debug($buffer); |
47
|
2 |
|
if (!$this->active) { |
48
|
1 |
|
return 0; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
':' === $buffer ? $status = 0 : passthru($buffer, $status); |
52
|
1 |
|
$this->debug("exit status: {$status}"); |
53
|
|
|
|
54
|
1 |
|
return $status; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $command |
59
|
|
|
* @param array $arguments |
60
|
|
|
* @param null|string $out captured standard output |
61
|
|
|
* @param-out string $out |
62
|
|
|
* |
63
|
|
|
* @param null|string $err captured standard error |
64
|
|
|
* @param-out string $err |
65
|
|
|
* |
66
|
|
|
* @throws \RuntimeException |
67
|
|
|
* |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
3 |
|
public function capture($command, array $arguments, &$out = null, &$err = null) |
71
|
|
|
{ |
72
|
3 |
|
$buffer = Lib::cmd($command, $arguments); |
73
|
3 |
|
$this->debug($buffer); |
74
|
3 |
|
if (!$this->active) { |
75
|
1 |
|
isset($out) || $out = ''; |
76
|
1 |
|
isset($err) || $err = ''; |
77
|
|
|
|
78
|
1 |
|
return 0; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
$proc = new Proc($buffer); |
82
|
2 |
|
$status = $proc->run(); |
83
|
2 |
|
$this->debug("exit status: {$status}"); |
84
|
2 |
|
$out = $proc->getStandardOutput(); |
85
|
2 |
|
$err = $proc->getStandardError(); |
86
|
|
|
|
87
|
2 |
|
return $status; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param bool $active |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
1 |
|
public function setActive($active) |
96
|
|
|
{ |
97
|
1 |
|
$this->active = (bool)$active; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $message |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
4 |
|
private function debug($message) |
106
|
|
|
{ |
107
|
4 |
|
if ($this->debugPrinter) { |
108
|
1 |
|
call_user_func($this->debugPrinter, $message); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|