Class Config

Config

extends Kohana_Config

Wrapper for configuration arrays. Multiple configuration readers can be attached to allow loading configuration from files, database, etc.

Configuration directives cascade across config sources in the same way that files cascade across the filesystem.

Directives from sources high in the sources list will override ones from those below them.

package
Kohana
category
Configuration
author
Kohana Team
copyright
(c) Kohana Team
license
https://koseven.ga/LICENSE.md


Information

This class is a transparent base class for Kohana_Config

Constants

  • None

Properties

Properties

protected $_groups

Default value:
array(0) 

protected $_sources

Default value:
array(0) 

Methods

public _write_config(string $group , string $key , mixed $value ) (defined in Kohana_Config)

Callback used by the config group to store changes made to configuration

Parameters

  • string $group required - Group name
  • string $key required - Variable name
  • mixed $value required - The new value

Return Values

  • Kohana_Config - Chainable instance

Source Code

public function _write_config($group, $key, $value)
{
	foreach ($this->_sources as $source)
	{
		if ( ! ($source instanceof Kohana_Config_Writer))
		{
			continue;
		}

		// Copy each value in the config
		$source->write($group, $key, $value);
	}

	return $this;
}

public attach(Kohana_Config_Source $source [, boolean $first = bool TRUE ] ) (defined in Kohana_Config)

Attach a configuration reader. By default, the reader will be added as the first used reader. However, if the reader should be used only when all other readers fail, use FALSE for the second parameter.

$config->attach($reader);        // Try first
$config->attach($reader, FALSE); // Try last

Parameters

  • Kohana_Config_Source $source required - Instance
  • boolean $first = bool TRUE - Add the reader as the first used object

Return Values

  • $this

Source Code

public function attach(Kohana_Config_Source $source, $first = TRUE)
{
	if ($first === TRUE)
	{
		// Place the log reader at the top of the stack
		array_unshift($this->_sources, $source);
	}
	else
	{
		// Place the reader at the bottom of the stack
		$this->_sources[] = $source;
	}

	// Clear any cached _groups
	$this->_groups = [];

	return $this;
}

public copy(string $group ) (defined in Kohana_Config)

Copy one configuration group to all of the other writers.

$config->copy($name);

Parameters

  • string $group required - Configuration group name

Return Values

  • $this

Source Code

public function copy($group)
{
	// Load the configuration group
	$config = $this->load($group);

	foreach ($config->as_array() as $key => $value)
	{
		$this->_write_config($group, $key, $value);
	}

	return $this;
}

public detach(Kohana_Config_Source $source ) (defined in Kohana_Config)

Detach a configuration reader.

$config->detach($reader);

Parameters

  • Kohana_Config_Source $source required - Instance

Return Values

  • $this

Source Code

public function detach(Kohana_Config_Source $source)
{
	if (($key = array_search($source, $this->_sources)) !== FALSE)
	{
		// Remove the writer
		unset($this->_sources[$key]);
	}

	return $this;
}

public load(string $group ) (defined in Kohana_Config)

Load a configuration group. Searches all the config sources, merging all the directives found into a single config group. Any changes made to the config in this group will be mirrored across all writable sources.

$array = $config->load($name);

See Kohana_Config_Group for more info

Parameters

  • string $group required - Configuration group name

Tags

Return Values

  • Kohana_Config_Group

Source Code

public function load($group)
{
	if ( ! count($this->_sources))
	{
		throw new Kohana_Exception('No configuration sources attached');
	}

	if (empty($group))
	{
		throw new Kohana_Exception("Need to specify a config group");
	}

	if ( ! is_string($group))
	{
		throw new Kohana_Exception("Config group must be a string");
	}

	if (strpos($group, '.') !== FALSE)
	{
		// Split the config group and path
		list($group, $path) = explode('.', $group, 2);
	}

	if (isset($this->_groups[$group]))
	{
		if (isset($path))
		{
			return Arr::path($this->_groups[$group], $path, NULL, '.');
		}
		return $this->_groups[$group];
	}

	$config = [];

	// We search from the "lowest" source and work our way up
	$sources = array_reverse($this->_sources);

	foreach ($sources as $source)
	{
		if ($source instanceof Kohana_Config_Reader)
		{
			if ($source_config = $source->load($group))
			{
				$config = Arr::merge($config, $source_config);
			}
		}
	}

	$this->_groups[$group] = new Config_Group($this, $group, $config);

	if (isset($path))
	{
		return Arr::path($config, $path, NULL, '.');
	}

	return $this->_groups[$group];
}

Do you want to contribute to Koseven?

We need YOUR help!

This project is open source. What does this mean? YOU can help:
  • Found a bug? Report it on Github
  • Need a feature? Add it Here
  • Want to help? Join the Forum
Go to Github