Skip to content

Commit

Permalink
refactor: unify loading logic and use filters to modify file path or …
Browse files Browse the repository at this point in the history
…config data
  • Loading branch information
sjinks committed Jan 21, 2025
1 parent 7ed5e81 commit 98f8188
Showing 1 changed file with 25 additions and 46 deletions.
71 changes: 25 additions & 46 deletions integrations/integration-vip-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ public function __construct( string $slug ) {
* @param string $slug A unique identifier for the integration.
*/
private function set_config( string $slug ): void {
if ( defined( 'VIP_GO_APP_ENVIRONMENT' ) && 'local' === constant( 'VIP_GO_APP_ENVIRONMENT' ) ) {
$config = $this->get_local_config_from_file( $slug );
} else {
$config = $this->get_vip_config_from_file( $slug );
}
$config = $this->get_vip_config_from_file( $slug );

if ( ! is_array( $config ) ) {
return;
Expand All @@ -86,51 +82,34 @@ protected function get_vip_config_from_file( string $slug ) {
? constant( 'WPVIP_INTEGRATIONS_CONFIG_DIR' )
: ABSPATH . 'config/integrations-config';
$config_file_name = $slug . '-config.php';
$config_file_path = $config_file_directory . '/' . $config_file_name;

/**
* Clear cache to always read data from latest config file.
*
* Kubernetes ConfigMap updates the file via symlink instead of actually replacing the file and
* PHP cache can hold a reference to the old symlink that can cause fatal if we use require
* on it.
*/
clearstatcache( true, $config_file_directory . '/' . $config_file_name );
// Clears cache for files created by k8s ConfigMap.
clearstatcache( true, $config_file_directory . '/..data' );
clearstatcache( true, $config_file_directory . '/..data/' . $config_file_name );

if ( ! is_readable( $config_file_path ) ) {
return null;
}

return require $config_file_path;
}
$config_file_path_orig = $config_file_directory . '/' . $config_file_name;

$config_file_path = apply_filters( 'vip_integrations_config_file_path', $config_file_path_orig, $slug );
$config_data = apply_filters( 'vip_integrations_pre_load_config', null, $config_file_path, $slug );

if ( is_null( $config_data ) ) {
if ( $config_file_path_orig === $config_file_path ) {
/**
* Clear cache to always read data from latest config file.
*
* Kubernetes ConfigMap updates the file via symlink instead of actually replacing the file and
* PHP cache can hold a reference to the old symlink that can cause fatal if we use require
* on it.
*/
clearstatcache( true, $config_file_directory . '/' . $config_file_name );
// Clears cache for files created by k8s ConfigMap.
clearstatcache( true, $config_file_directory . '/..data' );
clearstatcache( true, $config_file_directory . '/..data/' . $config_file_name );
}

/**
* Get config for local dev env and Codespaces
*
* @param string $slug A unique identifier for the integration.
*
* @return null|mixed
*/
protected function get_local_config_from_file( string $slug ) {
$config_file = '/app/integrations.json';
if ( ! file_exists( $config_file ) ) {
return null;
}
if ( ! is_readable( $config_file_path ) ) {
return null;
}

// phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
$config = json_decode( file_get_contents( $config_file ), true );
if ( ! isset( $config[ $slug ] ) ) {
return null;
$config_data = require $config_file_path;
}

// We don't really care about the org config in local dev.
// Network config might be tricky if there's mismatch between data.
return [
'env' => $config[ $slug ],
];
return $config_data;
}

/**
Expand Down

0 comments on commit 98f8188

Please sign in to comment.