Skip to content

Commit

Permalink
Experimenting with making a single migration method that can be overr…
Browse files Browse the repository at this point in the history
…iden and values merged all the way through
  • Loading branch information
ingeniumed committed Feb 26, 2025
1 parent d16e8c1 commit d6e5201
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
11 changes: 11 additions & 0 deletions inc/Config/ArraySerializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,15 @@ protected static function get_implementor( array $config ): ?string {
* @inheritDoc
*/
abstract public static function get_config_schema(): array;

/**
* Migrates the config to the current schema version.
* Can be overridden by child classes to perform custom migrations.
*
* @param array<string, mixed> $config The config to migrate.
* @return array<string, mixed> The migrated config.
*/
protected static function migrate_config( array $config ): array|WP_Error {
return $config;
}
}
35 changes: 5 additions & 30 deletions inc/Config/DataSource/HttpDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final public function get_service_name(): string {
* define their own validation schema.
*/
public static function preprocess_config( array $config ): array|WP_Error {
$config = static::perform_migration( $config );
$config = static::migrate_config( $config );

$service_config = $config['service_config'] ?? [];
$validator = new Validator( static::get_service_config_schema() );
Expand All @@ -65,31 +65,6 @@ public static function preprocess_config( array $config ): array|WP_Error {
);
}

/**
* Performs a migration if the config is out of date.
*/
private static function perform_migration( array $config ): array {
// By default, we want to have an active data source.
if ( ! isset( $config['active'] ) ) {
$config['active'] = true;
}

if ( static::SERVICE_SCHEMA_VERSION === $config['service_config']['__version'] ) {
return $config;
}

$migrated_config = static::migrate_config( $config );

if ( is_wp_error( $migrated_config ) ) {
$config['active'] = false;
} else {
$config['service_config']['__version'] = static::SERVICE_SCHEMA_VERSION;
$config['active'] = true;
}

return $config;
}

public static function from_uuid( string $uuid ): DataSourceInterface|WP_Error {
$config = DataSourceCrud::get_config_by_uuid( $uuid );

Expand Down Expand Up @@ -134,10 +109,10 @@ protected static function map_service_config( array $service_config ): array {
}

/**
* Migrates the config to the current schema version.
* Can be overridden by child classes to perform custom migrations.
* @inheritDoc
*/
protected static function migrate_config( array $config ): array {
return $config;
protected static function migrate_config( array $config ): array|WP_Error {
return static::migrate_config( $config );
}

}

0 comments on commit d6e5201

Please sign in to comment.