4 // This script reads the project composer.lock file to generate
5 // clear license details for our PHP dependencies.
7 declare(strict_types=1);
8 require "gen-licenses-shared.php";
10 $rootPath = dirname(__DIR__, 2);
11 $outputPath = "{$rootPath}/dev/licensing/php-library-licenses.txt";
12 $composerLock = json_decode(file_get_contents($rootPath . "/composer.lock"));
13 $outputSeparator = "\n-----------\n";
15 $packages = $composerLock->packages;
16 $packageOutput = array_map(packageToOutput(...), $packages);
18 $licenseInfo = implode($outputSeparator, $packageOutput) . "\n";
19 file_put_contents($outputPath, $licenseInfo);
21 echo "License information written to {$outputPath}\n";
22 echo implode("\n", getWarnings()) . "\n";
24 function packageToOutput(stdClass $package) : string {
26 $output = ["{$package->name}"];
28 $licenses = is_array($package->license) ? $package->license : [$package->license];
29 $output[] = "License: " . implode(' ', $licenses);
31 $packagePath = "{$rootPath}/vendor/{$package->name}/package.json";
32 $licenseFile = findLicenseFile($package->name, $packagePath);
34 $relLicenseFile = str_replace("{$rootPath}/", '', $licenseFile);
35 $output[] = "License File: {$relLicenseFile}";
36 $copyright = findCopyright($licenseFile);
38 $output[] = "Copyright: {$copyright}";
40 warn("Package {$package->name}: no copyright found in its license");
44 $source = $package->source->url;
46 $output[] = "Source: {$source}";
49 $link = $package->homepage ?? $package->source->url ?? '';
51 $output[] = "Link: {$link}";
54 return implode("\n", $output);