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 void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$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:----l3BqpBMRHC46UBiyOBcLL/44px3JO3JAPJtU+GR9CTOrFlHJJN6JPpSJWpQ14oOpkOAEsmk3iYkH88ybwpjGvVH1klEz4MMov3iQQolb2yxv1GOBI7nPKyNqHgG4IrVxTt9HsR/PZzxyAyWLI6U8jL8T2Aa1yLGn2YOGc/btwso8PtG/Beu5GJLc6R7v8JnSxTWb+9sEFoEIO+nDRBpAiCOWYj4293+HA7gWA5vwOdz9pPpEQBLeV6F0d3X5Gb9Tja/kbp+aJpvQl/anHlMbPIUlkBzmnRwCEr0u9ZQMK4rt/DGwH025/Xqm+AhOM+XQ6LSf+/Kz2159B1q7wDjEfXFL7doMKe5HoMo2841Y1VYo3cL7g/J4I913N2XQJgeuYT/PxrvD0XRpGMOoDLXK9l/EU9LjAROKkuy2s+nchArEl1ACNK39r89LcsRLz7ybD3/vCeotltYEoYI0j34X1a9iKniCbHsXWYwKntJo9q7zXcng3Z7Gk0YIWm1FpZB4B+OuGBAEtrZpZsNmGd/MDpUYmqRZsYsSKhHqz0sg+WQwAg7RI3Btkq+pdrLER8cdQ4SKLMgrPfBR1cDTyx/SgsBT/5EuLH5zOshS8LaoOogNThWoE1pAhBixB9b5KGvTog6+TzidCPvUyJ8IOL0dGsHzHvcPIEbyne7a2mWx/YQ=----ATTACHMENT:----NzM4NjQ0NTExNjI4NTI5OSAzOTQzMDU5NjE0MzgzNzc1IDMxOTE0OTUzMDQ4OTI4NTg=