--- /dev/null
+<?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;
+ }
+}
*/
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",
]);
*/
public function getPackageInfo();
+ /**
+ * Retrieve the full text license
+ * @return string
+ */
+ public function getLicense();
+
/**
* Provide installer command line args
* @return array|Traversable
namespace pharext\SourceDir;
use pharext\Cli\Args;
+use pharext\License;
use pharext\SourceDir;
use FilesystemIterator;
class Basic implements IteratorAggregate, SourceDir
{
+ use License;
+
private $path;
public function __construct($path) {
return [];
}
+ public function getLicense() {
+ if (($file = $this->findLicense($this->getBaseDir()))) {
+ return $this->readLicense($file);
+ }
+ return "UNKNOWN";
+ }
+
public function getArgs() {
return [];
}
namespace pharext\SourceDir;
-use pharext\Command;
use pharext\Cli\Args;
+use pharext\License;
use pharext\SourceDir;
/**
*/
class Git implements \IteratorAggregate, SourceDir
{
+ use License;
+
/**
* Base directory
* @var string
return [];
}
+ /**
+ * @inheritdoc
+ * @return string
+ */
+ public function getLicense() {
+ if (($file = $this->findLicense($this->getBaseDir()))) {
+ return $this->readLicense($file);
+ }
+ return "UNKNOWN";
+ }
+
/**
* @inheritdoc
* @return array
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
}
}
+ /**
+ * @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()