-
Notifications
You must be signed in to change notification settings - Fork 11
/
CmdExecuteService.php
146 lines (127 loc) · 3.52 KB
/
CmdExecuteService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Islandora\Crayfish\Commons;
use Psr\Log\LoggerInterface;
/**
* Runs a command streaming data in on stdin and out on stdout.
*
* @package Islandora\Crayfish\Commons
*/
class CmdExecuteService
{
/**
* @var null|\Psr\Log\LoggerInterface
*/
protected $log;
/**
* @var resource
*/
protected $output;
/**
* Executor constructor.
* @param LoggerInterface $log
*/
public function __construct(LoggerInterface $log = null)
{
$this->log = $log;
}
/**
* $output getter.
*
* @return resource;
*/
public function getOutputStream()
{
return $this->output;
}
/**
* Runs the command
*
* @param $cmd
* @param $data
*
* @throws \RuntimeException
*
* @return \Closure
* Closure that streams the output of the command.
*/
public function execute($cmd, $data)
{
// Use pipes for STDIN, STDOUT, and STDERR
$descr = array(
0 => array(
'pipe',
'r'
) ,
1 => array(
'pipe',
'w'
) ,
2 => array(
'pipe',
'w'
)
);
$pipes = [];
// Start process, telling it to use STDIN for input and STDOUT for output.
$cmd = escapeshellcmd($cmd);
$process = proc_open($cmd, $descr, $pipes);
// Get the data into pipe only if data is resource
if (gettype($data) == "resource") {
// Stream input to STDIN
while (!feof($data)) {
fwrite($pipes[0], fread($data, 1024));
}
// Close STDIN and the source data.
fclose($pipes[0]);
fclose($data);
}
// Wait for process to finish while reading STDOUT to a temp stream.
// Otherwise the process can block indefinitely if STODUT gets bigger
// than 4kb.
$this->output = fopen("php://temp", 'w+');
$exit_code = null;
while ($exit_code === null) {
$status = proc_get_status($process);
if ($status['running'] === false) {
$exit_code = $status['exitcode'];
}
$chunk = stream_get_contents($pipes[1]);
if ($chunk !== false) {
fwrite($this->output, $chunk);
}
}
// Close STDOUT
fclose($pipes[1]);
// On error, extract message from STDERR and throw an exception.
if ($exit_code != 0) {
$msg = stream_get_contents($pipes[2]);
$this->cleanup($pipes, $this->output, $process);
if ($this->log) {
$this->log->error('Process exited with non-zero code.', [
'exit_code' => $exit_code,
'stderr' => $msg,
]);
}
throw new \RuntimeException($msg, 500);
}
// Return a function that streams the output.
return function () use ($pipes, $process) {
rewind($this->output);
while (!feof($this->output)) {
echo fread($this->output, 1024);
ob_flush();
flush();
}
$this->cleanup($pipes, $this->output, $process);
};
}
protected function cleanup($pipes, $output, $process)
{
// Close STDERR
fclose($pipes[2]);
// Close the temp output stream.
fclose($this->output);
// Close the process
proc_close($process);
}
}