Newer
Older
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
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CoreAdminHome\Commands\SetConfig;
use Piwik\Config;
/**
* Representation of a INI config manipulation operation. Only supports two types
* of manipulations: appending to a config array and assigning a config value.
*/
class ConfigSettingManipulation
{
/**
* @var string
*/
private $sectionName;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $value;
/**
* @var bool
*/
private $isArrayAppend;
/**
* @param string $sectionName
* @param string $name
* @param string $value
* @param bool $isArrayAppend
*/
public function __construct($sectionName, $name, $value, $isArrayAppend = false)
{
$this->sectionName = $sectionName;
$this->name = $name;
$this->value = $value;
$this->isArrayAppend = $isArrayAppend;
}
/**
* Performs the INI config manipulation.
*
* @param Config $config
* @throws \Exception if trying to append to a non-array setting value or if trying to set an
* array value to a non-array setting
*/
public function manipulate(Config $config)
{
if ($this->isArrayAppend) {
$this->appendToArraySetting($config);
} else {
$this->setSingleConfigValue($config);
}
}
private function setSingleConfigValue(Config $config)
{
$sectionName = $this->sectionName;
$section = $config->$sectionName;
if (isset($section[$this->name])
&& is_array($section[$this->name])
&& !is_array($this->value)
) {
throw new \Exception("Trying to set non-array value to array setting " . $this->getSettingString() . ".");
$section[$this->name] = $this->value;
$config->$sectionName = $section;
}
private function appendToArraySetting(Config $config)
{
$sectionName = $this->sectionName;
$section = $config->$sectionName;
if (isset($section[$this->name])
&& !is_array($section[$this->name])
) {
throw new \Exception("Trying to append to non-array setting value " . $this->getSettingString() . ".");
}
$section[$this->name][] = $this->value;
$config->$sectionName = $section;
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
}
/**
* Creates a ConfigSettingManipulation instance from a string like:
*
* `SectionName.setting_name=value`
*
* or
*
* `SectionName.setting_name[]=value`
*
* The value must be JSON so `="string"` will work but `=string` will not.
*
* @param string $assignment
* @return self
*/
public static function make($assignment)
{
if (!preg_match('/^([a-zA-Z0-9_]+)\.([a-zA-Z0-9_]+)(\[\])?=(.*)/', $assignment, $matches)) {
throw new \InvalidArgumentException("Invalid assignment string '$assignment': expected section.name=value or section.name[]=value");
}
$section = $matches[1];
$name = $matches[2];
$isAppend = !empty($matches[3]);
$value = json_decode($matches[4], $isAssoc = true);
if ($value === null) {
throw new \InvalidArgumentException("Invalid assignment string '$assignment': could not parse value as JSON");
}
return new self($section, $name, $value, $isAppend);
}
private function getSettingString()
{
return "[{$this->sectionName}] {$this->name}";
}
/**
* @return string
*/
public function getSectionName()
{
return $this->sectionName;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* @return boolean
*/
public function isArrayAppend()
{
return $this->isArrayAppend;
}
/**
* @return string
*/
public function getValueString()
{
return json_encode($this->value);
}
}