-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIniBag.php
More file actions
186 lines (157 loc) · 3.89 KB
/
Copy pathIniBag.php
File metadata and controls
186 lines (157 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace Bolt\Session;
use Bolt\Common\Ini;
/**
* IniBag is a container for ini options.
*
* @author Carson Full <carsonfull@gmail.com>
*/
class IniBag extends OptionsBag
{
/** @var string|null */
private $extension;
/** @var string|null */
private $prefix;
/** @var array ini keys and whether they are editable */
private $cache;
/**
* Constructor.
*
* Note that extension and prefix, if given, are removed from the beginning of all keys.
*
* @param string|null $extension The extension name to pull ini values from, or null for all ini values
* @param string|null $prefix An additional prefix to filter by
*/
public function __construct($extension = null, $prefix = null)
{
if ($extension) {
if (!extension_loaded($extension)) {
throw new \InvalidArgumentException(sprintf('Extension "%s" does not exist.', $extension));
}
$prefix = $extension . '.' . $prefix;
}
$this->extension = $extension;
$this->prefix = $prefix;
parent::__construct([]);
}
/**
* {@inheritdoc}
*/
public function all()
{
$values = ini_get_all($this->extension);
$cache = [];
$parameters = [];
$prefixLength = strlen($this->prefix);
foreach ($values as $key => $value) {
// Filter out values that don't match the prefix.
if ($this->prefix && strpos($key, $this->prefix) === false) {
continue;
}
$key = substr($key, $prefixLength);
$parameters[$key] = $value['local_value'];
if ($this->cache === null) {
$cache[$key] = $value['access'] === 1 /* user */ || $value['access'] === 7 /* all */;
}
}
if ($this->cache === null) {
$this->cache = $cache;
}
return $parameters;
}
/**
* Returns all the options that are editable.
*
* @return array
*/
public function allEditable()
{
$editable = [];
foreach ($this as $key => $value) {
if ($this->cache[$key]) {
$editable[$key] = $value;
}
}
return $editable;
}
/**
* {@inheritdoc}
*/
public function keys()
{
if ($this->cache === null) {
$this->all();
}
return array_keys($this->cache);
}
/**
* {@inheritdoc}
*
* Use set() logic.
*/
public function add(array $parameters = [])
{
foreach ($parameters as $key => $value) {
$this->set($key, $value);
}
}
/**
* {@inheritdoc}
*
* Replace the values given, but don't remove any since that's not possible.
*/
public function replace(array $parameters = [])
{
$this->add($parameters);
}
/**
* {@inheritdoc}
*/
public function get($key, $default = null, $deep = false)
{
if (!$this->has($key)) {
return $default;
}
return ini_get($this->prefix . $key);
}
/**
* {@inheritdoc}
*/
public function set($key, $value)
{
Ini::set($this->prefix . $key, $value);
}
/**
* {@inheritdoc}
*
* Check existence after keys cache is populated.
*/
public function has($key)
{
return Ini::has($this->prefix . $key);
}
/**
* {@inheritdoc}
*/
public function remove($key)
{
throw new \BadMethodCallException('ini options cannot be removed.');
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
return new \ArrayIterator($this->all());
}
/**
* {@inheritdoc}
*/
public function count()
{
if ($this->cache === null) {
$this->all();
}
return count($this->cache);
}
}