| 
									
										
										
										
											2024-03-22 22:44:23 +08:00
										 |  |  | #!/usr/bin/env php | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This script reads the project composer.lock file to generate | 
					
						
							|  |  |  | // clear license details for our PHP dependencies. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-23 23:33:05 +08:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | require "gen-licenses-shared.php"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-22 22:44:23 +08:00
										 |  |  | $rootPath = dirname(__DIR__, 2); | 
					
						
							|  |  |  | $outputPath = "{$rootPath}/dev/licensing/php-library-licenses.txt"; | 
					
						
							|  |  |  | $composerLock = json_decode(file_get_contents($rootPath . "/composer.lock")); | 
					
						
							|  |  |  | $outputSeparator = "\n-----------\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $packages = $composerLock->packages; | 
					
						
							|  |  |  | $packageOutput = array_map(packageToOutput(...), $packages); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $licenseInfo =  implode($outputSeparator, $packageOutput) . "\n"; | 
					
						
							|  |  |  | file_put_contents($outputPath, $licenseInfo); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | echo "License information written to {$outputPath}\n"; | 
					
						
							| 
									
										
										
										
											2024-03-24 19:58:31 +08:00
										 |  |  | echo implode("\n", getWarnings()) . "\n"; | 
					
						
							| 
									
										
										
										
											2024-03-22 22:44:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function packageToOutput(stdClass $package) : string { | 
					
						
							| 
									
										
										
										
											2024-03-23 23:33:05 +08:00
										 |  |  |     global $rootPath; | 
					
						
							| 
									
										
										
										
											2024-03-22 22:44:23 +08:00
										 |  |  |     $output = ["{$package->name}"]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $licenses = is_array($package->license) ? $package->license : [$package->license]; | 
					
						
							|  |  |  |     $output[] = "License: " . implode(' ', $licenses); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-23 23:33:05 +08:00
										 |  |  |     $packagePath = "{$rootPath}/vendor/{$package->name}/package.json"; | 
					
						
							|  |  |  |     $licenseFile = findLicenseFile($package->name, $packagePath); | 
					
						
							| 
									
										
										
										
											2024-03-22 22:44:23 +08:00
										 |  |  |     if ($licenseFile) { | 
					
						
							| 
									
										
										
										
											2024-03-23 23:33:05 +08:00
										 |  |  |         $relLicenseFile = str_replace("{$rootPath}/", '', $licenseFile); | 
					
						
							|  |  |  |         $output[] = "License File: {$relLicenseFile}"; | 
					
						
							| 
									
										
										
										
											2024-03-22 22:44:23 +08:00
										 |  |  |         $copyright = findCopyright($licenseFile); | 
					
						
							|  |  |  |         if ($copyright) { | 
					
						
							|  |  |  |             $output[] = "Copyright: {$copyright}"; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2024-03-23 23:33:05 +08:00
										 |  |  |             warn("Package {$package->name}: no copyright found in its license"); | 
					
						
							| 
									
										
										
										
											2024-03-22 22:44:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $source = $package->source->url; | 
					
						
							|  |  |  |     if ($source) { | 
					
						
							|  |  |  |         $output[] = "Source: {$source}"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $link = $package->homepage ?? $package->source->url ?? ''; | 
					
						
							|  |  |  |     if ($link) { | 
					
						
							|  |  |  |         $output[] = "Link: {$link}"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return implode("\n", $output); | 
					
						
							|  |  |  | } |