fix command help text output
[pharext/pharext] / src / pharext / PackageInfo.php
1 <?php
2
3 namespace pharext;
4
5 trait PackageInfo
6 {
7 /**
8 * @param string $path package root
9 * @return array
10 */
11 public function findPackageInfo($path) {
12 try {
13 if (!strlen($name = $this->findPackageName($path, $header))) {
14 return [];
15 }
16 if (!$release = $this->findPackageReleaseVersion($path, $header, $name)) {
17 return [];
18 }
19 } catch (Exception $e) {
20 return [];
21 }
22
23 return compact("name", "release");
24 }
25
26 private function findPackageName($path, &$header = null) {
27 $grep_res = (new ExecCmd("grep"))->run(
28 array_merge(
29 ["-HEo", "phpext_[^ ]+_ptr"],
30 explode("\n", (new ExecCmd("find"))->run([
31 $path, "-type", "f", "-name", "php_*.h"
32 ])->getOutput())
33 )
34 )->getOutput();
35 if (!list($header, $phpext_ptr) = explode(":", $grep_res)) {
36 return [];
37 }
38 if (!$name = substr($phpext_ptr, 7, -4)) {
39 return [];
40 }
41 return $name;
42
43 }
44
45 private function findPackageReleaseVersion($path, $header, $name) {
46 $uc_name = strtoupper($name);
47 $cpp_tmp = new Tempfile("cpp");
48 $cpp_hnd = $cpp_tmp->getStream();
49 fprintf($cpp_hnd, "#include \"%s\"\n", $header);
50 fprintf($cpp_hnd, "#if defined(PHP_PECL_%s_VERSION)\n", $uc_name);
51 fprintf($cpp_hnd, "PHP_PECL_%s_VERSION\n", $uc_name);
52 fprintf($cpp_hnd, "#elif defined(PHP_%s_VERSION)\n", $uc_name);
53 fprintf($cpp_hnd, "PHP_%s_VERSION\n", $uc_name);
54 fprintf($cpp_hnd, "#elif defined(%s_VERSION)\n", $uc_name);
55 fprintf($cpp_hnd, "%s_VERSION\n", $uc_name);
56 fprintf($cpp_hnd, "#endif\n");
57 fflush($cpp_hnd);
58 $php_cnf = (defined("PHP_BINARY") ? PHP_BINARY : "php") . "-config";
59 $php_inc = (new ExecCmd($php_cnf))->run([
60 "--includes"
61 ])->getOutput();
62 $ext_inc = (new ExecCmd("find"))->run([
63 $path, "-not", "-path", "*/.*", "-type", "d", "-printf", "-I'%p' "
64 ])->getOutput();
65 $cpp_res = (new ExecCmd("cpp $php_inc $ext_inc"))->run([
66 "-E", $cpp_tmp
67 ])->getOutput();
68 return trim(substr($cpp_res, strrpos($cpp_res, "\n")), "\"\n");
69 }
70 }