stellarwp/configuration

StellarWP Configuration Accessor

dev-main 2024-01-13 00:32 UTC

This package is auto-updated.

Last update: 2024-04-13 04:31:09 UTC


README

Provides a system-wide set of configuration values. Easily access feature flags, and other immutable configurations.

Inspired by systems that load configurations from various sources, like retrieving .ini or .env values.

Setup

Add a configuration loader, so the system knows where to get the configuration values from.

// Constants_Provider.php
class Constants_Provider implements Configuration_Provider_Interface {

	public function has( $key ): bool {
		return defined( $key );
	}

	public function get( $key ) {
		if ( $this->has( $key ) ) {

			return constant( $key );
		}

		return null;
	}

	public function all(): array {
		return get_defined_constants( false );
	}
}
// Provider.php

class Provider {
	protected function register(): void {
		// Can add other loaders with other configuration values, such as local vs prod configurations.
		$loader = ( new Configuration_Loader() )->add( new Constants_Provider() );
		$this->configuration = new Configuration( $loader );
	}
}

This is an extensible loader to allow various configuration sources and application specific logic to bind configuration providers in different ways.

Retrieve Configuration Value

// wp-config.php
define('TEC_FEATURE_FLAG', true);
// Model.php
public function config_magic() {
	// Feature enabled?
	if ( $this->configuration->get( 'TEC_FEATURE_FLAG' ) ) {
		// do stuff...
	}
}