From 92a64b316ca4c8441bc57cb32cfc5a3e9df659c3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Jul 2024 12:26:08 +0200 Subject: [PATCH] Refactor and cleanup --- .../DevTools/src/RefactorConfigCommand.php | 107 +++++++++++------- 1 file changed, 66 insertions(+), 41 deletions(-) diff --git a/monorepo/DevTools/src/RefactorConfigCommand.php b/monorepo/DevTools/src/RefactorConfigCommand.php index d4d7a7ca64c..927b26d5a26 100644 --- a/monorepo/DevTools/src/RefactorConfigCommand.php +++ b/monorepo/DevTools/src/RefactorConfigCommand.php @@ -26,63 +26,81 @@ /** * @internal This class is internal to the hydephp/develop monorepo. + * * @experimental https://github.com/hydephp/develop/pull/1833 */ class RefactorConfigCommand extends Command { + protected const SUPPORTED_FORMATS = ['yaml']; + /** @var string */ protected $signature = 'refactor:config {format : The new configuration format}'; /** @var string */ protected $description = 'Migrate the configuration to a different format.'; - protected const FORMATS = ['yaml']; - public function handle(): int { $format = $this->argument('format'); - if (! in_array($format, self::FORMATS)) { - $this->error('Invalid format. Supported formats: '.implode(', ', self::FORMATS)); + if (! in_array($format, self::SUPPORTED_FORMATS)) { + $this->error('Invalid format. Supported formats: '.implode(', ', self::SUPPORTED_FORMATS)); return 1; } - $this->gray(' > Migrating configuration to '.$format); + $this->gray(" > Migrating configuration to $format"); - match ($format) { + return match ($format) { 'yaml' => $this->migrateToYaml(), }; + } + + protected function migrateToYaml(): int + { + $this->ensureYamlConfigDoesNotExist(); + + $config = $this->getConfigDiff(); + + if (empty($config)) { + $this->warn("You don't seem to have any configuration to migrate."); + + return 0; + } + + $serializedConfig = $this->serializePhpData($config); + $yaml = $this->dumpConfigToYaml($serializedConfig); + + file_put_contents(Hyde::path('hyde.yml'), $yaml); $this->info('All done!'); return 0; } - protected function migrateToYaml(): void + protected function ensureYamlConfigDoesNotExist(): void { if (file_exists(Hyde::path('hyde.yml')) || file_exists(Hyde::path('hyde.yaml'))) { throw new RuntimeException('Configuration already exists in YAML format.'); } + } + protected function getConfigDiff(): array + { $config = config('hyde'); - $default = require Hyde::vendorPath('config/hyde.php'); - // Todo: Add argument to not diff out defaults - $config = $this->diffConfig($config, $default); - - $config = $this->serializePhpData($config); - - $yaml = Yaml::dump($config, 16, 4, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE); - - if ($yaml === '[]') { - $this->warn("You don't seem to have any configuration to migrate."); - return; - } + return $this->diffConfig($config, $default); + } - file_put_contents(Hyde::path('hyde.yml'), $yaml); + protected function dumpConfigToYaml(array $config): string + { + return Yaml::dump($config, 16, 4, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE); } + /** + * @param array $config + * @return array + */ protected function serializePhpData(array $config): array { return collect($config)->mapWithKeys(function ($value, $key) { @@ -94,6 +112,11 @@ protected function serializePhpData(array $config): array })->toArray(); } + /** + * @param mixed $value + * @param string|int $key + * @return array + */ protected function serializePhpValue(mixed $value, string|int $key): array { if ($value instanceof Feature) { @@ -105,45 +128,47 @@ protected function serializePhpValue(mixed $value, string|int $key): array } if ($value instanceof MetadataElementContract) { - // We don't have deserialization logic for this (yet?) return [$key => $value->__toString()]; } if ($value instanceof PostAuthor) { - // Not fully supported in v1 - return [$key => [ - 'username' => $value->username, - 'name' => $value->name, - 'website' => $value->website, - ]]; + return [$key => $this->serializePostAuthor($value)]; } return [$key => $value]; } - // Remove any default values from the config by iterating the root array keys and comparing the values + protected function serializePostAuthor(PostAuthor $author): array + { + return [ + 'username' => $author->username, + 'name' => $author->name, + 'website' => $author->website, + ]; + } + + /** + * @param array $config + * @param array $default + * @return array + */ protected function diffConfig(array $config, array $default): array { - $new = []; + $diff = []; foreach ($config as $key => $value) { - if (is_array($value) && isset($default[$key])) { - if ($value === $default[$key]) { - continue; - } + if (! isset($default[$key]) || $value != $default[$key]) { + $diff[$key] = $value; } - - // Loose comparison - if (isset($default[$key]) && $value == $default[$key]) { - continue; - } - - $new[$key] = $value; } - return $this->arrayFilterRecurse($new); + return $this->arrayFilterRecurse($diff); } + /** + * @param array $input + * @return array + */ protected function arrayFilterRecurse(array $input): array { foreach ($input as $key => &$value) {