2023-04-15 18:46:46 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\util;
|
|
|
|
|
|
|
|
|
|
use SPC\exception\FileSystemException;
|
|
|
|
|
use SPC\exception\RuntimeException;
|
|
|
|
|
use SPC\exception\WrongUsageException;
|
|
|
|
|
use SPC\store\Config;
|
|
|
|
|
use SPC\store\FileSystem;
|
|
|
|
|
|
2024-05-16 10:51:31 +08:00
|
|
|
/**
|
|
|
|
|
* License dumper, dump source license files to target directory
|
|
|
|
|
*/
|
2023-04-15 18:46:46 +08:00
|
|
|
class LicenseDumper
|
|
|
|
|
{
|
|
|
|
|
private array $exts = [];
|
|
|
|
|
|
|
|
|
|
private array $libs = [];
|
|
|
|
|
|
|
|
|
|
private array $sources = [];
|
|
|
|
|
|
|
|
|
|
public function addExts(array $exts): LicenseDumper
|
|
|
|
|
{
|
|
|
|
|
$this->exts = array_merge($exts, $this->exts);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function addLibs(array $libs): LicenseDumper
|
|
|
|
|
{
|
|
|
|
|
$this->libs = array_merge($libs, $this->libs);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function addSources(array $sources): LicenseDumper
|
|
|
|
|
{
|
|
|
|
|
$this->sources = array_merge($sources, $this->sources);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-05-16 10:51:31 +08:00
|
|
|
* Dump source licenses to target directory
|
|
|
|
|
*
|
|
|
|
|
* @param string $target_dir Target directory
|
|
|
|
|
* @return bool Success or not
|
2023-04-15 18:46:46 +08:00
|
|
|
* @throws WrongUsageException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
public function dump(string $target_dir): bool
|
|
|
|
|
{
|
|
|
|
|
// mkdir first
|
|
|
|
|
if (is_dir($target_dir) && !FileSystem::removeDir($target_dir)) {
|
2023-09-04 14:13:15 +02:00
|
|
|
logger()->warning('Target dump directory is not empty, be aware!');
|
2023-04-15 18:46:46 +08:00
|
|
|
}
|
|
|
|
|
FileSystem::createDir($target_dir);
|
|
|
|
|
foreach ($this->exts as $ext) {
|
|
|
|
|
if (Config::getExt($ext, 'type') !== 'external') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-09-04 14:13:15 +02:00
|
|
|
|
2023-04-15 18:46:46 +08:00
|
|
|
$source_name = Config::getExt($ext, 'source');
|
2023-09-04 14:13:15 +02:00
|
|
|
foreach ($this->getSourceLicenses($source_name) as $index => $license) {
|
2024-05-16 10:51:31 +08:00
|
|
|
$result = file_put_contents("{$target_dir}/ext_{$ext}_{$index}.txt", $license);
|
|
|
|
|
if ($result === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-09-04 14:13:15 +02:00
|
|
|
}
|
2023-04-15 18:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($this->libs as $lib) {
|
|
|
|
|
$source_name = Config::getLib($lib, 'source');
|
2023-09-04 14:13:15 +02:00
|
|
|
foreach ($this->getSourceLicenses($source_name) as $index => $license) {
|
2024-05-16 10:51:31 +08:00
|
|
|
$result = file_put_contents("{$target_dir}/lib_{$lib}_{$index}.txt", $license);
|
|
|
|
|
if ($result === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-09-04 14:13:15 +02:00
|
|
|
}
|
2023-04-15 18:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($this->sources as $source) {
|
2023-09-04 14:13:15 +02:00
|
|
|
foreach ($this->getSourceLicenses($source) as $index => $license) {
|
2024-05-16 10:51:31 +08:00
|
|
|
$result = file_put_contents("{$target_dir}/src_{$source}_{$index}.txt", $license);
|
|
|
|
|
if ($result === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-09-04 14:13:15 +02:00
|
|
|
}
|
2023-04-15 18:46:46 +08:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-09-04 14:13:15 +02:00
|
|
|
* @return string[]
|
2023-04-15 18:46:46 +08:00
|
|
|
* @throws FileSystemException
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2023-09-04 14:13:15 +02:00
|
|
|
private function getSourceLicenses(string $source_name): iterable
|
2023-04-15 18:46:46 +08:00
|
|
|
{
|
2023-09-04 14:13:15 +02:00
|
|
|
$licenses = Config::getSource($source_name)['license'] ?? [];
|
|
|
|
|
if ($licenses === []) {
|
|
|
|
|
throw new RuntimeException('source [' . $source_name . '] license meta not exist');
|
2023-04-15 18:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
2023-09-04 14:13:15 +02:00
|
|
|
if (!array_is_list($licenses)) {
|
|
|
|
|
$licenses = [$licenses];
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 20:05:09 +02:00
|
|
|
foreach ($licenses as $index => $license) {
|
|
|
|
|
yield ($license['suffix'] ?? $index) => match ($license['type']) {
|
2023-09-04 14:13:15 +02:00
|
|
|
'text' => $license['text'],
|
|
|
|
|
'file' => $this->loadSourceFile($source_name, $license['path'], Config::getSource($source_name)['path'] ?? null),
|
|
|
|
|
default => throw new RuntimeException('source [' . $source_name . '] license type is not allowed'),
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-04-15 18:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2023-10-16 22:58:49 +02:00
|
|
|
private function loadSourceFile(string $source_name, ?string $in_path, ?string $custom_base_path = null): string
|
2023-04-15 18:46:46 +08:00
|
|
|
{
|
2023-10-16 22:58:49 +02:00
|
|
|
if (is_null($in_path)) {
|
|
|
|
|
throw new RuntimeException('source [' . $source_name . '] license file is not set, please check config/source.json');
|
|
|
|
|
}
|
2023-04-15 18:46:46 +08:00
|
|
|
if (!file_exists(SOURCE_PATH . '/' . ($custom_base_path ?? $source_name) . '/' . $in_path)) {
|
2023-09-04 14:13:15 +02:00
|
|
|
throw new RuntimeException('source [' . $source_name . '] license file [' . $in_path . '] not exist');
|
2023-04-15 18:46:46 +08:00
|
|
|
}
|
2023-09-04 14:13:15 +02:00
|
|
|
|
2023-04-15 18:46:46 +08:00
|
|
|
return file_get_contents(SOURCE_PATH . '/' . ($custom_base_path ?? $source_name) . '/' . $in_path);
|
|
|
|
|
}
|
|
|
|
|
}
|