mirror of
https://github.com/phpbb/phpbb.git
synced 2025-02-25 12:33:29 +01:00
There is a phpbb_config class which simply holds an array and does not persist any data. It implements ArrayAccess, Countable and IteratorAggregate to allow regular use of configuration as if it was still an array. The phpbb_config_db class depends on an instance of the dbal and a cache driver. It obtains the configuration data from cache and database as necessary and persists data to the database. The functions set_config and set_config_count remain for backward compatability but they only call methods on the new config class now instead of directly manipulating the database and cache. PHPBB3-9988
158 lines
3.8 KiB
PHP
158 lines
3.8 KiB
PHP
<?php
|
|
/**
|
|
*
|
|
* @package phpBB3
|
|
* @copyright (c) 2010 phpBB Group
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
if (!defined('IN_PHPBB'))
|
|
{
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Configuration container class
|
|
* @package phpBB3
|
|
*/
|
|
class phpbb_config implements ArrayAccess, IteratorAggregate, Countable
|
|
{
|
|
/**
|
|
* The configuration data
|
|
* @var array(string => string)
|
|
*/
|
|
protected $config;
|
|
|
|
/**
|
|
* Creates a configuration container with a default set of values
|
|
*
|
|
* @param array(string => string) $config The configuration data.
|
|
*/
|
|
public function __construct(array $config)
|
|
{
|
|
$this->config = $config;
|
|
}
|
|
|
|
/**
|
|
* Retrieves an ArrayIterator over the configuration values.
|
|
*
|
|
* @return ArrayIterator An iterator over all config data
|
|
*/
|
|
public function getIterator()
|
|
{
|
|
return new ArrayIterator($this->config);
|
|
}
|
|
|
|
/**
|
|
* Checks if the specified config value exists.
|
|
*
|
|
* @param string $key The configuration option's name.
|
|
* @return bool Whether the configuration option exists.
|
|
*/
|
|
public function offsetExists($key)
|
|
{
|
|
return isset($this->config[$key]);
|
|
}
|
|
|
|
/**
|
|
* Retrieves a configuration value.
|
|
*
|
|
* @param string $key The configuration option's name.
|
|
* @return string The configuration value
|
|
*/
|
|
public function offsetGet($key)
|
|
{
|
|
return (isset($this->config[$key])) ? $this->config[$key] : '';
|
|
}
|
|
|
|
/**
|
|
* Temporarily overwrites the value of a configuration variable.
|
|
*
|
|
* The configuration change will not persist. It will be lost
|
|
* after the request.
|
|
*
|
|
* @param string $key The configuration option's name.
|
|
* @param string $value The temporary value.
|
|
*/
|
|
public function offsetSet($key, $value)
|
|
{
|
|
$this->config[$key] = $value;
|
|
}
|
|
|
|
/**
|
|
* Called when deleting a configuration value directly, triggers an error.
|
|
*
|
|
* @param string $key The configuration option's name.
|
|
*/
|
|
public function offsetUnset($key)
|
|
{
|
|
trigger_error('Config values have to be deleted explicitly with the phpbb_config::delete($key) method.', E_USER_ERROR);
|
|
}
|
|
|
|
/**
|
|
* Retrieves the number of configuration options currently set.
|
|
*
|
|
* @return int Number of config options
|
|
*/
|
|
public function count()
|
|
{
|
|
return count($this->config);
|
|
}
|
|
|
|
/**
|
|
* Sets a configuration option's value
|
|
*
|
|
* @param string $key The configuration option's name
|
|
* @param string $value New configuration value
|
|
* @param bool $cache Whether this variable should be cached or if it
|
|
* changes too frequently to be efficiently cached.
|
|
*/
|
|
public function set($key, $value, $cache = true)
|
|
{
|
|
$this->config[$key] = $value;
|
|
}
|
|
|
|
/**
|
|
* Sets a configuration option's value only if the old_value matches the
|
|
* current configuration value or the configuration value does not exist yet.
|
|
*
|
|
* @param string $key The configuration option's name
|
|
* @param string $old_value Current configuration value
|
|
* @param string $value New configuration value
|
|
* @param bool $cache Whether this variable should be cached or if it
|
|
* changes too frequently to be efficiently cached.
|
|
* @return bool True if the value was changed, false otherwise.
|
|
*/
|
|
public function set_atomic($key, $old_value, $value, $cache = true)
|
|
{
|
|
if (!isset($this->config[$key]) || $this->config[$key] == $old_value)
|
|
{
|
|
$this->config[$key] = $value;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Increments an integer configuration value.
|
|
*
|
|
* @param string $key The configuration option's name
|
|
* @param int $increment Amount to increment by
|
|
* @param bool $cache Whether this variable should be cached or if it
|
|
* changes too frequently to be efficiently cached.
|
|
*/
|
|
function increment($key, $increment, $cache = true)
|
|
{
|
|
if (!isset($this->config[$key]))
|
|
{
|
|
$this->config[$key] = 0;
|
|
}
|
|
|
|
$this->config[$key] += $increment;
|
|
}
|
|
}
|