L O A D I N G
Edit this page

Config Component

Introduction

Config is a component that allows to find, load, combine, autofill and validate configuration values of your application.

Features

Config features:

  • Support for accessing configuration values using dot-notation
  • Support for isolating, merging and replacing multiple configurations
  • Built-in overwrite handlers
  • Kraken Framework compatibility

Examples

This section contains examples and patterns that can be used with described component.

Reading Configuration Files

This example shows how configuration file can be read from file with a help from Kraken\Filesystem component.

$path = __DIR__ . '/storage';
$file = 'config.php';

$adapterFactory = new FilesystemAdapterFactory();
$configFactory  = new ConfigFactory(
    new Filesystem(
        $adapterFactory->create('Local', [ [ 'path' => $path ] ])
    ),
    [ '#' . $file . '#si' ]
);

$config = $configFactory->create();

Reading Values

This example show how configuration values can be read from Config instance using dot notation.

$value = $config->get('someOption.subOption.aKey', $defaultValue);

Writing Values

$config->set('someOption.subOption.aKey', 'anotherValue');

Removing Values

$config->remove($key = 'someOption.subOption.aKey');
$value = $config->get($key); // returns null since this value is unset

Merging Configuration Sets

$config->merge($newOptions = [
  'someOptions.option1' => 'value1',
  'someOptions.option2' => 'value2'
]);

Using Overwrite Method

Using overwrite method allows you to define how given configuration set is going to be merged.

There are several options available:

  • Isolation
  • Merge
  • Replace
  • Reverse Isolation
  • Reverse Merge
  • Reverse Replace

This is how you can use them on a fly:

$config->merge($newOptions, new Overwrite\OverwriteReplacer);

Alternatively Kraken allows you to set overwrite method as default, so you won't have to provide it on each merge call.

$config->setOverwriteHandler(new Overwrite\OverwriteReplacer);

Important Classes & Interfaces

This section contains list of most important classes and interfaces shipped with this component. It does not include all classes and interface.

Config

class Config implements ConfigInterface

Config is implementation of configuration-aware controller that allows writing and reading configuration using dot-notation.

ConfigInterface

interface ConfigInterface extends ConfigReaderInterface, ConfigWriterInterface

ConfigFactory

class ConfigFactory extends SimpleFactory implements ConfigFactoryInterface

ConfigFactory is a specially configured factory that allows creation of Config instances from .php configuration files.

ConfigFactoryInterface

interface ConfigFactoryInterface extends SimpleFactoryInterface
Start from the Beginning