Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a migration feature #366

Draft
wants to merge 12 commits into
base: trunk
Choose a base branch
from
16 changes: 16 additions & 0 deletions inc/Config/ArraySerializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ final public static function from_array( array $config, ?ValidatorInterface $val
return $subclass::from_array( $config, $validator );
}

$config = static::migrate_config( $config );
if ( is_wp_error( $config ) ) {
return $config;
}

$config = static::preprocess_config( $config );
if ( is_wp_error( $config ) ) {
return $config;
Expand Down Expand Up @@ -91,4 +96,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.
*/
public static function migrate_config( array $config ): array|WP_Error {
return $config;
}
}
8 changes: 8 additions & 0 deletions inc/Config/ArraySerializableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public static function preprocess_config( array $config ): array|WP_Error;
*/
public static function get_config_schema(): array;

/**
* Migrates the config to the current schema version.
*
* @param array<string, mixed> $config The config to migrate.
* @return array<string, mixed> The migrated config.
*/
public static function migrate_config( array $config ): array|WP_Error;

/**
* Converts the current object to an array representation.
*
Expand Down
7 changes: 7 additions & 0 deletions inc/Config/DataSource/HttpDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,11 @@ protected static function map_service_config( array $service_config ): array {
'request_headers' => $service_config['request_headers'] ?? [],
];
}

/**
* @inheritDoc
*/
public static function migrate_config( array $config ): array|WP_Error {
return $config;
}
}
Loading