模块

Config
继承自 BootPHP_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
BootPHP
category
配置
author
Tinsh
copyright
© 2005-2016 Kilofox Studio

该类在 SYSPATH/classes/config.php 第 5 行声明。

属性

protected $_sources

方法

public _write_config( string $group , string $key , mixed $value ) (在 BootPHP_Config 中定义)

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

参数

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

返回值

  • BootPHP_Config - Chainable instance

源代码

public function _write_config($group, $key, $value)
{
	foreach ($this->_sources as $source)
	{
		if (!($source instanceof BootPHP_Config_Writer))
		{
			continue;
		}
		// Copy each value in the config
		$source->write($group, $key, $value);
	}
	return $this;
}

public attach( object $source [, boolean $first = bool true ] ) (在 BootPHP_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

参数

  • object $source required - BootPHP_Config_Source instance
  • boolean $first = bool true - Add the reader as the first used object

返回值

  • $this

源代码

public function attach($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;
	}
	return $this;
}

public copy( string $group ) (在 BootPHP_Config 中定义)

Copy one configuration group to all of the other writers.

$config->copy($name);

参数

  • string $group required - Configuration group name

返回值

  • $this

源代码

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( object $source ) (在 BootPHP_Config 中定义)

Detach a configuration reader.

$config->detach($reader);

参数

  • object $source required - BootPHP_Config_Source instance

返回值

  • $this

源代码

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

public load( string $group ) (在 BootPHP_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 BootPHP_Config_Group for more info

参数

  • string $group required - Configuration group name

Tags

返回值

  • object - BootPHP_Config_Group

源代码

public function load($group)
{
	if (!count($this->_sources))
	{
		throw new BootPHP_Exception('No configuration sources attached');
	}
	if (empty($group))
	{
		throw new BootPHP_Exception('Need to specify a config group');
	}
	if (!is_string($group))
	{
		throw new BootPHP_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 = array();
	// We search from the "lowest" source and work our way up
	$sources = array_reverse($this->_sources);
	foreach ($sources as $source)
	{
		if ($source instanceof Config_File)
		{
			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];
}