2023-07-28 00:02:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\store;
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
use SPC\exception\FileSystemException;
|
|
|
|
|
use SPC\exception\RuntimeException;
|
2023-07-28 00:02:49 +08:00
|
|
|
use SPC\exception\WrongUsageException;
|
|
|
|
|
|
|
|
|
|
class SourceExtractor
|
|
|
|
|
{
|
2023-08-20 19:51:45 +08:00
|
|
|
/**
|
|
|
|
|
* @throws WrongUsageException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2023-07-28 00:02:49 +08:00
|
|
|
public static function initSource(?array $sources = null, ?array $libs = null, ?array $exts = null): void
|
|
|
|
|
{
|
|
|
|
|
if (!file_exists(DOWNLOAD_PATH . '/.lock.json')) {
|
|
|
|
|
throw new WrongUsageException('Download lock file "downloads/.lock.json" not found, maybe you need to download sources first ?');
|
|
|
|
|
}
|
|
|
|
|
$lock = json_decode(FileSystem::readFile(DOWNLOAD_PATH . '/.lock.json'), true);
|
|
|
|
|
|
|
|
|
|
$sources_extracted = [];
|
|
|
|
|
// source check exist
|
|
|
|
|
if (is_array($sources)) {
|
|
|
|
|
foreach ($sources as $source) {
|
|
|
|
|
$sources_extracted[$source] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// lib check source exist
|
|
|
|
|
if (is_array($libs)) {
|
|
|
|
|
foreach ($libs as $lib) {
|
|
|
|
|
// get source name for lib
|
|
|
|
|
$source = Config::getLib($lib, 'source');
|
|
|
|
|
$sources_extracted[$source] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// ext check source exist
|
|
|
|
|
if (is_array($exts)) {
|
|
|
|
|
foreach ($exts as $ext) {
|
|
|
|
|
// get source name for ext
|
|
|
|
|
if (Config::getExt($ext, 'type') !== 'external') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$source = Config::getExt($ext, 'source');
|
|
|
|
|
$sources_extracted[$source] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// start check
|
|
|
|
|
foreach ($sources_extracted as $source => $item) {
|
|
|
|
|
if (Config::getSource($source) === null) {
|
2023-10-14 11:22:46 +08:00
|
|
|
throw new WrongUsageException("Source [{$source}] does not exist, please check the name and correct it !");
|
2023-07-28 00:02:49 +08:00
|
|
|
}
|
|
|
|
|
if (!isset($lock[$source])) {
|
|
|
|
|
throw new WrongUsageException('Source [' . $source . '] not downloaded or not locked, you should download it first !');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check source dir exist
|
2023-10-15 15:45:34 +08:00
|
|
|
$check = $lock[$source]['move_path'] === null ? (SOURCE_PATH . '/' . $source) : (SOURCE_PATH . '/' . $lock[$source]['move_path']);
|
2023-07-28 00:02:49 +08:00
|
|
|
if (!is_dir($check)) {
|
2023-10-15 15:45:34 +08:00
|
|
|
logger()->debug('Extracting source [' . $source . '] to ' . $check . ' ...');
|
2023-07-28 00:02:49 +08:00
|
|
|
FileSystem::extractSource($source, DOWNLOAD_PATH . '/' . ($lock[$source]['filename'] ?? $lock[$source]['dirname']), $lock[$source]['move_path']);
|
2023-10-15 15:45:34 +08:00
|
|
|
} else {
|
|
|
|
|
logger()->debug('Source [' . $source . '] already extracted in ' . $check . ', skip !');
|
2023-07-28 00:02:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|