-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrapper.php
57 lines (52 loc) · 1.73 KB
/
wrapper.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
<?php
/**
* Call the "cv" command.
*
* The following are all roughly equivalent:
* - Bash: cv api system.flush
* - PHP: $result = json_decode(`cv api system.flush`, TRUE);
* - PHP: $result = cv('api system.flush')
*
* The `cv()` wrapper is useful because it:
* - Decodes output. If the command returns JSON, it's parsed.
* - Checks for fatal errors; if encountered, they're rethrown as exceptions.
*
* The wrapper should be simple to port to other languages.
*
* @param string $cmd
* The rest of the command to send.
* @param string $decode
* Ex: 'json', 'phpcode', or 'raw'.
* @return string
* Response output (if the command executed normally).
* @throws \RuntimeException
* If the command terminates abnormally.
*/
function cv($cmd, $decode = 'json') {
$cmd = 'cv ' . $cmd;
$descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
$oldOutput = getenv('CV_OUTPUT');
putenv("CV_OUTPUT=json");
$process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
putenv("CV_OUTPUT=$oldOutput");
fclose($pipes[0]);
$result = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) !== 0) {
throw new RuntimeException("Command failed ($cmd):\n$result");
}
switch ($decode) {
case 'raw':
return $result;
case 'phpcode':
// If the last output is /*PHPCODE*/, then we managed to complete execution.
if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") {
throw new \RuntimeException("Command failed ($cmd):\n$result");
}
return $result;
case 'json':
return json_decode($result, 1);
default:
throw new RuntimeException("Bad decoder format ($decode)");
}
}