Skip to content

Latest commit

 

History

History
87 lines (64 loc) · 2.25 KB

configurable-objects.md

File metadata and controls

87 lines (64 loc) · 2.25 KB

Configurable Objects

Mixin

Configurable objects (which could have been called "Config Aware") can have an associated Config object that can help define various properties, states, or other.

API

The trait provides three methods: config(), setConfig(), and createConfig().

  • config() — Retrieve the Config object or the value of a given key.

    public ConfigurableTrait::config ( void ) : ConfigInterface
    public ConfigurableTrait::config ( string $key [, mixed $default = null ] ) : mixed
  • setConfig() — Assign the given instance of ConfigInterface . Otherwise, create a new Config object with the given associative array or the config file to import.

    public ConfigurableTrait::setConfig ( ConfigInterface $config ) : self
    public ConfigurableTrait::setConfig ( mixed $data ) : self
  • createConfig() — Create a new Config object with. Optionally, merge the data from the given instance of ConfigInterface , the associative array, or from the config file to import.

    protected ConfigurableTrait::createConfig ( [ mixed $data = null ] ) : ConfigInterface

Examples

Example #1: Implementation of the mixin

namespace Acme;

use Acme\Config;
use Charcoal\Config\ConfigurableInterface;
use Charcoal\Config\ConfigurableTrait;

class App implements ConfigurableInterface
{
    use ConfigurableTrait;

	/**
	 * @param  mixed $data Initial data; a filepath, an array, or another Config object.
	 * @return Config
	 */
    public function createConfig($data = null)
    {
        $cfg = new Config($data);
        if ($data !== null) {
            $cfg->merge($data);
        }
        return $cfg;
    }
}

The class above could be used as such:

use Acme\App;

$foo = new App();
$foo->setConfig([
    'foo' => [
        'baz' => 42,
        'qux' => null,
    ]
]);

$cfg = $foo->config();
echo $cfg['foo.baz']; // Returns 42
echo $foo->config('foo.baz'); // Also, 42
echo $foo->config('foo.qux', -1); // Returns -1