| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugins\CoreAdminHome\Commands\SetConfig; |
|
|
| use Piwik\Config; |
|
|
| |
| |
| |
| |
| class ConfigSettingManipulation |
| { |
| |
| |
| |
| private $sectionName; |
|
|
| |
| |
| |
| private $name; |
|
|
| |
| |
| |
| private $value; |
|
|
| |
| |
| |
| private $isArrayAppend; |
|
|
| |
| |
| |
| |
| |
| |
| public function __construct($sectionName, $name, $value, $isArrayAppend = false) |
| { |
| $this->sectionName = $sectionName; |
| $this->name = $name; |
| $this->value = $value; |
| $this->isArrayAppend = $isArrayAppend; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| 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; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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}"; |
| } |
|
|
| |
| |
| |
| public function getSectionName() |
| { |
| return $this->sectionName; |
| } |
|
|
| |
| |
| |
| public function getName() |
| { |
| return $this->name; |
| } |
|
|
| |
| |
| |
| public function getValue() |
| { |
| return $this->value; |
| } |
|
|
| |
| |
| |
| public function isArrayAppend() |
| { |
| return $this->isArrayAppend; |
| } |
|
|
| |
| |
| |
| public function getValueString() |
| { |
| return json_encode($this->value); |
| } |
| } |
|
|