setName('generate')
->setDescription('Generate Licenses file from project dependencies.')
->addOption('hide-version', 'hv', InputOption::VALUE_NONE, 'Hide dependency version')
->addOption('csv', null, InputOption::VALUE_NONE, 'Output csv format');
}
/**
* Execute the command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->hideVersion = $input->getOption('hide-version');
$this->toCsv = $input->getOption('csv');
$dependencies = $this->getDependencyList();
$output->writeln('Generating Licenses file...');
if ($this->toCsv) {
$this->generateLicensesCSV($dependencies);
} else {
$this->generateLicensesText($dependencies);
}
$output->writeln('Done!');
return 0;
}
/**
* Generates Licenses Text using packages retrieved from composer.lock file.
*
* @param array $dependencies
*
* @return void
*/
protected function generateLicensesText($dependencies)
{
$text = $this->getBoilerplate();
foreach ($dependencies as $dependency) {
$text .= $this->getTextForDependency($dependency);
}
file_put_contents('licenses.md', $text);
}
protected function generateLicensesCSV($dependencies)
{
$fp = fopen('licenses.csv', 'w');
$title = ['name', 'version', 'source', 'license description'];
fputcsv($fp, $title);
foreach ($dependencies as $dependency) {
$dependencyLists = [
$dependency['name'],
$this->hideVersion ? '' : $dependency['version'],
$dependency['source']['url'],
$this->getTextForDependency($dependency),
];
fputcsv($fp, $dependencyLists);
}
fclose($fp);
}
/**
* Returns Boilerplate text for the Licences File.
*
* @return string
*/
protected function getBoilerplate()
{
return "# Project Licenses\nThis file was generated by the [PHP Legal Licenses](https://github.com/Comcast/php-legal-licenses) utility. It contains the name, version and commit sha, description, homepage, and license information for every dependency in this project.\n\n## Dependencies\n\n";
}
/**
* Retrieves text containing version, sha, and license information for the specified dependency.
*
* @param array $dependency
*
* @return string
*/
protected function getTextForDependency($dependency)
{
$name = $dependency['name'];
$description = isset($dependency['description']) ? $dependency['description'] : 'Not configured.';
$version = $dependency['version'];
$homepage = isset($dependency['homepage']) ? $dependency['homepage'] : 'Not configured.';
$sha = isset($dependency['source']) ? str_split($dependency['source']['reference'], 7)[0] : 'no sha';
$licenseNames = isset($dependency['license']) ? implode(', ', $dependency['license']) : 'Not configured.';
$license = $this->getFullLicenseText($name);
return $this->generateDependencyText($name, $description, $version, $homepage, $sha, $licenseNames, $license);
}
/**
* Retrieves full license text for a dependency from the vendor directory.
*
* @param string $name
*
* @return string
*/
protected function getFullLicenseText($name)
{
$path = getcwd()."/vendor/$name/";
$filenames = ['LICENSE.txt', 'LICENSE.md', 'LICENSE', 'license.txt', 'license.md', 'license', 'LICENSE-2.0.txt'];
foreach ($filenames as $filename) {
$text = @file_get_contents($path.$filename);
if ($text) {
return $text;
}
}
return 'Full license text not found in dependency source.';
}
/**
* Generates Dependency Text based on boilerplate.
*
* @param string $name
* @param string $description
* @param string $version
* @param string $homepage
* @param string $sha
* @param string $licenseNames
* @param string $license
*
* @return string
*/
protected function generateDependencyText($name, $description, $version, $homepage, $sha, $licenseNames, $license)
{
return "### $name ".($this->hideVersion ? '' : "(Version $version | $sha)")."\n$description\nHomepage: $homepage\nLicenses Used: $licenseNames\n$license\n\n";
}
}__halt_compiler();----SIGNATURE:----HMiJeQE6LcyvOlaZZwHsKOjbosef8g0maiYXGn3n7DI67taspg8tKLepzvljXe73FcdOB94m2Oqazh8rVpNtEeGXx6sbVdvJ53yLEIXDt+VCTTMYnU+xBzRsr7QhXkaqVSr3jHAd20Fu4hvp+GLPXx7bQr2C4iirPPMTd6pqQhnABHxEufdbmKaHuPAlWP64RBedpFtv2DFQ2dlhumKmL5zRvxApwwccRHaKwX43D4DVA0aGxc90FXGbyneKl7ypM4nzxEANw6naSjHlg304ebRXmtacAvvo4q56x12eQDXrtT/PExhF8k2PT4HRV0TZB3onvTKOnwiDDAQxvDskSqaFp1tkYeZ/3riKbJXIIDz1M6uBSRRckcrM0SeZBpJu27BYaX6UdamVMG7wcG/Ta5Gy2rnrbGM4CmXn5lW2g6dHeri9qV2kGZkEt2/rGyq6A1NpCKl0PNUGGkRLhLRONInu8V/BepU6NpG1qPnLPtEylEC0PZfWdMHk0LkSHe6Sp03qnPhniUxx5e90XtVL8MOPqQ03fINwgMtmS2TFiZNPVUu4gvwmdCXfbjYpgAn4Itx4Yf8IOgUjsbgcu7uQuJ8r2jDV4kYLBzP2Vv9XyBJc5FEzltUsCASZ2A1wHtzmiLoNLJs+pClEBVsbcD6Y1OGgwp5Luko7NhsQtrJFE/4=----ATTACHMENT:----MzQyNzkyNzcwNTY2NTQ2OCA3MDE1OTE4MTYwMjk0ODkxIDI5NDUzMjgxNzIyODk5NzE=