1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\config;
15
16/**
17* Configuration container class
18*/
19class config implements \ArrayAccess, \IteratorAggregate, \Countable
20{
21	/**
22	* The configuration data
23	* @var array<string,string>
24	*/
25	protected $config;
26
27	/**
28	* Creates a configuration container with a default set of values
29	*
30	* @param array<string,string> $config The configuration data.
31	*/
32	public function __construct(array $config)
33	{
34		$this->config = $config;
35	}
36
37	/**
38	* Retrieves an ArrayIterator over the configuration values.
39	*
40	* @return \ArrayIterator An iterator over all config data
41	*/
42	public function getIterator()
43	{
44		return new \ArrayIterator($this->config);
45	}
46
47	/**
48	* Checks if the specified config value exists.
49	*
50	* @param  string $key The configuration option's name.
51	* @return bool        Whether the configuration option exists.
52	*/
53	public function offsetExists($key)
54	{
55		return isset($this->config[$key]);
56	}
57
58	/**
59	* Retrieves a configuration value.
60	*
61	* @param  string $key The configuration option's name.
62	* @return string      The configuration value
63	*/
64	public function offsetGet($key)
65	{
66		return (isset($this->config[$key])) ? $this->config[$key] : '';
67	}
68
69	/**
70	* Temporarily overwrites the value of a configuration variable.
71	*
72	* The configuration change will not persist. It will be lost
73	* after the request.
74	*
75	* @param string $key   The configuration option's name.
76	* @param string $value The temporary value.
77	*/
78	public function offsetSet($key, $value)
79	{
80		$this->config[$key] = $value;
81	}
82
83	/**
84	* Called when deleting a configuration value directly, triggers an error.
85	*
86	* @param string $key The configuration option's name.
87	*/
88	public function offsetUnset($key)
89	{
90		trigger_error('Config values have to be deleted explicitly with the \phpbb\config\config::delete($key) method.', E_USER_ERROR);
91	}
92
93	/**
94	* Retrieves the number of configuration options currently set.
95	*
96	* @return int Number of config options
97	*/
98	public function count()
99	{
100		return count($this->config);
101	}
102
103	/**
104	* Removes a configuration option
105	*
106	* @param  String $key       The configuration option's name
107	* @param  bool   $use_cache Whether this variable should be cached or if it
108	*                           changes too frequently to be efficiently cached
109	* @return null
110	*/
111	public function delete($key, $use_cache = true)
112	{
113		unset($this->config[$key]);
114	}
115
116	/**
117	* Sets a configuration option's value
118	*
119	* @param string $key       The configuration option's name
120	* @param string $value     New configuration value
121	* @param bool   $use_cache Whether this variable should be cached or if it
122	*                          changes too frequently to be efficiently cached.
123	*/
124	public function set($key, $value, $use_cache = true)
125	{
126		$this->config[$key] = $value;
127	}
128
129	/**
130	* Sets a configuration option's value only if the old_value matches the
131	* current configuration value or the configuration value does not exist yet.
132	*
133	* @param  string $key       The configuration option's name
134	* @param  string $old_value Current configuration value
135	* @param  string $new_value New configuration value
136	* @param  bool   $use_cache Whether this variable should be cached or if it
137	*                           changes too frequently to be efficiently cached.
138	* @return bool              True if the value was changed, false otherwise.
139	*/
140	public function set_atomic($key, $old_value, $new_value, $use_cache = true)
141	{
142		if (!isset($this->config[$key]) || $this->config[$key] == $old_value)
143		{
144			$this->config[$key] = $new_value;
145			return true;
146		}
147		return false;
148	}
149
150	/**
151	* Checks configuration option's value only if the new_value matches the
152	* current configuration value and the configuration value does exist.Called
153	* only after set_atomic has been called.
154	*
155	* @param  string $key       The configuration option's name
156	* @param  string $new_value New configuration value
157	* @throws \phpbb\exception\http_exception when config value is set and not equal to new_value.
158	* @return bool              True if the value was changed, false otherwise.
159	*/
160	public function ensure_lock($key, $new_value)
161	{
162		if (isset($this->config[$key]) && $this->config[$key] == $new_value)
163		{
164			return true;
165		}
166		throw new \phpbb\exception\http_exception(500, 'Failure while aqcuiring locks.');
167	}
168
169	/**
170	* Increments an integer configuration value.
171	*
172	* @param string $key       The configuration option's name
173	* @param int    $increment Amount to increment by
174	* @param bool   $use_cache Whether this variable should be cached or if it
175	*                          changes too frequently to be efficiently cached.
176	*/
177	function increment($key, $increment, $use_cache = true)
178	{
179		if (!isset($this->config[$key]))
180		{
181			$this->config[$key] = 0;
182		}
183
184		$this->config[$key] += $increment;
185	}
186}
187