Skip to content

Commit

Permalink
Parse data
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jul 12, 2024
1 parent a60df99 commit 03648c8
Showing 1 changed file with 66 additions and 8 deletions.
74 changes: 66 additions & 8 deletions monorepo/DevTools/src/RefactorConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@

namespace Hyde\MonorepoDevTools;

use Enum;
use UnitEnum;
use Hyde\Hyde;
use BackedEnum;
use Hyde\Enums\Feature;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
use Hyde\Console\Concerns\Command;

use Hyde\Framework\Features\Blogging\Models\PostAuthor;
use Hyde\Framework\Features\Metadata\MetadataElementContract;

use function copy;
use function config;
use function substr;
use function collect;
use function gettype;
use function implode;
use function in_array;
use function is_array;
use function is_string;
use function file_exists;
use function str_starts_with;
use function file_put_contents;

/**
Expand All @@ -26,13 +44,12 @@ class RefactorConfigCommand extends Command
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::FORMATS)) {
$this->error('Invalid format. Supported formats: ' . implode(', ', self::FORMATS));
return 1;
}

$this->gray(' > Migrating configuration to '.$format);
$this->gray(' > Migrating configuration to ' . $format);

match ($format) {
'yaml' => $this->migrateToYaml(),
Expand All @@ -45,14 +62,55 @@ public function handle(): int

protected function migrateToYaml(): void
{
// todo if file exists, add backup
if (file_exists(Hyde::path('hyde.yml')) && !file_exists(Hyde::path('hyde.yml.bak'))) {
copy(Hyde::path('hyde.yml'), Hyde::path('hyde.yml.bak'));
}

$config = config('hyde');
$config = $this->serializePhpData($config);

$yaml = Yaml::dump($config);
$yaml = Yaml::dump($config, 16, 4, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);

// todo diff out defaults?
// Todo: Diff out defaults? (unless with argument)

file_put_contents(Hyde::path('hyde.yml'), $yaml);
}

protected function serializePhpData(array $config): array
{
return collect($config)->mapWithKeys(function ($value, $key) {
if (is_array($value)) {
return [$key => $this->serializePhpData($value)];
}

return $this->serializePhpValue($value, $key);
})->toArray();
}

protected function serializePhpValue(mixed $value, string|int $key): array
{
if ($value instanceof Feature) {
return [$key => Str::kebab($value->name)];
}

if (is_string($key) && str_starts_with($key, 'Hyde\Pages\\')) {
return [Str::kebab(substr($key, 11)) => $value];
}

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 => $value];
}
}

0 comments on commit 03648c8

Please sign in to comment.