Passed
Push — master ( 4e9cd2...bc8d14 )
by Roeland
19:16 queued 09:49
created
core/Command/Config/System/Base.php 1 patch
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -26,53 +26,53 @@
 block discarded – undo
26 26
 
27 27
 abstract class Base extends \OC\Core\Command\Base {
28 28
 
29
-	/** @var SystemConfig */
30
-	protected $systemConfig;
29
+    /** @var SystemConfig */
30
+    protected $systemConfig;
31 31
 
32
-	/**
33
-	 * @param string $argumentName
34
-	 * @param CompletionContext $context
35
-	 * @return string[]
36
-	 */
37
-	public function completeArgumentValues($argumentName, CompletionContext $context) {
38
-		if ($argumentName === 'name') {
39
-			$words = $this->getPreviousNames($context, $context->getWordIndex());
40
-			if (empty($words)) {
41
-				$completions = $this->systemConfig->getKeys();
42
-			} else {
43
-				$key = array_shift($words);
44
-				$value = $this->systemConfig->getValue($key);
45
-				$completions = array_keys($value);
32
+    /**
33
+     * @param string $argumentName
34
+     * @param CompletionContext $context
35
+     * @return string[]
36
+     */
37
+    public function completeArgumentValues($argumentName, CompletionContext $context) {
38
+        if ($argumentName === 'name') {
39
+            $words = $this->getPreviousNames($context, $context->getWordIndex());
40
+            if (empty($words)) {
41
+                $completions = $this->systemConfig->getKeys();
42
+            } else {
43
+                $key = array_shift($words);
44
+                $value = $this->systemConfig->getValue($key);
45
+                $completions = array_keys($value);
46 46
 
47
-				while (!empty($words) && is_array($value)) {
48
-					$key = array_shift($words);
49
-					if (!isset($value[$key]) || !is_array($value[$key])) {
50
-						break;
51
-					}
47
+                while (!empty($words) && is_array($value)) {
48
+                    $key = array_shift($words);
49
+                    if (!isset($value[$key]) || !is_array($value[$key])) {
50
+                        break;
51
+                    }
52 52
 
53
-					$value = $value[$key];
54
-					$completions = array_keys($value);
55
-				}
56
-			}
53
+                    $value = $value[$key];
54
+                    $completions = array_keys($value);
55
+                }
56
+            }
57 57
 
58
-			return $completions;
59
-		}
60
-		return parent::completeArgumentValues($argumentName, $context);
61
-	}
58
+            return $completions;
59
+        }
60
+        return parent::completeArgumentValues($argumentName, $context);
61
+    }
62 62
 
63
-	/**
64
-	 * @param CompletionContext $context
65
-	 * @param int $currentIndex
66
-	 * @return string[]
67
-	 */
68
-	protected function getPreviousNames(CompletionContext $context, $currentIndex) {
69
-		$word = $context->getWordAtIndex($currentIndex - 1);
70
-		if ($word === $this->getName() || $currentIndex <= 0) {
71
-			return [];
72
-		}
63
+    /**
64
+     * @param CompletionContext $context
65
+     * @param int $currentIndex
66
+     * @return string[]
67
+     */
68
+    protected function getPreviousNames(CompletionContext $context, $currentIndex) {
69
+        $word = $context->getWordAtIndex($currentIndex - 1);
70
+        if ($word === $this->getName() || $currentIndex <= 0) {
71
+            return [];
72
+        }
73 73
 
74
-		$words = $this->getPreviousNames($context, $currentIndex - 1);
75
-		$words[] = $word;
76
-		return $words;
77
-	}
74
+        $words = $this->getPreviousNames($context, $currentIndex - 1);
75
+        $words[] = $word;
76
+        return $words;
77
+    }
78 78
 }
Please login to merge, or discard this patch.
core/Command/Config/System/SetConfig.php 2 patches
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
 			$this->systemConfig->setValue($configName, $configValue['value']);
98 98
 		}
99 99
 
100
-		$output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' set to ' . $configValue['readable-value'] . '</info>');
100
+		$output->writeln('<info>System config value '.implode(' => ', $configNames).' set to '.$configValue['readable-value'].'</info>');
101 101
 		return 0;
102 102
 	}
103 103
 
@@ -116,7 +116,7 @@  discard block
 block discarded – undo
116 116
 				}
117 117
 				return [
118 118
 					'value' => (int) $value,
119
-					'readable-value' => 'integer ' . (int) $value,
119
+					'readable-value' => 'integer '.(int) $value,
120 120
 				];
121 121
 
122 122
 			case 'double':
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 				}
127 127
 				return [
128 128
 					'value' => (double) $value,
129
-					'readable-value' => 'double ' . (double) $value,
129
+					'readable-value' => 'double '.(double) $value,
130 130
 				];
131 131
 
132 132
 			case 'boolean':
@@ -136,13 +136,13 @@  discard block
 block discarded – undo
136 136
 					case 'true':
137 137
 						return [
138 138
 							'value' => true,
139
-							'readable-value' => 'boolean ' . $value,
139
+							'readable-value' => 'boolean '.$value,
140 140
 						];
141 141
 
142 142
 					case 'false':
143 143
 						return [
144 144
 							'value' => false,
145
-							'readable-value' => 'boolean ' . $value,
145
+							'readable-value' => 'boolean '.$value,
146 146
 						];
147 147
 
148 148
 					default:
@@ -159,7 +159,7 @@  discard block
 block discarded – undo
159 159
 				$value = (string) $value;
160 160
 				return [
161 161
 					'value' => $value,
162
-					'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
162
+					'readable-value' => ($value === '') ? 'empty string' : 'string '.$value,
163 163
 				];
164 164
 
165 165
 			default:
Please login to merge, or discard this patch.
Indentation   +176 added lines, -176 removed lines patch added patch discarded remove patch
@@ -31,180 +31,180 @@
 block discarded – undo
31 31
 use Symfony\Component\Console\Output\OutputInterface;
32 32
 
33 33
 class SetConfig extends Base {
34
-	/** * @var SystemConfig */
35
-	protected $systemConfig;
36
-
37
-	/**
38
-	 * @param SystemConfig $systemConfig
39
-	 */
40
-	public function __construct(SystemConfig $systemConfig) {
41
-		parent::__construct();
42
-		$this->systemConfig = $systemConfig;
43
-	}
44
-
45
-	protected function configure() {
46
-		parent::configure();
47
-
48
-		$this
49
-			->setName('config:system:set')
50
-			->setDescription('Set a system config value')
51
-			->addArgument(
52
-				'name',
53
-				InputArgument::REQUIRED | InputArgument::IS_ARRAY,
54
-				'Name of the config parameter, specify multiple for array parameter'
55
-			)
56
-			->addOption(
57
-				'type',
58
-				null,
59
-				InputOption::VALUE_REQUIRED,
60
-				'Value type [string, integer, double, boolean]',
61
-				'string'
62
-			)
63
-			->addOption(
64
-				'value',
65
-				null,
66
-				InputOption::VALUE_REQUIRED,
67
-				'The new value of the config'
68
-			)
69
-			->addOption(
70
-				'update-only',
71
-				null,
72
-				InputOption::VALUE_NONE,
73
-				'Only updates the value, if it is not set before, it is not being added'
74
-			)
75
-		;
76
-	}
77
-
78
-	protected function execute(InputInterface $input, OutputInterface $output) {
79
-		$configNames = $input->getArgument('name');
80
-		$configName = $configNames[0];
81
-		$configValue = $this->castValue($input->getOption('value'), $input->getOption('type'));
82
-		$updateOnly = $input->getOption('update-only');
83
-
84
-		if (count($configNames) > 1) {
85
-			$existingValue = $this->systemConfig->getValue($configName);
86
-
87
-			$newValue = $this->mergeArrayValue(
88
-				array_slice($configNames, 1), $existingValue, $configValue['value'], $updateOnly
89
-			);
90
-
91
-			$this->systemConfig->setValue($configName, $newValue);
92
-		} else {
93
-			if ($updateOnly && !in_array($configName, $this->systemConfig->getKeys(), true)) {
94
-				throw new \UnexpectedValueException('Config parameter does not exist');
95
-			}
96
-
97
-			$this->systemConfig->setValue($configName, $configValue['value']);
98
-		}
99
-
100
-		$output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' set to ' . $configValue['readable-value'] . '</info>');
101
-		return 0;
102
-	}
103
-
104
-	/**
105
-	 * @param string $value
106
-	 * @param string $type
107
-	 * @return mixed
108
-	 * @throws \InvalidArgumentException
109
-	 */
110
-	protected function castValue($value, $type) {
111
-		switch ($type) {
112
-			case 'integer':
113
-			case 'int':
114
-				if (!is_numeric($value)) {
115
-					throw new \InvalidArgumentException('Non-numeric value specified');
116
-				}
117
-				return [
118
-					'value' => (int) $value,
119
-					'readable-value' => 'integer ' . (int) $value,
120
-				];
121
-
122
-			case 'double':
123
-			case 'float':
124
-				if (!is_numeric($value)) {
125
-					throw new \InvalidArgumentException('Non-numeric value specified');
126
-				}
127
-				return [
128
-					'value' => (double) $value,
129
-					'readable-value' => 'double ' . (double) $value,
130
-				];
131
-
132
-			case 'boolean':
133
-			case 'bool':
134
-				$value = strtolower($value);
135
-				switch ($value) {
136
-					case 'true':
137
-						return [
138
-							'value' => true,
139
-							'readable-value' => 'boolean ' . $value,
140
-						];
141
-
142
-					case 'false':
143
-						return [
144
-							'value' => false,
145
-							'readable-value' => 'boolean ' . $value,
146
-						];
147
-
148
-					default:
149
-						throw new \InvalidArgumentException('Unable to parse value as boolean');
150
-				}
151
-
152
-			case 'null':
153
-				return [
154
-					'value' => null,
155
-					'readable-value' => 'null',
156
-				];
157
-
158
-			case 'string':
159
-				$value = (string) $value;
160
-				return [
161
-					'value' => $value,
162
-					'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
163
-				];
164
-
165
-			default:
166
-				throw new \InvalidArgumentException('Invalid type');
167
-		}
168
-	}
169
-
170
-	/**
171
-	 * @param array $configNames
172
-	 * @param mixed $existingValues
173
-	 * @param mixed $value
174
-	 * @param bool $updateOnly
175
-	 * @return array merged value
176
-	 * @throws \UnexpectedValueException
177
-	 */
178
-	protected function mergeArrayValue(array $configNames, $existingValues, $value, $updateOnly) {
179
-		$configName = array_shift($configNames);
180
-		if (!is_array($existingValues)) {
181
-			$existingValues = [];
182
-		}
183
-		if (!empty($configNames)) {
184
-			if (isset($existingValues[$configName])) {
185
-				$existingValue = $existingValues[$configName];
186
-			} else {
187
-				$existingValue = [];
188
-			}
189
-			$existingValues[$configName] = $this->mergeArrayValue($configNames, $existingValue, $value, $updateOnly);
190
-		} else {
191
-			if (!isset($existingValues[$configName]) && $updateOnly) {
192
-				throw new \UnexpectedValueException('Config parameter does not exist');
193
-			}
194
-			$existingValues[$configName] = $value;
195
-		}
196
-		return $existingValues;
197
-	}
198
-
199
-	/**
200
-	 * @param string $optionName
201
-	 * @param CompletionContext $context
202
-	 * @return string[]
203
-	 */
204
-	public function completeOptionValues($optionName, CompletionContext $context) {
205
-		if ($optionName === 'type') {
206
-			return ['string', 'integer', 'double', 'boolean'];
207
-		}
208
-		return parent::completeOptionValues($optionName, $context);
209
-	}
34
+    /** * @var SystemConfig */
35
+    protected $systemConfig;
36
+
37
+    /**
38
+     * @param SystemConfig $systemConfig
39
+     */
40
+    public function __construct(SystemConfig $systemConfig) {
41
+        parent::__construct();
42
+        $this->systemConfig = $systemConfig;
43
+    }
44
+
45
+    protected function configure() {
46
+        parent::configure();
47
+
48
+        $this
49
+            ->setName('config:system:set')
50
+            ->setDescription('Set a system config value')
51
+            ->addArgument(
52
+                'name',
53
+                InputArgument::REQUIRED | InputArgument::IS_ARRAY,
54
+                'Name of the config parameter, specify multiple for array parameter'
55
+            )
56
+            ->addOption(
57
+                'type',
58
+                null,
59
+                InputOption::VALUE_REQUIRED,
60
+                'Value type [string, integer, double, boolean]',
61
+                'string'
62
+            )
63
+            ->addOption(
64
+                'value',
65
+                null,
66
+                InputOption::VALUE_REQUIRED,
67
+                'The new value of the config'
68
+            )
69
+            ->addOption(
70
+                'update-only',
71
+                null,
72
+                InputOption::VALUE_NONE,
73
+                'Only updates the value, if it is not set before, it is not being added'
74
+            )
75
+        ;
76
+    }
77
+
78
+    protected function execute(InputInterface $input, OutputInterface $output) {
79
+        $configNames = $input->getArgument('name');
80
+        $configName = $configNames[0];
81
+        $configValue = $this->castValue($input->getOption('value'), $input->getOption('type'));
82
+        $updateOnly = $input->getOption('update-only');
83
+
84
+        if (count($configNames) > 1) {
85
+            $existingValue = $this->systemConfig->getValue($configName);
86
+
87
+            $newValue = $this->mergeArrayValue(
88
+                array_slice($configNames, 1), $existingValue, $configValue['value'], $updateOnly
89
+            );
90
+
91
+            $this->systemConfig->setValue($configName, $newValue);
92
+        } else {
93
+            if ($updateOnly && !in_array($configName, $this->systemConfig->getKeys(), true)) {
94
+                throw new \UnexpectedValueException('Config parameter does not exist');
95
+            }
96
+
97
+            $this->systemConfig->setValue($configName, $configValue['value']);
98
+        }
99
+
100
+        $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' set to ' . $configValue['readable-value'] . '</info>');
101
+        return 0;
102
+    }
103
+
104
+    /**
105
+     * @param string $value
106
+     * @param string $type
107
+     * @return mixed
108
+     * @throws \InvalidArgumentException
109
+     */
110
+    protected function castValue($value, $type) {
111
+        switch ($type) {
112
+            case 'integer':
113
+            case 'int':
114
+                if (!is_numeric($value)) {
115
+                    throw new \InvalidArgumentException('Non-numeric value specified');
116
+                }
117
+                return [
118
+                    'value' => (int) $value,
119
+                    'readable-value' => 'integer ' . (int) $value,
120
+                ];
121
+
122
+            case 'double':
123
+            case 'float':
124
+                if (!is_numeric($value)) {
125
+                    throw new \InvalidArgumentException('Non-numeric value specified');
126
+                }
127
+                return [
128
+                    'value' => (double) $value,
129
+                    'readable-value' => 'double ' . (double) $value,
130
+                ];
131
+
132
+            case 'boolean':
133
+            case 'bool':
134
+                $value = strtolower($value);
135
+                switch ($value) {
136
+                    case 'true':
137
+                        return [
138
+                            'value' => true,
139
+                            'readable-value' => 'boolean ' . $value,
140
+                        ];
141
+
142
+                    case 'false':
143
+                        return [
144
+                            'value' => false,
145
+                            'readable-value' => 'boolean ' . $value,
146
+                        ];
147
+
148
+                    default:
149
+                        throw new \InvalidArgumentException('Unable to parse value as boolean');
150
+                }
151
+
152
+            case 'null':
153
+                return [
154
+                    'value' => null,
155
+                    'readable-value' => 'null',
156
+                ];
157
+
158
+            case 'string':
159
+                $value = (string) $value;
160
+                return [
161
+                    'value' => $value,
162
+                    'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
163
+                ];
164
+
165
+            default:
166
+                throw new \InvalidArgumentException('Invalid type');
167
+        }
168
+    }
169
+
170
+    /**
171
+     * @param array $configNames
172
+     * @param mixed $existingValues
173
+     * @param mixed $value
174
+     * @param bool $updateOnly
175
+     * @return array merged value
176
+     * @throws \UnexpectedValueException
177
+     */
178
+    protected function mergeArrayValue(array $configNames, $existingValues, $value, $updateOnly) {
179
+        $configName = array_shift($configNames);
180
+        if (!is_array($existingValues)) {
181
+            $existingValues = [];
182
+        }
183
+        if (!empty($configNames)) {
184
+            if (isset($existingValues[$configName])) {
185
+                $existingValue = $existingValues[$configName];
186
+            } else {
187
+                $existingValue = [];
188
+            }
189
+            $existingValues[$configName] = $this->mergeArrayValue($configNames, $existingValue, $value, $updateOnly);
190
+        } else {
191
+            if (!isset($existingValues[$configName]) && $updateOnly) {
192
+                throw new \UnexpectedValueException('Config parameter does not exist');
193
+            }
194
+            $existingValues[$configName] = $value;
195
+        }
196
+        return $existingValues;
197
+    }
198
+
199
+    /**
200
+     * @param string $optionName
201
+     * @param CompletionContext $context
202
+     * @return string[]
203
+     */
204
+    public function completeOptionValues($optionName, CompletionContext $context) {
205
+        if ($optionName === 'type') {
206
+            return ['string', 'integer', 'double', 'boolean'];
207
+        }
208
+        return parent::completeOptionValues($optionName, $context);
209
+    }
210 210
 }
Please login to merge, or discard this patch.
core/Command/Config/System/GetConfig.php 1 patch
Indentation   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -29,72 +29,72 @@
 block discarded – undo
29 29
 use Symfony\Component\Console\Output\OutputInterface;
30 30
 
31 31
 class GetConfig extends Base {
32
-	/** * @var SystemConfig */
33
-	protected $systemConfig;
32
+    /** * @var SystemConfig */
33
+    protected $systemConfig;
34 34
 
35
-	/**
36
-	 * @param SystemConfig $systemConfig
37
-	 */
38
-	public function __construct(SystemConfig $systemConfig) {
39
-		parent::__construct();
40
-		$this->systemConfig = $systemConfig;
41
-	}
35
+    /**
36
+     * @param SystemConfig $systemConfig
37
+     */
38
+    public function __construct(SystemConfig $systemConfig) {
39
+        parent::__construct();
40
+        $this->systemConfig = $systemConfig;
41
+    }
42 42
 
43
-	protected function configure() {
44
-		parent::configure();
43
+    protected function configure() {
44
+        parent::configure();
45 45
 
46
-		$this
47
-			->setName('config:system:get')
48
-			->setDescription('Get a system config value')
49
-			->addArgument(
50
-				'name',
51
-				InputArgument::REQUIRED | InputArgument::IS_ARRAY,
52
-				'Name of the config to get, specify multiple for array parameter'
53
-			)
54
-			->addOption(
55
-				'default-value',
56
-				null,
57
-				InputOption::VALUE_OPTIONAL,
58
-				'If no default value is set and the config does not exist, the command will exit with 1'
59
-			)
60
-		;
61
-	}
46
+        $this
47
+            ->setName('config:system:get')
48
+            ->setDescription('Get a system config value')
49
+            ->addArgument(
50
+                'name',
51
+                InputArgument::REQUIRED | InputArgument::IS_ARRAY,
52
+                'Name of the config to get, specify multiple for array parameter'
53
+            )
54
+            ->addOption(
55
+                'default-value',
56
+                null,
57
+                InputOption::VALUE_OPTIONAL,
58
+                'If no default value is set and the config does not exist, the command will exit with 1'
59
+            )
60
+        ;
61
+    }
62 62
 
63
-	/**
64
-	 * Executes the current command.
65
-	 *
66
-	 * @param InputInterface  $input  An InputInterface instance
67
-	 * @param OutputInterface $output An OutputInterface instance
68
-	 * @return null|int null or 0 if everything went fine, or an error code
69
-	 */
70
-	protected function execute(InputInterface $input, OutputInterface $output) {
71
-		$configNames = $input->getArgument('name');
72
-		$configName = array_shift($configNames);
73
-		$defaultValue = $input->getOption('default-value');
63
+    /**
64
+     * Executes the current command.
65
+     *
66
+     * @param InputInterface  $input  An InputInterface instance
67
+     * @param OutputInterface $output An OutputInterface instance
68
+     * @return null|int null or 0 if everything went fine, or an error code
69
+     */
70
+    protected function execute(InputInterface $input, OutputInterface $output) {
71
+        $configNames = $input->getArgument('name');
72
+        $configName = array_shift($configNames);
73
+        $defaultValue = $input->getOption('default-value');
74 74
 
75
-		if (!in_array($configName, $this->systemConfig->getKeys()) && !$input->hasParameterOption('--default-value')) {
76
-			return 1;
77
-		}
75
+        if (!in_array($configName, $this->systemConfig->getKeys()) && !$input->hasParameterOption('--default-value')) {
76
+            return 1;
77
+        }
78 78
 
79
-		if (!in_array($configName, $this->systemConfig->getKeys())) {
80
-			$configValue = $defaultValue;
81
-		} else {
82
-			$configValue = $this->systemConfig->getValue($configName);
83
-			if (!empty($configNames)) {
84
-				foreach ($configNames as $configName) {
85
-					if (isset($configValue[$configName])) {
86
-						$configValue = $configValue[$configName];
87
-					} else if (!$input->hasParameterOption('--default-value')) {
88
-						return 1;
89
-					} else {
90
-						$configValue = $defaultValue;
91
-						break;
92
-					}
93
-				}
94
-			}
95
-		}
79
+        if (!in_array($configName, $this->systemConfig->getKeys())) {
80
+            $configValue = $defaultValue;
81
+        } else {
82
+            $configValue = $this->systemConfig->getValue($configName);
83
+            if (!empty($configNames)) {
84
+                foreach ($configNames as $configName) {
85
+                    if (isset($configValue[$configName])) {
86
+                        $configValue = $configValue[$configName];
87
+                    } else if (!$input->hasParameterOption('--default-value')) {
88
+                        return 1;
89
+                    } else {
90
+                        $configValue = $defaultValue;
91
+                        break;
92
+                    }
93
+                }
94
+            }
95
+        }
96 96
 
97
-		$this->writeMixedInOutputFormat($input, $output, $configValue);
98
-		return 0;
99
-	}
97
+        $this->writeMixedInOutputFormat($input, $output, $configValue);
98
+        return 0;
99
+    }
100 100
 }
Please login to merge, or discard this patch.
core/Command/Config/App/DeleteConfig.php 2 patches
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -29,53 +29,53 @@
 block discarded – undo
29 29
 use Symfony\Component\Console\Output\OutputInterface;
30 30
 
31 31
 class DeleteConfig extends Base {
32
-	/** * @var IConfig */
33
-	protected $config;
32
+    /** * @var IConfig */
33
+    protected $config;
34 34
 
35
-	/**
36
-	 * @param IConfig $config
37
-	 */
38
-	public function __construct(IConfig $config) {
39
-		parent::__construct();
40
-		$this->config = $config;
41
-	}
35
+    /**
36
+     * @param IConfig $config
37
+     */
38
+    public function __construct(IConfig $config) {
39
+        parent::__construct();
40
+        $this->config = $config;
41
+    }
42 42
 
43
-	protected function configure() {
44
-		parent::configure();
43
+    protected function configure() {
44
+        parent::configure();
45 45
 
46
-		$this
47
-			->setName('config:app:delete')
48
-			->setDescription('Delete an app config value')
49
-			->addArgument(
50
-				'app',
51
-				InputArgument::REQUIRED,
52
-				'Name of the app'
53
-			)
54
-			->addArgument(
55
-				'name',
56
-				InputArgument::REQUIRED,
57
-				'Name of the config to delete'
58
-			)
59
-			->addOption(
60
-				'error-if-not-exists',
61
-				null,
62
-				InputOption::VALUE_NONE,
63
-				'Checks whether the config exists before deleting it'
64
-			)
65
-		;
66
-	}
46
+        $this
47
+            ->setName('config:app:delete')
48
+            ->setDescription('Delete an app config value')
49
+            ->addArgument(
50
+                'app',
51
+                InputArgument::REQUIRED,
52
+                'Name of the app'
53
+            )
54
+            ->addArgument(
55
+                'name',
56
+                InputArgument::REQUIRED,
57
+                'Name of the config to delete'
58
+            )
59
+            ->addOption(
60
+                'error-if-not-exists',
61
+                null,
62
+                InputOption::VALUE_NONE,
63
+                'Checks whether the config exists before deleting it'
64
+            )
65
+        ;
66
+    }
67 67
 
68
-	protected function execute(InputInterface $input, OutputInterface $output) {
69
-		$appName = $input->getArgument('app');
70
-		$configName = $input->getArgument('name');
68
+    protected function execute(InputInterface $input, OutputInterface $output) {
69
+        $appName = $input->getArgument('app');
70
+        $configName = $input->getArgument('name');
71 71
 
72
-		if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->config->getAppKeys($appName))) {
73
-			$output->writeln('<error>Config ' . $configName . ' of app ' . $appName . ' could not be deleted because it did not exist</error>');
74
-			return 1;
75
-		}
72
+        if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->config->getAppKeys($appName))) {
73
+            $output->writeln('<error>Config ' . $configName . ' of app ' . $appName . ' could not be deleted because it did not exist</error>');
74
+            return 1;
75
+        }
76 76
 
77
-		$this->config->deleteAppValue($appName, $configName);
78
-		$output->writeln('<info>Config value ' . $configName . ' of app ' . $appName . ' deleted</info>');
79
-		return 0;
80
-	}
77
+        $this->config->deleteAppValue($appName, $configName);
78
+        $output->writeln('<info>Config value ' . $configName . ' of app ' . $appName . ' deleted</info>');
79
+        return 0;
80
+    }
81 81
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -70,12 +70,12 @@
 block discarded – undo
70 70
 		$configName = $input->getArgument('name');
71 71
 
72 72
 		if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->config->getAppKeys($appName))) {
73
-			$output->writeln('<error>Config ' . $configName . ' of app ' . $appName . ' could not be deleted because it did not exist</error>');
73
+			$output->writeln('<error>Config '.$configName.' of app '.$appName.' could not be deleted because it did not exist</error>');
74 74
 			return 1;
75 75
 		}
76 76
 
77 77
 		$this->config->deleteAppValue($appName, $configName);
78
-		$output->writeln('<info>Config value ' . $configName . ' of app ' . $appName . ' deleted</info>');
78
+		$output->writeln('<info>Config value '.$configName.' of app '.$appName.' deleted</info>');
79 79
 		return 0;
80 80
 	}
81 81
 }
Please login to merge, or discard this patch.
core/Command/Config/App/Base.php 1 patch
Indentation   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -26,23 +26,23 @@
 block discarded – undo
26 26
 
27 27
 abstract class Base extends \OC\Core\Command\Base {
28 28
 
29
-	/** * @var IConfig */
30
-	protected $config;
29
+    /** * @var IConfig */
30
+    protected $config;
31 31
 
32
-	/**
33
-	 * @param string $argumentName
34
-	 * @param CompletionContext $context
35
-	 * @return string[]
36
-	 */
37
-	public function completeArgumentValues($argumentName, CompletionContext $context) {
38
-		if ($argumentName === 'app') {
39
-			return \OC_App::getAllApps();
40
-		}
32
+    /**
33
+     * @param string $argumentName
34
+     * @param CompletionContext $context
35
+     * @return string[]
36
+     */
37
+    public function completeArgumentValues($argumentName, CompletionContext $context) {
38
+        if ($argumentName === 'app') {
39
+            return \OC_App::getAllApps();
40
+        }
41 41
 
42
-		if ($argumentName === 'name') {
43
-			$appName = $context->getWordAtIndex($context->getWordIndex() - 1);
44
-			return $this->config->getAppKeys($appName);
45
-		}
46
-		return [];
47
-	}
42
+        if ($argumentName === 'name') {
43
+            $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
44
+            return $this->config->getAppKeys($appName);
45
+        }
46
+        return [];
47
+    }
48 48
 }
Please login to merge, or discard this patch.
core/Command/Config/App/SetConfig.php 2 patches
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -29,61 +29,61 @@
 block discarded – undo
29 29
 use Symfony\Component\Console\Output\OutputInterface;
30 30
 
31 31
 class SetConfig extends Base {
32
-	/** * @var IConfig */
33
-	protected $config;
32
+    /** * @var IConfig */
33
+    protected $config;
34 34
 
35
-	/**
36
-	 * @param IConfig $config
37
-	 */
38
-	public function __construct(IConfig $config) {
39
-		parent::__construct();
40
-		$this->config = $config;
41
-	}
35
+    /**
36
+     * @param IConfig $config
37
+     */
38
+    public function __construct(IConfig $config) {
39
+        parent::__construct();
40
+        $this->config = $config;
41
+    }
42 42
 
43
-	protected function configure() {
44
-		parent::configure();
43
+    protected function configure() {
44
+        parent::configure();
45 45
 
46
-		$this
47
-			->setName('config:app:set')
48
-			->setDescription('Set an app config value')
49
-			->addArgument(
50
-				'app',
51
-				InputArgument::REQUIRED,
52
-				'Name of the app'
53
-			)
54
-			->addArgument(
55
-				'name',
56
-				InputArgument::REQUIRED,
57
-				'Name of the config to set'
58
-			)
59
-			->addOption(
60
-				'value',
61
-				null,
62
-				InputOption::VALUE_REQUIRED,
63
-				'The new value of the config'
64
-			)
65
-			->addOption(
66
-				'update-only',
67
-				null,
68
-				InputOption::VALUE_NONE,
69
-				'Only updates the value, if it is not set before, it is not being added'
70
-			)
71
-		;
72
-	}
46
+        $this
47
+            ->setName('config:app:set')
48
+            ->setDescription('Set an app config value')
49
+            ->addArgument(
50
+                'app',
51
+                InputArgument::REQUIRED,
52
+                'Name of the app'
53
+            )
54
+            ->addArgument(
55
+                'name',
56
+                InputArgument::REQUIRED,
57
+                'Name of the config to set'
58
+            )
59
+            ->addOption(
60
+                'value',
61
+                null,
62
+                InputOption::VALUE_REQUIRED,
63
+                'The new value of the config'
64
+            )
65
+            ->addOption(
66
+                'update-only',
67
+                null,
68
+                InputOption::VALUE_NONE,
69
+                'Only updates the value, if it is not set before, it is not being added'
70
+            )
71
+        ;
72
+    }
73 73
 
74
-	protected function execute(InputInterface $input, OutputInterface $output) {
75
-		$appName = $input->getArgument('app');
76
-		$configName = $input->getArgument('name');
74
+    protected function execute(InputInterface $input, OutputInterface $output) {
75
+        $appName = $input->getArgument('app');
76
+        $configName = $input->getArgument('name');
77 77
 
78
-		if (!in_array($configName, $this->config->getAppKeys($appName)) && $input->hasParameterOption('--update-only')) {
79
-			$output->writeln('<comment>Config value ' . $configName . ' for app ' . $appName . ' not updated, as it has not been set before.</comment>');
80
-			return 1;
81
-		}
78
+        if (!in_array($configName, $this->config->getAppKeys($appName)) && $input->hasParameterOption('--update-only')) {
79
+            $output->writeln('<comment>Config value ' . $configName . ' for app ' . $appName . ' not updated, as it has not been set before.</comment>');
80
+            return 1;
81
+        }
82 82
 
83
-		$configValue = $input->getOption('value');
84
-		$this->config->setAppValue($appName, $configName, $configValue);
83
+        $configValue = $input->getOption('value');
84
+        $this->config->setAppValue($appName, $configName, $configValue);
85 85
 
86
-		$output->writeln('<info>Config value ' . $configName . ' for app ' . $appName . ' set to ' . $configValue . '</info>');
87
-		return 0;
88
-	}
86
+        $output->writeln('<info>Config value ' . $configName . ' for app ' . $appName . ' set to ' . $configValue . '</info>');
87
+        return 0;
88
+    }
89 89
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -76,14 +76,14 @@
 block discarded – undo
76 76
 		$configName = $input->getArgument('name');
77 77
 
78 78
 		if (!in_array($configName, $this->config->getAppKeys($appName)) && $input->hasParameterOption('--update-only')) {
79
-			$output->writeln('<comment>Config value ' . $configName . ' for app ' . $appName . ' not updated, as it has not been set before.</comment>');
79
+			$output->writeln('<comment>Config value '.$configName.' for app '.$appName.' not updated, as it has not been set before.</comment>');
80 80
 			return 1;
81 81
 		}
82 82
 
83 83
 		$configValue = $input->getOption('value');
84 84
 		$this->config->setAppValue($appName, $configName, $configValue);
85 85
 
86
-		$output->writeln('<info>Config value ' . $configName . ' for app ' . $appName . ' set to ' . $configValue . '</info>');
86
+		$output->writeln('<info>Config value '.$configName.' for app '.$appName.' set to '.$configValue.'</info>');
87 87
 		return 0;
88 88
 	}
89 89
 }
Please login to merge, or discard this patch.
core/Command/Config/App/GetConfig.php 1 patch
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -29,65 +29,65 @@
 block discarded – undo
29 29
 use Symfony\Component\Console\Output\OutputInterface;
30 30
 
31 31
 class GetConfig extends Base {
32
-	/** * @var IConfig */
33
-	protected $config;
32
+    /** * @var IConfig */
33
+    protected $config;
34 34
 
35
-	/**
36
-	 * @param IConfig $config
37
-	 */
38
-	public function __construct(IConfig $config) {
39
-		parent::__construct();
40
-		$this->config = $config;
41
-	}
35
+    /**
36
+     * @param IConfig $config
37
+     */
38
+    public function __construct(IConfig $config) {
39
+        parent::__construct();
40
+        $this->config = $config;
41
+    }
42 42
 
43
-	protected function configure() {
44
-		parent::configure();
43
+    protected function configure() {
44
+        parent::configure();
45 45
 
46
-		$this
47
-			->setName('config:app:get')
48
-			->setDescription('Get an app config value')
49
-			->addArgument(
50
-				'app',
51
-				InputArgument::REQUIRED,
52
-				'Name of the app'
53
-			)
54
-			->addArgument(
55
-				'name',
56
-				InputArgument::REQUIRED,
57
-				'Name of the config to get'
58
-			)
59
-			->addOption(
60
-				'default-value',
61
-				null,
62
-				InputOption::VALUE_OPTIONAL,
63
-				'If no default value is set and the config does not exist, the command will exit with 1'
64
-			)
65
-		;
66
-	}
46
+        $this
47
+            ->setName('config:app:get')
48
+            ->setDescription('Get an app config value')
49
+            ->addArgument(
50
+                'app',
51
+                InputArgument::REQUIRED,
52
+                'Name of the app'
53
+            )
54
+            ->addArgument(
55
+                'name',
56
+                InputArgument::REQUIRED,
57
+                'Name of the config to get'
58
+            )
59
+            ->addOption(
60
+                'default-value',
61
+                null,
62
+                InputOption::VALUE_OPTIONAL,
63
+                'If no default value is set and the config does not exist, the command will exit with 1'
64
+            )
65
+        ;
66
+    }
67 67
 
68
-	/**
69
-	 * Executes the current command.
70
-	 *
71
-	 * @param InputInterface  $input  An InputInterface instance
72
-	 * @param OutputInterface $output An OutputInterface instance
73
-	 * @return null|int null or 0 if everything went fine, or an error code
74
-	 */
75
-	protected function execute(InputInterface $input, OutputInterface $output) {
76
-		$appName = $input->getArgument('app');
77
-		$configName = $input->getArgument('name');
78
-		$defaultValue = $input->getOption('default-value');
68
+    /**
69
+     * Executes the current command.
70
+     *
71
+     * @param InputInterface  $input  An InputInterface instance
72
+     * @param OutputInterface $output An OutputInterface instance
73
+     * @return null|int null or 0 if everything went fine, or an error code
74
+     */
75
+    protected function execute(InputInterface $input, OutputInterface $output) {
76
+        $appName = $input->getArgument('app');
77
+        $configName = $input->getArgument('name');
78
+        $defaultValue = $input->getOption('default-value');
79 79
 
80
-		if (!in_array($configName, $this->config->getAppKeys($appName)) && !$input->hasParameterOption('--default-value')) {
81
-			return 1;
82
-		}
80
+        if (!in_array($configName, $this->config->getAppKeys($appName)) && !$input->hasParameterOption('--default-value')) {
81
+            return 1;
82
+        }
83 83
 
84
-		if (!in_array($configName, $this->config->getAppKeys($appName))) {
85
-			$configValue = $defaultValue;
86
-		} else {
87
-			$configValue = $this->config->getAppValue($appName, $configName);
88
-		}
84
+        if (!in_array($configName, $this->config->getAppKeys($appName))) {
85
+            $configValue = $defaultValue;
86
+        } else {
87
+            $configValue = $this->config->getAppValue($appName, $configName);
88
+        }
89 89
 
90
-		$this->writeMixedInOutputFormat($input, $output, $configValue);
91
-		return 0;
92
-	}
90
+        $this->writeMixedInOutputFormat($input, $output, $configValue);
91
+        return 0;
92
+    }
93 93
 }
Please login to merge, or discard this patch.
core/Command/Config/Import.php 2 patches
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -32,7 +32,7 @@  discard block
 block discarded – undo
32 32
 use Symfony\Component\Console\Input\InputInterface;
33 33
 use Symfony\Component\Console\Output\OutputInterface;
34 34
 
35
-class Import extends Command implements CompletionAwareInterface  {
35
+class Import extends Command implements CompletionAwareInterface {
36 36
 	protected $validRootKeys = ['system', 'apps'];
37 37
 
38 38
 	/** @var IConfig */
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 		try {
70 70
 			$configs = $this->validateFileContent($content);
71 71
 		} catch (\UnexpectedValueException $e) {
72
-			$output->writeln('<error>' . $e->getMessage(). '</error>');
72
+			$output->writeln('<error>'.$e->getMessage().'</error>');
73 73
 			return;
74 74
 		}
75 75
 
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
 			}
90 90
 		}
91 91
 
92
-		$output->writeln('<info>Config successfully imported from: ' . $importFile . '</info>');
92
+		$output->writeln('<info>Config successfully imported from: '.$importFile.'</info>');
93 93
 	}
94 94
 
95 95
 	/**
@@ -143,10 +143,10 @@  discard block
 block discarded – undo
143 143
 		$additionalKeys = array_diff($arrayKeys, $this->validRootKeys);
144 144
 		$commonKeys = array_intersect($arrayKeys, $this->validRootKeys);
145 145
 		if (!empty($additionalKeys)) {
146
-			throw new \UnexpectedValueException('Found invalid entries in root: ' . implode(', ', $additionalKeys));
146
+			throw new \UnexpectedValueException('Found invalid entries in root: '.implode(', ', $additionalKeys));
147 147
 		}
148 148
 		if (empty($commonKeys)) {
149
-			throw new \UnexpectedValueException('At least one key of the following is expected: ' . implode(', ', $this->validRootKeys));
149
+			throw new \UnexpectedValueException('At least one key of the following is expected: '.implode(', ', $this->validRootKeys));
150 150
 		}
151 151
 
152 152
 		if (isset($array['system'])) {
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
 	 */
175 175
 	protected function checkTypeRecursively($configValue, $configName) {
176 176
 		if (!is_array($configValue) && !is_bool($configValue) && !is_int($configValue) && !is_string($configValue) && !is_null($configValue)) {
177
-			throw new \UnexpectedValueException('Invalid system config value for "' . $configName . '". Only arrays, bools, integers, strings and null (delete) are allowed.');
177
+			throw new \UnexpectedValueException('Invalid system config value for "'.$configName.'". Only arrays, bools, integers, strings and null (delete) are allowed.');
178 178
 		}
179 179
 		if (is_array($configValue)) {
180 180
 			foreach ($configValue as $key => $value) {
@@ -192,7 +192,7 @@  discard block
 block discarded – undo
192 192
 		foreach ($array as $app => $configs) {
193 193
 			foreach ($configs as $name => $value) {
194 194
 				if (!is_int($value) && !is_string($value) && !is_null($value)) {
195
-					throw new \UnexpectedValueException('Invalid app config value for "' . $app . '":"' . $name . '". Only integers, strings and null (delete) are allowed.');
195
+					throw new \UnexpectedValueException('Invalid app config value for "'.$app.'":"'.$name.'". Only integers, strings and null (delete) are allowed.');
196 196
 				}
197 197
 			}
198 198
 		}
Please login to merge, or discard this patch.
Indentation   +189 added lines, -189 removed lines patch added patch discarded remove patch
@@ -33,193 +33,193 @@
 block discarded – undo
33 33
 use Symfony\Component\Console\Output\OutputInterface;
34 34
 
35 35
 class Import extends Command implements CompletionAwareInterface  {
36
-	protected $validRootKeys = ['system', 'apps'];
37
-
38
-	/** @var IConfig */
39
-	protected $config;
40
-
41
-	/**
42
-	 * @param IConfig $config
43
-	 */
44
-	public function __construct(IConfig $config) {
45
-		parent::__construct();
46
-		$this->config = $config;
47
-	}
48
-
49
-	protected function configure() {
50
-		$this
51
-			->setName('config:import')
52
-			->setDescription('Import a list of configs')
53
-			->addArgument(
54
-				'file',
55
-				InputArgument::OPTIONAL,
56
-				'File with the json array to import'
57
-			)
58
-		;
59
-	}
60
-
61
-	protected function execute(InputInterface $input, OutputInterface $output) {
62
-		$importFile = $input->getArgument('file');
63
-		if ($importFile !== null) {
64
-			$content = $this->getArrayFromFile($importFile);
65
-		} else {
66
-			$content = $this->getArrayFromStdin();
67
-		}
68
-
69
-		try {
70
-			$configs = $this->validateFileContent($content);
71
-		} catch (\UnexpectedValueException $e) {
72
-			$output->writeln('<error>' . $e->getMessage(). '</error>');
73
-			return;
74
-		}
75
-
76
-		if (!empty($configs['system'])) {
77
-			$this->config->setSystemValues($configs['system']);
78
-		}
79
-
80
-		if (!empty($configs['apps'])) {
81
-			foreach ($configs['apps'] as $app => $appConfigs) {
82
-				foreach ($appConfigs as $key => $value) {
83
-					if ($value === null) {
84
-						$this->config->deleteAppValue($app, $key);
85
-					} else {
86
-						$this->config->setAppValue($app, $key, $value);
87
-					}
88
-				}
89
-			}
90
-		}
91
-
92
-		$output->writeln('<info>Config successfully imported from: ' . $importFile . '</info>');
93
-	}
94
-
95
-	/**
96
-	 * Get the content from stdin ("config:import < file.json")
97
-	 *
98
-	 * @return string
99
-	 */
100
-	protected function getArrayFromStdin() {
101
-		// Read from stdin. stream_set_blocking is used to prevent blocking
102
-		// when nothing is passed via stdin.
103
-		stream_set_blocking(STDIN, 0);
104
-		$content = file_get_contents('php://stdin');
105
-		stream_set_blocking(STDIN, 1);
106
-		return $content;
107
-	}
108
-
109
-	/**
110
-	 * Get the content of the specified file ("config:import file.json")
111
-	 *
112
-	 * @param string $importFile
113
-	 * @return string
114
-	 */
115
-	protected function getArrayFromFile($importFile) {
116
-		return file_get_contents($importFile);
117
-	}
118
-
119
-	/**
120
-	 * @param string $content
121
-	 * @return array
122
-	 * @throws \UnexpectedValueException when the array is invalid
123
-	 */
124
-	protected function validateFileContent($content) {
125
-		$decodedContent = json_decode($content, true);
126
-		if (!is_array($decodedContent) || empty($decodedContent)) {
127
-			throw new \UnexpectedValueException('The file must contain a valid json array');
128
-		}
129
-
130
-		$this->validateArray($decodedContent);
131
-
132
-		return $decodedContent;
133
-	}
134
-
135
-	/**
136
-	 * Validates that the array only contains `system` and `apps`
137
-	 *
138
-	 * @param array $array
139
-	 */
140
-	protected function validateArray($array) {
141
-		$arrayKeys = array_keys($array);
142
-		$additionalKeys = array_diff($arrayKeys, $this->validRootKeys);
143
-		$commonKeys = array_intersect($arrayKeys, $this->validRootKeys);
144
-		if (!empty($additionalKeys)) {
145
-			throw new \UnexpectedValueException('Found invalid entries in root: ' . implode(', ', $additionalKeys));
146
-		}
147
-		if (empty($commonKeys)) {
148
-			throw new \UnexpectedValueException('At least one key of the following is expected: ' . implode(', ', $this->validRootKeys));
149
-		}
150
-
151
-		if (isset($array['system'])) {
152
-			if (is_array($array['system'])) {
153
-				foreach ($array['system'] as $name => $value) {
154
-					$this->checkTypeRecursively($value, $name);
155
-				}
156
-			} else {
157
-				throw new \UnexpectedValueException('The system config array is not an array');
158
-			}
159
-		}
160
-
161
-		if (isset($array['apps'])) {
162
-			if (is_array($array['apps'])) {
163
-				$this->validateAppsArray($array['apps']);
164
-			} else {
165
-				throw new \UnexpectedValueException('The apps config array is not an array');
166
-			}
167
-		}
168
-	}
169
-
170
-	/**
171
-	 * @param mixed $configValue
172
-	 * @param string $configName
173
-	 */
174
-	protected function checkTypeRecursively($configValue, $configName) {
175
-		if (!is_array($configValue) && !is_bool($configValue) && !is_int($configValue) && !is_string($configValue) && !is_null($configValue)) {
176
-			throw new \UnexpectedValueException('Invalid system config value for "' . $configName . '". Only arrays, bools, integers, strings and null (delete) are allowed.');
177
-		}
178
-		if (is_array($configValue)) {
179
-			foreach ($configValue as $key => $value) {
180
-				$this->checkTypeRecursively($value, $configName);
181
-			}
182
-		}
183
-	}
184
-
185
-	/**
186
-	 * Validates that app configs are only integers and strings
187
-	 *
188
-	 * @param array $array
189
-	 */
190
-	protected function validateAppsArray($array) {
191
-		foreach ($array as $app => $configs) {
192
-			foreach ($configs as $name => $value) {
193
-				if (!is_int($value) && !is_string($value) && !is_null($value)) {
194
-					throw new \UnexpectedValueException('Invalid app config value for "' . $app . '":"' . $name . '". Only integers, strings and null (delete) are allowed.');
195
-				}
196
-			}
197
-		}
198
-	}
199
-
200
-	/**
201
-	 * @param string $optionName
202
-	 * @param CompletionContext $context
203
-	 * @return string[]
204
-	 */
205
-	public function completeOptionValues($optionName, CompletionContext $context) {
206
-		return [];
207
-	}
208
-
209
-	/**
210
-	 * @param string $argumentName
211
-	 * @param CompletionContext $context
212
-	 * @return string[]
213
-	 */
214
-	public function completeArgumentValues($argumentName, CompletionContext $context) {
215
-		if ($argumentName === 'file') {
216
-			$helper = new ShellPathCompletion(
217
-				$this->getName(),
218
-				'file',
219
-				Completion::TYPE_ARGUMENT
220
-			);
221
-			return $helper->run();
222
-		}
223
-		return [];
224
-	}
36
+    protected $validRootKeys = ['system', 'apps'];
37
+
38
+    /** @var IConfig */
39
+    protected $config;
40
+
41
+    /**
42
+     * @param IConfig $config
43
+     */
44
+    public function __construct(IConfig $config) {
45
+        parent::__construct();
46
+        $this->config = $config;
47
+    }
48
+
49
+    protected function configure() {
50
+        $this
51
+            ->setName('config:import')
52
+            ->setDescription('Import a list of configs')
53
+            ->addArgument(
54
+                'file',
55
+                InputArgument::OPTIONAL,
56
+                'File with the json array to import'
57
+            )
58
+        ;
59
+    }
60
+
61
+    protected function execute(InputInterface $input, OutputInterface $output) {
62
+        $importFile = $input->getArgument('file');
63
+        if ($importFile !== null) {
64
+            $content = $this->getArrayFromFile($importFile);
65
+        } else {
66
+            $content = $this->getArrayFromStdin();
67
+        }
68
+
69
+        try {
70
+            $configs = $this->validateFileContent($content);
71
+        } catch (\UnexpectedValueException $e) {
72
+            $output->writeln('<error>' . $e->getMessage(). '</error>');
73
+            return;
74
+        }
75
+
76
+        if (!empty($configs['system'])) {
77
+            $this->config->setSystemValues($configs['system']);
78
+        }
79
+
80
+        if (!empty($configs['apps'])) {
81
+            foreach ($configs['apps'] as $app => $appConfigs) {
82
+                foreach ($appConfigs as $key => $value) {
83
+                    if ($value === null) {
84
+                        $this->config->deleteAppValue($app, $key);
85
+                    } else {
86
+                        $this->config->setAppValue($app, $key, $value);
87
+                    }
88
+                }
89
+            }
90
+        }
91
+
92
+        $output->writeln('<info>Config successfully imported from: ' . $importFile . '</info>');
93
+    }
94
+
95
+    /**
96
+     * Get the content from stdin ("config:import < file.json")
97
+     *
98
+     * @return string
99
+     */
100
+    protected function getArrayFromStdin() {
101
+        // Read from stdin. stream_set_blocking is used to prevent blocking
102
+        // when nothing is passed via stdin.
103
+        stream_set_blocking(STDIN, 0);
104
+        $content = file_get_contents('php://stdin');
105
+        stream_set_blocking(STDIN, 1);
106
+        return $content;
107
+    }
108
+
109
+    /**
110
+     * Get the content of the specified file ("config:import file.json")
111
+     *
112
+     * @param string $importFile
113
+     * @return string
114
+     */
115
+    protected function getArrayFromFile($importFile) {
116
+        return file_get_contents($importFile);
117
+    }
118
+
119
+    /**
120
+     * @param string $content
121
+     * @return array
122
+     * @throws \UnexpectedValueException when the array is invalid
123
+     */
124
+    protected function validateFileContent($content) {
125
+        $decodedContent = json_decode($content, true);
126
+        if (!is_array($decodedContent) || empty($decodedContent)) {
127
+            throw new \UnexpectedValueException('The file must contain a valid json array');
128
+        }
129
+
130
+        $this->validateArray($decodedContent);
131
+
132
+        return $decodedContent;
133
+    }
134
+
135
+    /**
136
+     * Validates that the array only contains `system` and `apps`
137
+     *
138
+     * @param array $array
139
+     */
140
+    protected function validateArray($array) {
141
+        $arrayKeys = array_keys($array);
142
+        $additionalKeys = array_diff($arrayKeys, $this->validRootKeys);
143
+        $commonKeys = array_intersect($arrayKeys, $this->validRootKeys);
144
+        if (!empty($additionalKeys)) {
145
+            throw new \UnexpectedValueException('Found invalid entries in root: ' . implode(', ', $additionalKeys));
146
+        }
147
+        if (empty($commonKeys)) {
148
+            throw new \UnexpectedValueException('At least one key of the following is expected: ' . implode(', ', $this->validRootKeys));
149
+        }
150
+
151
+        if (isset($array['system'])) {
152
+            if (is_array($array['system'])) {
153
+                foreach ($array['system'] as $name => $value) {
154
+                    $this->checkTypeRecursively($value, $name);
155
+                }
156
+            } else {
157
+                throw new \UnexpectedValueException('The system config array is not an array');
158
+            }
159
+        }
160
+
161
+        if (isset($array['apps'])) {
162
+            if (is_array($array['apps'])) {
163
+                $this->validateAppsArray($array['apps']);
164
+            } else {
165
+                throw new \UnexpectedValueException('The apps config array is not an array');
166
+            }
167
+        }
168
+    }
169
+
170
+    /**
171
+     * @param mixed $configValue
172
+     * @param string $configName
173
+     */
174
+    protected function checkTypeRecursively($configValue, $configName) {
175
+        if (!is_array($configValue) && !is_bool($configValue) && !is_int($configValue) && !is_string($configValue) && !is_null($configValue)) {
176
+            throw new \UnexpectedValueException('Invalid system config value for "' . $configName . '". Only arrays, bools, integers, strings and null (delete) are allowed.');
177
+        }
178
+        if (is_array($configValue)) {
179
+            foreach ($configValue as $key => $value) {
180
+                $this->checkTypeRecursively($value, $configName);
181
+            }
182
+        }
183
+    }
184
+
185
+    /**
186
+     * Validates that app configs are only integers and strings
187
+     *
188
+     * @param array $array
189
+     */
190
+    protected function validateAppsArray($array) {
191
+        foreach ($array as $app => $configs) {
192
+            foreach ($configs as $name => $value) {
193
+                if (!is_int($value) && !is_string($value) && !is_null($value)) {
194
+                    throw new \UnexpectedValueException('Invalid app config value for "' . $app . '":"' . $name . '". Only integers, strings and null (delete) are allowed.');
195
+                }
196
+            }
197
+        }
198
+    }
199
+
200
+    /**
201
+     * @param string $optionName
202
+     * @param CompletionContext $context
203
+     * @return string[]
204
+     */
205
+    public function completeOptionValues($optionName, CompletionContext $context) {
206
+        return [];
207
+    }
208
+
209
+    /**
210
+     * @param string $argumentName
211
+     * @param CompletionContext $context
212
+     * @return string[]
213
+     */
214
+    public function completeArgumentValues($argumentName, CompletionContext $context) {
215
+        if ($argumentName === 'file') {
216
+            $helper = new ShellPathCompletion(
217
+                $this->getName(),
218
+                'file',
219
+                Completion::TYPE_ARGUMENT
220
+            );
221
+            return $helper->run();
222
+        }
223
+        return [];
224
+    }
225 225
 }
Please login to merge, or discard this patch.
core/Command/Maintenance/DataFingerprint.php 1 patch
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -30,25 +30,25 @@
 block discarded – undo
30 30
 
31 31
 class DataFingerprint extends Command {
32 32
 
33
-	/** @var IConfig */
34
-	protected $config;
35
-	/** @var ITimeFactory */
36
-	protected $timeFactory;
33
+    /** @var IConfig */
34
+    protected $config;
35
+    /** @var ITimeFactory */
36
+    protected $timeFactory;
37 37
 
38
-	public function __construct(IConfig $config,
39
-								ITimeFactory $timeFactory) {
40
-		$this->config = $config;
41
-		$this->timeFactory = $timeFactory;
42
-		parent::__construct();
43
-	}
38
+    public function __construct(IConfig $config,
39
+                                ITimeFactory $timeFactory) {
40
+        $this->config = $config;
41
+        $this->timeFactory = $timeFactory;
42
+        parent::__construct();
43
+    }
44 44
 
45
-	protected function configure() {
46
-		$this
47
-			->setName('maintenance:data-fingerprint')
48
-			->setDescription('update the systems data-fingerprint after a backup is restored');
49
-	}
45
+    protected function configure() {
46
+        $this
47
+            ->setName('maintenance:data-fingerprint')
48
+            ->setDescription('update the systems data-fingerprint after a backup is restored');
49
+    }
50 50
 
51
-	protected function execute(InputInterface $input, OutputInterface $output) {
52
-		$this->config->setSystemValue('data-fingerprint', md5($this->timeFactory->getTime()));
53
-	}
51
+    protected function execute(InputInterface $input, OutputInterface $output) {
52
+        $this->config->setSystemValue('data-fingerprint', md5($this->timeFactory->getTime()));
53
+    }
54 54
 }
Please login to merge, or discard this patch.