Commit | Line | Data |
---|---|---|
e691f069 TO |
1 | <?php |
2 | ||
3 | ini_set('memory_limit', '2G'); | |
4 | ini_set('safe_mode', 0); | |
5 | eval(cv('php:boot --level=classloader', 'phpcode')); | |
6 | ||
7 | // Allow autoloading of PHPUnit helper classes in this extension. | |
8 | $loader = new \Composer\Autoload\ClassLoader(); | |
9 | $loader->add('CRM_', __DIR__); | |
10 | $loader->add('Civi\\', __DIR__); | |
11 | $loader->add('api_', __DIR__); | |
12 | $loader->add('api\\', __DIR__); | |
13 | $loader->register(); | |
14 | ||
15 | /** | |
16 | * Call the "cv" command. | |
17 | * | |
18 | * @param string $cmd | |
19 | * The rest of the command to send. | |
20 | * @param string $decode | |
21 | * Ex: 'json' or 'phpcode'. | |
22 | * @return string | |
23 | * Response output (if the command executed normally). | |
24 | * @throws \RuntimeException | |
25 | * If the command terminates abnormally. | |
26 | */ | |
27 | function cv($cmd, $decode = 'json') { | |
28 | $cmd = 'cv ' . $cmd; | |
29 | $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR); | |
30 | $oldOutput = getenv('CV_OUTPUT'); | |
31 | putenv("CV_OUTPUT=json"); | |
32 | ||
33 | // Execute `cv` in the original folder. This is a work-around for | |
34 | // phpunit/codeception, which seem to manipulate PWD. | |
35 | $cmd = sprintf('cd %s; %s', escapeshellarg(getenv('PWD')), $cmd); | |
36 | ||
37 | $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__); | |
38 | putenv("CV_OUTPUT=$oldOutput"); | |
39 | fclose($pipes[0]); | |
40 | $result = stream_get_contents($pipes[1]); | |
41 | fclose($pipes[1]); | |
42 | if (proc_close($process) !== 0) { | |
43 | throw new RuntimeException("Command failed ($cmd):\n$result"); | |
44 | } | |
45 | switch ($decode) { | |
46 | case 'raw': | |
47 | return $result; | |
48 | ||
49 | case 'phpcode': | |
50 | // If the last output is /*PHPCODE*/, then we managed to complete execution. | |
51 | if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") { | |
52 | throw new \RuntimeException("Command failed ($cmd):\n$result"); | |
53 | } | |
54 | return $result; | |
55 | ||
56 | case 'json': | |
57 | return json_decode($result, 1); | |
58 | ||
59 | default: | |
60 | throw new RuntimeException("Bad decoder format ($decode)"); | |
61 | } | |
62 | } |