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