2023-03-18 17:32:21 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\command;
|
|
|
|
|
|
|
|
|
|
use SPC\exception\FileSystemException;
|
|
|
|
|
use SPC\exception\ValidationException;
|
|
|
|
|
use SPC\store\FileSystem;
|
|
|
|
|
use SPC\util\ConfigValidator;
|
2023-04-22 17:45:43 +08:00
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
2023-03-18 17:32:21 +08:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改 config 后对其 kv 进行排序的操作
|
|
|
|
|
*/
|
2023-04-22 17:45:43 +08:00
|
|
|
#[AsCommand('sort-config', 'After config edited, sort it by alphabet')]
|
2023-03-18 17:32:21 +08:00
|
|
|
class SortConfigCommand extends BaseCommand
|
|
|
|
|
{
|
|
|
|
|
public function configure()
|
|
|
|
|
{
|
|
|
|
|
$this->addArgument('config-name', InputArgument::REQUIRED, 'Your config to be sorted, you can sort "lib", "source" and "ext".');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws ValidationException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
2023-04-22 17:45:43 +08:00
|
|
|
public function handle(): int
|
2023-03-18 17:32:21 +08:00
|
|
|
{
|
2023-04-22 17:45:43 +08:00
|
|
|
switch ($name = $this->getArgument('config-name')) {
|
2023-03-18 17:32:21 +08:00
|
|
|
case 'lib':
|
|
|
|
|
$file = json_decode(FileSystem::readFile(ROOT_DIR . '/config/lib.json'), true);
|
|
|
|
|
ConfigValidator::validateLibs($file);
|
|
|
|
|
ksort($file);
|
|
|
|
|
file_put_contents(ROOT_DIR . '/config/lib.json', json_encode($file, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
|
|
|
break;
|
|
|
|
|
case 'source':
|
|
|
|
|
$file = json_decode(FileSystem::readFile(ROOT_DIR . '/config/source.json'), true);
|
|
|
|
|
ConfigValidator::validateSource($file);
|
|
|
|
|
ksort($file);
|
|
|
|
|
file_put_contents(ROOT_DIR . '/config/source.json', json_encode($file, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
|
|
|
break;
|
|
|
|
|
case 'ext':
|
|
|
|
|
$file = json_decode(FileSystem::readFile(ROOT_DIR . '/config/ext.json'), true);
|
|
|
|
|
ConfigValidator::validateExts($file);
|
|
|
|
|
ksort($file);
|
|
|
|
|
file_put_contents(ROOT_DIR . '/config/ext.json', json_encode($file, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-04-22 17:45:43 +08:00
|
|
|
$this->output->writeln("<error>invalid config name: {$name}</error>");
|
2023-03-18 17:32:21 +08:00
|
|
|
return 1;
|
|
|
|
|
}
|
2023-04-22 17:45:43 +08:00
|
|
|
$this->output->writeln('<info>sort success</info>');
|
2023-03-18 17:32:21 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|