stellarwp / configuration
StellarWP 配置访问器
dev-main
2024-01-13 00:32 UTC
Requires
- php: >=7.0.0
This package is auto-updated.
Last update: 2024-09-13 05:22:40 UTC
README
提供一套系统级的配置值。轻松访问功能标志和其他不可变配置。
受从各种来源加载配置的系统启发,例如检索 .ini
或 .env
值。
设置
添加配置加载器,以便系统知道从哪里获取配置值。
// 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 ); } }
这是一个可扩展的加载器,允许以不同的方式绑定配置提供程序,以支持各种配置来源和应用特定逻辑。
检索配置值
// 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... } }