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; |
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
|
5 |
|
$this->active = true; |
34
|
5 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* passthru implementation |
38
|
|
|
* |
39
|
|
|
* @param string $command |
40
|
|
|
* @param array $arguments |
41
|
|
|
* |
42
|
|
|
* @return int |
43
|
|
|
*/ |
44
|
2 |
|
public function pass($command, array $arguments) |
45
|
|
|
{ |
46
|
2 |
|
$buffer = Lib::cmd($command, $arguments); |
47
|
2 |
|
$this->debug($buffer); |
48
|
2 |
|
if (!$this->active) { |
49
|
1 |
|
return 0; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
':' === $buffer ? $status = 0 : passthru($buffer, $status); |
53
|
1 |
|
$this->debug("exit status: ${status}"); |
54
|
|
|
|
55
|
|
|
return $status; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $command |
60
|
|
|
* @param array $arguments |
61
|
|
|
* @param null|string $out captured standard output |
62
|
|
|
* @param-out string $out |
63
|
|
|
* |
64
|
|
|
* @param null|string $err captured standard error |
65
|
|
|
* @param-out string $err |
66
|
|
|
* |
67
|
|
|
* @throws \RuntimeException |
68
|
|
|
* |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
|
|
public function capture($command, array $arguments, &$out = null, &$err = null) |
72
|
|
|
{ |
73
|
3 |
|
$buffer = Lib::cmd($command, $arguments); |
74
|
3 |
|
$this->debug($buffer); |
75
|
3 |
|
if (!$this->active) { |
76
|
1 |
|
isset($out) || $out = ''; |
77
|
1 |
|
isset($err) || $err = ''; |
78
|
|
|
|
79
|
1 |
|
return 0; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
$proc = new Proc($buffer); |
83
|
2 |
|
$status = $proc->run(); |
84
|
2 |
|
$this->debug("exit status: ${status}"); |
85
|
2 |
|
$out = $proc->getStandardOutput(); |
86
|
2 |
|
$err = $proc->getStandardError(); |
87
|
|
|
|
88
|
2 |
|
return $status; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param bool $active |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function setActive($active) |
97
|
|
|
{ |
98
|
1 |
|
$this->active = (bool)$active; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $message |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
private function debug($message) |
107
|
|
|
{ |
108
|
4 |
|
if ($this->debugPrinter) { |
109
|
1 |
|
call_user_func($this->debugPrinter, $message); |
110
|
|
|
} |
111
|
4 |
|
} |
112
|
|
|
} |
113
|
|
|
|