license helper
authorMichael Wallner <mike@php.net>
Fri, 15 May 2015 10:52:54 +0000 (12:52 +0200)
committerMichael Wallner <mike@php.net>
Fri, 15 May 2015 10:52:54 +0000 (12:52 +0200)
bin/pharext
src/pharext/License.php [new file with mode: 0644]
src/pharext/Packager.php
src/pharext/SourceDir.php
src/pharext/SourceDir/Basic.php
src/pharext/SourceDir/Git.php
src/pharext/SourceDir/Pecl.php

index e9c765d71c5d00aa95454691248432fc1c20a00d..8f90155d7cc18f523256040d4e7df0eba2315e07 100755 (executable)
Binary files a/bin/pharext and b/bin/pharext differ
diff --git a/src/pharext/License.php b/src/pharext/License.php
new file mode 100644 (file)
index 0000000..0f339fd
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+namespace pharext;
+
+trait License
+{
+       function findLicense($dir, $file = null) {
+               if (isset($file)) {
+                       return realpath("$dir/$file");
+               }
+
+               $names = [];
+               foreach (["{,UN}LICEN{S,C}{E,ING}", "COPY{,ING,RIGHT}"] as $name) {
+                       $names[] = $this->mergeLicensePattern($name, strtolower($name));
+               }
+               $exts = [];
+               foreach (["t{,e}xt", "rst", "asc{,i,ii}", "m{,ark}d{,own}", "htm{,l}"] as $ext) {
+                       $exts[] = $this->mergeLicensePattern(strtoupper($ext), $ext);
+               }
+               
+               $pattern = "{". implode(",", $names) ."}{,.{". implode(",", $exts) ."}}";
+
+               if (($glob = glob("$dir/$pattern", GLOB_BRACE))) {
+                       return current($glob);
+               }
+       }
+
+       private function mergeLicensePattern($upper, $lower) {
+               $pattern = "";
+               $length = strlen($upper);
+               for ($i = 0; $i < $length; ++$i) {
+                       if ($lower{$i} === $upper{$i}) {
+                               $pattern .= $upper{$i};
+                       } else {
+                               $pattern .= "[" . $upper{$i} . $lower{$i} . "]";
+                       }
+               }
+               return $pattern;
+       }
+
+       public function readLicense($file) {
+               $text = file_get_contents($file);
+               switch (strtolower(pathinfo($file, PATHINFO_EXTENSION))) {
+                       case "htm":
+                       case "html":
+                               $text = strip_tags($text);
+                               break;
+               }
+               return $text;
+       }
+}
index cd3b4d660fdd62e70bec8e3459946a41c684b34f..b54d2849092adf724d837fe9174b6340e790053f 100644 (file)
@@ -243,16 +243,10 @@ class Packager implements Command
         */
        private function createPackage() {
                try {
-                       if (($glob = glob($this->source->getBaseDir()."/LICENSE*"))) {
-                               $license = file_get_contents(current($glob));
-                       } else {
-                               $this->warn("Could not find any LICENSE.* files!\n");
-                               $license = "UNKNOWN\n";
-                       }
                        $meta = array_merge(Metadata::all(), [
                                "name" => $this->args->name,
                                "release" => $this->args->release,
-                               "license" => $license,
+                               "license" => $this->source->getLicense(),
                                "stub" => "pharext_installer.php",
                                "type" => $this->args->zend ? "zend_extension" : "extension",
                        ]);
index 8620668d5215220ba199a069632966c112240b14..c7068ba235763917d153851bd1a917f05212fc31 100644 (file)
@@ -19,6 +19,12 @@ interface SourceDir extends \Traversable
         */
        public function getPackageInfo();
 
+       /**
+        * Retrieve the full text license
+        * @return string
+        */
+       public function getLicense();
+
        /**
         * Provide installer command line args
         * @return array|Traversable
index 414c2015765a884f7c993316aca569f97645692c..fa180fee5896eede4bcefb5aebf2032a9c45e07b 100644 (file)
@@ -3,6 +3,7 @@
 namespace pharext\SourceDir;
 
 use pharext\Cli\Args;
+use pharext\License;
 use pharext\SourceDir;
 
 use FilesystemIterator;
@@ -14,6 +15,8 @@ use RecursiveIteratorIterator;
 
 class Basic implements IteratorAggregate, SourceDir
 {
+       use License;
+       
        private $path;
        
        public function __construct($path) {
@@ -28,6 +31,13 @@ class Basic implements IteratorAggregate, SourceDir
                return [];
        }
        
+       public function getLicense() {
+               if (($file = $this->findLicense($this->getBaseDir()))) {
+                       return $this->readLicense($file);
+               }
+               return "UNKNOWN";
+       }
+
        public function getArgs() {
                return [];
        }
index e17a3055c15d1b5fd534e73f3bdbb3f529afbc67..62dc24f9e5162cfde0cf7ff3d6380c4e5a7a2875 100644 (file)
@@ -2,8 +2,8 @@
 
 namespace pharext\SourceDir;
 
-use pharext\Command;
 use pharext\Cli\Args;
+use pharext\License;
 use pharext\SourceDir;
 
 /**
@@ -11,6 +11,8 @@ use pharext\SourceDir;
  */
 class Git implements \IteratorAggregate, SourceDir
 {
+       use License;
+       
        /**
         * Base directory
         * @var string
@@ -41,6 +43,17 @@ class Git implements \IteratorAggregate, SourceDir
                return [];
        }
 
+       /**
+        * @inheritdoc
+        * @return string
+        */
+       public function getLicense() {
+               if (($file = $this->findLicense($this->getBaseDir()))) {
+                       return $this->readLicense($file);
+               }
+               return "UNKNOWN";
+       }
+
        /**
         * @inheritdoc
         * @return array
index 351eaa21eae0b869dcb1cf857d049a4b426c821a..277e122b255c9cd67b140dcde8bf68df412a264b 100644 (file)
@@ -5,13 +5,15 @@ namespace pharext\SourceDir;
 use pharext\Cli\Args;
 use pharext\Exception;
 use pharext\SourceDir;
-use pharext\Tempfile;
+use pharext\License;
 
 /**
  * A PECL extension source directory containing a v2 package.xml
  */
 class Pecl implements \IteratorAggregate, SourceDir
 {
+       use License;
+       
        /**
         * The package.xml
         * @var SimpleXmlElement
@@ -73,6 +75,25 @@ class Pecl implements \IteratorAggregate, SourceDir
                }
        }
 
+       /**
+        * @inheritdoc
+        * @return string
+        */
+       public function getLicense() {
+               if (($license = $this->sxe->xpath("/pecl:package/pecl:license"))) {
+                       if (($file = $this->findLicense($this->getBaseDir(), $license[0]["filesource"]))) {
+                               return $this->readLicense($file);
+                       }
+               }
+               if (($file = $this->findLicense($this->getBaseDir()))) {
+                       return $this->readLicense($file);
+               }
+               if ($license) {
+                       return $license[0] ." ". $license[0]["uri"];
+               }
+               return "UNKNOWN";
+       }
+
        /**
         * @inheritdoc
         * @see \pharext\SourceDir::getArgs()