PHP code example of brightnucleus / config

1. Go to this page and download the library: Download brightnucleus/config library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brightnucleus / config example snippets


 namespace BrightNucleus\Example;

/*
 * Example class main settings.
 */
$example = [
	'test_key' => 'test_value',
];

return [
	'BrightNucleus' => [
		'Example' => $example,
	],
];

 namespace BrightNucleus\Example;

use BrightNucleus\Config\ConfigInterface;
use BrightNucleus\Config\ConfigTrait;
use BrightNucleus\Exception\RuntimeException;

class Example {

	use ConfigTrait;

	/**
	 * Instantiate an Example object.
	 *
	 * @param ConfigInterface $config Config to parametrize the object.
	 * @throws RuntimeException       If the Config could not be parsed correctly.
	 */
	public function __construct( ConfigInterface $config ) {
		$this->processConfig( $config );
	}

	/**
	 * Do something.
	 */
	public function run() {
		$key = 'test_key';

		return sprintf(
		_( 'The value of the config key "$1%s" is "$2%s".'),
			$key,
			$this->getConfigKey( $key )
		);
	}
}

 namespace BrightNucleus\Example;

use BrightNucleus\Config\ConfigFactory;

function init() {
	$configFile = __DIR__ . '/config/example_settings.php';
	$config     = ConfigFactory::createSubConfig($configFile, 'BrightNucleus\Example');
	$example    = new Example( $config );

	// Outputs:
	// The value of the config key "test_key" is "test_value".
	echo $example->run();
}

 namespace BrightNucleus\Example;

use BrightNucleus\Config\ConfigInterface;
use BrightNucleus\Config\ConfigTrait;
use BrightNucleus\Exception\RuntimeException;

class Example {

	use ConfigTrait;

	/**
	 * Instantiate an Example object.
	 *
	 * For this constructor, the `$config` argument is optional, and the class will
	 * load a default configuration if none was injected.
	 *
	 * @param ConfigInterface|null $config Optional. Config to parametrize the object.
	 * @throws RuntimeException If the Config could not be parsed correctly.
	 */
	public function __construct( ConfigInterface $config = null ) {

	    // We either process the $config that was injected or fetch a default one.
		$this->processConfig( $config ?: $this->fetchDefaultConfig() );
	}

	/**
	 * Get the default configuration file name.
	 *
	 * This is used to override the default location.
	 *
	 * @return string Path & filename to the default configuration file.
	 */
	protected function getDefaultConfigFile() {
	    return __DIR__ . '/../config/my_default_config.php';
	}
}

 namespace BrightNucleus\Example;

/*
 * Example class main settings.
 */
$example = [
	'test_key' => 'override_value',
];

return [
	'BrightNucleus' => [
		'Example' => $example,
	],
];

 namespace BrightNucleus\Example;

use BrightNucleus\Config\ConfigFactory;

function init() {
	$configFile   = __DIR__ . '/config/example_settings.php';
	$overrideFile = __DIR__ . '/config/override_settings.php';
	$config       = ConfigFactory::merge($configFile, $overrideFile);
	$example      = new Example( $config );

	// Outputs:
	// Both files will be loaded, but values from the second override the first.
	// The value of the config key "test_key" is "override_value".
	echo $example->run();
}