sourcedir: guess basic package info
authorMichael Wallner <mike@php.net>
Sun, 9 Oct 2016 17:29:11 +0000 (19:29 +0200)
committerMichael Wallner <mike@php.net>
Sun, 9 Oct 2016 17:29:11 +0000 (19:29 +0200)
src/pharext/PackageInfo.php [new file with mode: 0644]
src/pharext/SourceDir/Basic.php
src/pharext/SourceDir/Git.php

diff --git a/src/pharext/PackageInfo.php b/src/pharext/PackageInfo.php
new file mode 100644 (file)
index 0000000..3e2a78d
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+
+namespace pharext;
+
+trait PackageInfo
+{
+       /**
+        * @return array
+        */
+       public function findPackageInfo($path) {
+               try {
+                       if (!strlen($name = $this->findPackageName($path, $header))) {
+                               return [];
+                       }
+                       if (!$release = $this->findPackageReleaseVersion($path, $header, strtoupper($name))) {
+                               return [];
+                       }
+               } catch (Exception $e) {
+                       return [];
+               }
+
+               return compact("name", "release");
+       }
+
+       private function findPackageName($path, &$header = null) {
+               $grep_res = (new ExecCmd("grep"))->run(
+                       array_merge(
+                               ["-HEo", "phpext_[^ ]+_ptr"],
+                               explode("\n", (new ExecCmd("find"))->run([
+                                       $path, "-type", "f", "-name", "php_*.h"
+                               ])->getOutput())
+                       )
+               )->getOutput();
+               if (!list($header, $phpext_ptr) = explode(":", $grep_res)) {
+                       return [];
+               }
+               if (!$name = substr($phpext_ptr, 7, -4)) {
+                       return [];
+               }
+               return $name;
+
+       }
+
+       private function findPackageReleaseVersion($path, $header, $uname) {
+               $cpp_tmp = new Tempfile("cpp");
+               $cpp_hnd = $cpp_tmp->getStream();
+               fprintf($cpp_hnd, "#include \"%s\"\n", $header);
+               fprintf($cpp_hnd, "#if defined(PHP_PECL_%s_VERSION)\n", $uname);
+               fprintf($cpp_hnd, "PHP_PECL_%s_VERSION\n", $uname);
+               fprintf($cpp_hnd, "#elif defined(PHP_%s_VERSION)\n", $uname);
+               fprintf($cpp_hnd, "PHP_%s_VERSION\n", $uname);
+               fprintf($cpp_hnd, "#elif defined(%s_VERSION)\n", $uname);
+               fprintf($cpp_hnd, "%s_VERSION\n", $uname);
+               fprintf($cpp_hnd, "#endif\n");
+               fflush($cpp_hnd);
+               $php_inc = (new ExecCmd((PHP_BINARY ?? "php")."-config"))->run([
+                       "--includes"
+               ])->getOutput();
+               $ext_inc = (new ExecCmd("find"))->run([
+                       $path, "-not", "-path", "*/.*", "-type", "d", "-printf", "-I'%p' "
+               ])->getOutput();
+               $cpp_res = (new ExecCmd("cpp $php_inc $ext_inc"))->run([
+                       "-E", $cpp_tmp
+               ])->getOutput();
+               return trim(substr($cpp_res, strrpos($cpp_res, "\n")), "\"\n");
+       }
+}
index f960236c765fb9c1484c1bb268b58757f0021d81..13891d02a9b65eeb452a065e7453f4f9d6362086 100644 (file)
@@ -4,6 +4,7 @@ namespace pharext\SourceDir;
 
 use pharext\Cli\Args;
 use pharext\License;
+use pharext\PackageInfo;
 use pharext\SourceDir;
 
 use FilesystemIterator;
@@ -16,6 +17,7 @@ use RecursiveIteratorIterator;
 class Basic implements IteratorAggregate, SourceDir
 {
        use License;
+       use PackageInfo;
        
        private $path;
        
@@ -26,9 +28,13 @@ class Basic implements IteratorAggregate, SourceDir
        public function getBaseDir() {
                return $this->path;
        }
-       
+
+       /**
+        * @inheritdoc
+        * @return array
+        */
        public function getPackageInfo() {
-               return [];
+               return $this->findPackageInfo($this->getBaseDir());
        }
        
        public function getLicense() {
index 62dc24f9e5162cfde0cf7ff3d6380c4e5a7a2875..9c51c7d25febbbb651eacb9d7e47251f01ab883b 100644 (file)
@@ -3,8 +3,12 @@
 namespace pharext\SourceDir;
 
 use pharext\Cli\Args;
+use pharext\Exception;
+use pharext\ExecCmd;
 use pharext\License;
+use pharext\PackageInfo;
 use pharext\SourceDir;
+use pharext\Tempfile;
 
 /**
  * Extension source directory which is a git repo
@@ -12,6 +16,7 @@ use pharext\SourceDir;
 class Git implements \IteratorAggregate, SourceDir
 {
        use License;
+       use PackageInfo;
        
        /**
         * Base directory
@@ -40,7 +45,7 @@ class Git implements \IteratorAggregate, SourceDir
         * @return array
         */
        public function getPackageInfo() {
-               return [];
+               return $this->findPackageInfo($this->getBaseDir());
        }
 
        /**