Repository navigation
Configuration
← Collection Component | Container Component →
The Configuration class serves as a storage for configuration data. It supports dot-notation key access for nested values and can also load from PHP files in a directory. It is primarily used in conjunction with IntegrationInterface implementations to pass configuration values for bootstrapping.
The Configuration class accepts data in three ways:
- As an array - the constructor accepts an associative array directly
-
As a string - the constructor treats a string argument as a directory path and calls
load()internally -
As null - creates an empty configuration that can be populated later via
set()
Values are retrieved using get() which supports dot notation for accessing nested keys. When a key does not exist, get() returns the provided default value, or null if no default is specified.
The set() method stores a value at a given key, supporting the same dot notation. If the third argument $fromFile is set to true, the value is treated as a file path and loaded via require.
The load() method scans a directory for *.php files and merges them into the configuration, where each file's basename (without .php) becomes a top-level key.
// app/web/index.php
use Rougin\Slytherin\Integration\Configuration;
$data = array('app' => array());
$data['app']['name'] = 'My Application';
$data['app']['debug'] = true;
$database = array('name' => 'test');
$database['username'] = 'root';
$database['password'] = 'secret';
$data['database'] = $database;
$config = new Configuration($data);
// Access top-level keys -----------
$appName = $config->get('app.name'); // 'My Application'
// ---------------------------------
// Access nested keys with dot notation ----
$dbUser = $config->get('database.username'); // 'root'
// -----------------------------------------
// Provide a default value for missing keys ---
$dbPort = $config->get('database.port', 3306); // 3306
// --------------------------------------------// app/web/index.php
use Rougin\Slytherin\Integration\Configuration;
// Assuming a directory "config/" with:
// config/app.php → array('name' => 'My App')
// config/database.php → array('host' => 'localhost', 'name' => 'test')
$config = new Configuration;
$config->load(__DIR__ . '/config');
$appName = $config->get('app.name'); // 'My App'
$dbHost = $config->get('database.host'); // 'localhost'// app/web/index.php
use Rougin\Slytherin\Application;
use Rougin\Slytherin\Integration\Configuration;
use Rougin\Slytherin\Routing\Router;
use Rougin\Slytherin\Routing\RoutingIntegration;
$config = new Configuration;
// Set a custom router instance ----
$router = new Router;
$router->get('/', function ()
{
return 'Hello world!';
});
$config->set('app.router', $router);
// ---------------------------------
$app = new Application;
// Pass the configuration when integrating ------
$app->integrate(new RoutingIntegration, $config);
// ----------------------------------------------
$app->run();namespace Rougin\Slytherin\Integration;
class Configuration
{
/**
* @param array<string, mixed>|string|null $data
*/
public function __construct($data = null);
/**
* Returns the value from the specified key.
*
* @param string $key
* @param mixed|null $default
* @return mixed
*/
public function get($key, $default = null);
/**
* Loads the configuration from a specified directory.
*
* @param string $directory
* @return array<string, mixed>
*/
public function load($directory);
/**
* Sets the value to the specified key.
*
* @param string $key
* @param mixed $value
* @param boolean $fromFile
* @return self
*/
public function set($key, $value, $fromFile = false);
}