Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
18 / 19 |
|
91.67% |
11 / 12 |
CRAP | |
0.00% |
0 / 1 |
| config | |
94.74% |
18 / 19 |
|
91.67% |
11 / 12 |
18.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| offsetExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| offsetGet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| offsetSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| offsetUnset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set_atomic | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| ensure_lock | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| increment | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | |
| 14 | namespace phpbb\config; |
| 15 | |
| 16 | /** |
| 17 | * Configuration container class |
| 18 | */ |
| 19 | class config implements \ArrayAccess, \IteratorAggregate, \Countable |
| 20 | { |
| 21 | /** |
| 22 | * The configuration data |
| 23 | * @var array<string,int|string> |
| 24 | */ |
| 25 | protected $config; |
| 26 | |
| 27 | /** |
| 28 | * Creates a configuration container with a default set of values |
| 29 | * |
| 30 | * @param array<string,int|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(): \ArrayIterator |
| 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 | #[\ReturnTypeWillChange] |
| 54 | public function offsetExists($key) |
| 55 | { |
| 56 | return isset($this->config[$key]); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Retrieves a configuration value. |
| 61 | * |
| 62 | * @param string $key The configuration option's name. |
| 63 | * @return int|string The configuration value |
| 64 | */ |
| 65 | #[\ReturnTypeWillChange] |
| 66 | public function offsetGet($key) |
| 67 | { |
| 68 | return (isset($this->config[$key])) ? $this->config[$key] : ''; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Temporarily overwrites the value of a configuration variable. |
| 73 | * |
| 74 | * The configuration change will not persist. It will be lost |
| 75 | * after the request. |
| 76 | * |
| 77 | * @param string $offset The configuration option's name. |
| 78 | * @param int|string $value The temporary value. |
| 79 | */ |
| 80 | #[\ReturnTypeWillChange] |
| 81 | public function offsetSet($offset, $value) |
| 82 | { |
| 83 | $this->config[$offset] = $value; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Called when deleting a configuration value directly, triggers an error. |
| 88 | * |
| 89 | * @param string $offset The configuration option's name. |
| 90 | */ |
| 91 | #[\ReturnTypeWillChange] |
| 92 | public function offsetUnset($offset): never |
| 93 | { |
| 94 | trigger_error('Config values have to be deleted explicitly with the \phpbb\config\config::delete($key) method.', E_USER_ERROR); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Retrieves the number of configuration options currently set. |
| 99 | * |
| 100 | * @return int Number of config options |
| 101 | */ |
| 102 | public function count(): int |
| 103 | { |
| 104 | return count($this->config); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Removes a configuration option |
| 109 | * |
| 110 | * @param String $key The configuration option's name |
| 111 | * @param bool $use_cache Whether this variable should be cached or if it |
| 112 | * changes too frequently to be efficiently cached |
| 113 | * @return void |
| 114 | */ |
| 115 | public function delete($key, $use_cache = true) |
| 116 | { |
| 117 | unset($this->config[$key]); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Sets a configuration option's value |
| 122 | * |
| 123 | * @param string $key The configuration option's name |
| 124 | * @param int|string $value New configuration value |
| 125 | * @param bool $use_cache Whether this variable should be cached or if it |
| 126 | * changes too frequently to be efficiently cached. |
| 127 | */ |
| 128 | public function set($key, $value, $use_cache = true) |
| 129 | { |
| 130 | $this->config[$key] = $value; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Sets a configuration option's value only if the old_value matches the |
| 135 | * current configuration value or the configuration value does not exist yet. |
| 136 | * |
| 137 | * @param string $key The configuration option's name |
| 138 | * @param int|string $old_value Current configuration value |
| 139 | * @param int|string $new_value New configuration value |
| 140 | * @param bool $use_cache Whether this variable should be cached or if it |
| 141 | * changes too frequently to be efficiently cached. |
| 142 | * @return bool True if the value was changed, false otherwise. |
| 143 | */ |
| 144 | public function set_atomic($key, $old_value, $new_value, $use_cache = true) |
| 145 | { |
| 146 | if (!isset($this->config[$key]) || $this->config[$key] == $old_value) |
| 147 | { |
| 148 | $this->config[$key] = $new_value; |
| 149 | return true; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Checks configuration option's value only if the new_value matches the |
| 156 | * current configuration value and the configuration value does exist.Called |
| 157 | * only after set_atomic has been called. |
| 158 | * |
| 159 | * @param string $key The configuration option's name |
| 160 | * @param int|string $new_value New configuration value |
| 161 | * @throws \phpbb\exception\http_exception when config value is set and not equal to new_value. |
| 162 | * @return bool True if the value was changed, false otherwise. |
| 163 | */ |
| 164 | public function ensure_lock($key, $new_value) |
| 165 | { |
| 166 | if (isset($this->config[$key]) && $this->config[$key] == $new_value) |
| 167 | { |
| 168 | return true; |
| 169 | } |
| 170 | throw new \phpbb\exception\http_exception(500, 'Failure while aqcuiring locks.'); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Increments an integer configuration value. |
| 175 | * |
| 176 | * @param string $key The configuration option's name |
| 177 | * @param int $increment Amount to increment by |
| 178 | * @param bool $use_cache Whether this variable should be cached or if it |
| 179 | * changes too frequently to be efficiently cached. |
| 180 | */ |
| 181 | function increment($key, $increment, $use_cache = true) |
| 182 | { |
| 183 | if (!isset($this->config[$key])) |
| 184 | { |
| 185 | $this->config[$key] = 0; |
| 186 | } |
| 187 | |
| 188 | $this->config[$key] += $increment; |
| 189 | } |
| 190 | } |