Completed
Pull Request — master (#3966)
by Morris
20:02 queued 08:06
created
lib/private/Config.php 2 patches
Indentation   +206 added lines, -206 removed lines patch added patch discarded remove patch
@@ -40,238 +40,238 @@
 block discarded – undo
40 40
  */
41 41
 class Config {
42 42
 
43
-	const ENV_PREFIX = 'NC_';
43
+    const ENV_PREFIX = 'NC_';
44 44
 
45
-	/** @var array Associative array ($key => $value) */
46
-	protected $cache = array();
47
-	/** @var string */
48
-	protected $configDir;
49
-	/** @var string */
50
-	protected $configFilePath;
51
-	/** @var string */
52
-	protected $configFileName;
45
+    /** @var array Associative array ($key => $value) */
46
+    protected $cache = array();
47
+    /** @var string */
48
+    protected $configDir;
49
+    /** @var string */
50
+    protected $configFilePath;
51
+    /** @var string */
52
+    protected $configFileName;
53 53
 
54
-	/**
55
-	 * @param string $configDir Path to the config dir, needs to end with '/'
56
-	 * @param string $fileName (Optional) Name of the config file. Defaults to config.php
57
-	 */
58
-	public function __construct($configDir, $fileName = 'config.php') {
59
-		$this->configDir = $configDir;
60
-		$this->configFilePath = $this->configDir.$fileName;
61
-		$this->configFileName = $fileName;
62
-		$this->readData();
63
-	}
54
+    /**
55
+     * @param string $configDir Path to the config dir, needs to end with '/'
56
+     * @param string $fileName (Optional) Name of the config file. Defaults to config.php
57
+     */
58
+    public function __construct($configDir, $fileName = 'config.php') {
59
+        $this->configDir = $configDir;
60
+        $this->configFilePath = $this->configDir.$fileName;
61
+        $this->configFileName = $fileName;
62
+        $this->readData();
63
+    }
64 64
 
65
-	/**
66
-	 * Lists all available config keys
67
-	 *
68
-	 * Please note that it does not return the values.
69
-	 *
70
-	 * @return array an array of key names
71
-	 */
72
-	public function getKeys() {
73
-		return array_keys($this->cache);
74
-	}
65
+    /**
66
+     * Lists all available config keys
67
+     *
68
+     * Please note that it does not return the values.
69
+     *
70
+     * @return array an array of key names
71
+     */
72
+    public function getKeys() {
73
+        return array_keys($this->cache);
74
+    }
75 75
 
76
-	/**
77
-	 * Returns a config value
78
-	 *
79
-	 * gets its value from an `NC_` prefixed environment variable
80
-	 * if it doesn't exist from config.php
81
-	 * if this doesn't exist either, it will return the given `$default`
82
-	 *
83
-	 * @param string $key key
84
-	 * @param mixed $default = null default value
85
-	 * @return mixed the value or $default
86
-	 */
87
-	public function getValue($key, $default = null) {
76
+    /**
77
+     * Returns a config value
78
+     *
79
+     * gets its value from an `NC_` prefixed environment variable
80
+     * if it doesn't exist from config.php
81
+     * if this doesn't exist either, it will return the given `$default`
82
+     *
83
+     * @param string $key key
84
+     * @param mixed $default = null default value
85
+     * @return mixed the value or $default
86
+     */
87
+    public function getValue($key, $default = null) {
88 88
 
89
-		if (isset($this->cache[$key])) {
90
-			return $this->cache[$key];
91
-		}
89
+        if (isset($this->cache[$key])) {
90
+            return $this->cache[$key];
91
+        }
92 92
 
93
-		$envValue = getenv(self::ENV_PREFIX . $key);
94
-		if ($envValue !== false) {
95
-			$this->cache[$key] = $envValue;
96
-			return $envValue;
97
-		}
93
+        $envValue = getenv(self::ENV_PREFIX . $key);
94
+        if ($envValue !== false) {
95
+            $this->cache[$key] = $envValue;
96
+            return $envValue;
97
+        }
98 98
 
99
-		return $default;
100
-	}
99
+        return $default;
100
+    }
101 101
 
102
-	/**
103
-	 * Sets and deletes values and writes the config.php
104
-	 *
105
-	 * @param array $configs Associative array with `key => value` pairs
106
-	 *                       If value is null, the config key will be deleted
107
-	 */
108
-	public function setValues(array $configs) {
109
-		$needsUpdate = false;
110
-		foreach ($configs as $key => $value) {
111
-			if ($value !== null) {
112
-				$needsUpdate |= $this->set($key, $value);
113
-			} else {
114
-				$needsUpdate |= $this->delete($key);
115
-			}
116
-		}
102
+    /**
103
+     * Sets and deletes values and writes the config.php
104
+     *
105
+     * @param array $configs Associative array with `key => value` pairs
106
+     *                       If value is null, the config key will be deleted
107
+     */
108
+    public function setValues(array $configs) {
109
+        $needsUpdate = false;
110
+        foreach ($configs as $key => $value) {
111
+            if ($value !== null) {
112
+                $needsUpdate |= $this->set($key, $value);
113
+            } else {
114
+                $needsUpdate |= $this->delete($key);
115
+            }
116
+        }
117 117
 
118
-		if ($needsUpdate) {
119
-			// Write changes
120
-			$this->writeData();
121
-		}
122
-	}
118
+        if ($needsUpdate) {
119
+            // Write changes
120
+            $this->writeData();
121
+        }
122
+    }
123 123
 
124
-	/**
125
-	 * Sets the value and writes it to config.php if required
126
-	 *
127
-	 * @param string $key key
128
-	 * @param mixed $value value
129
-	 */
130
-	public function setValue($key, $value) {
131
-		if ($this->set($key, $value)) {
132
-			// Write changes
133
-			$this->writeData();
134
-		}
135
-	}
124
+    /**
125
+     * Sets the value and writes it to config.php if required
126
+     *
127
+     * @param string $key key
128
+     * @param mixed $value value
129
+     */
130
+    public function setValue($key, $value) {
131
+        if ($this->set($key, $value)) {
132
+            // Write changes
133
+            $this->writeData();
134
+        }
135
+    }
136 136
 
137
-	/**
138
-	 * This function sets the value
139
-	 *
140
-	 * @param string $key key
141
-	 * @param mixed $value value
142
-	 * @return bool True if the file needs to be updated, false otherwise
143
-	 */
144
-	protected function set($key, $value) {
145
-		if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
146
-			// Add change
147
-			$this->cache[$key] = $value;
148
-			return true;
149
-		}
137
+    /**
138
+     * This function sets the value
139
+     *
140
+     * @param string $key key
141
+     * @param mixed $value value
142
+     * @return bool True if the file needs to be updated, false otherwise
143
+     */
144
+    protected function set($key, $value) {
145
+        if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
146
+            // Add change
147
+            $this->cache[$key] = $value;
148
+            return true;
149
+        }
150 150
 
151
-		return false;
152
-	}
151
+        return false;
152
+    }
153 153
 
154
-	/**
155
-	 * Removes a key from the config and removes it from config.php if required
156
-	 * @param string $key
157
-	 */
158
-	public function deleteKey($key) {
159
-		if ($this->delete($key)) {
160
-			// Write changes
161
-			$this->writeData();
162
-		}
163
-	}
154
+    /**
155
+     * Removes a key from the config and removes it from config.php if required
156
+     * @param string $key
157
+     */
158
+    public function deleteKey($key) {
159
+        if ($this->delete($key)) {
160
+            // Write changes
161
+            $this->writeData();
162
+        }
163
+    }
164 164
 
165
-	/**
166
-	 * This function removes a key from the config
167
-	 *
168
-	 * @param string $key
169
-	 * @return bool True if the file needs to be updated, false otherwise
170
-	 */
171
-	protected function delete($key) {
172
-		if (isset($this->cache[$key])) {
173
-			// Delete key from cache
174
-			unset($this->cache[$key]);
175
-			return true;
176
-		}
177
-		return false;
178
-	}
165
+    /**
166
+     * This function removes a key from the config
167
+     *
168
+     * @param string $key
169
+     * @return bool True if the file needs to be updated, false otherwise
170
+     */
171
+    protected function delete($key) {
172
+        if (isset($this->cache[$key])) {
173
+            // Delete key from cache
174
+            unset($this->cache[$key]);
175
+            return true;
176
+        }
177
+        return false;
178
+    }
179 179
 
180
-	/**
181
-	 * Loads the config file
182
-	 *
183
-	 * Reads the config file and saves it to the cache
184
-	 *
185
-	 * @throws \Exception If no lock could be acquired or the config file has not been found
186
-	 */
187
-	private function readData() {
188
-		// Default config should always get loaded
189
-		$configFiles = array($this->configFilePath);
180
+    /**
181
+     * Loads the config file
182
+     *
183
+     * Reads the config file and saves it to the cache
184
+     *
185
+     * @throws \Exception If no lock could be acquired or the config file has not been found
186
+     */
187
+    private function readData() {
188
+        // Default config should always get loaded
189
+        $configFiles = array($this->configFilePath);
190 190
 
191
-		// Add all files in the config dir ending with the same file name
192
-		$extra = glob($this->configDir.'*.'.$this->configFileName);
193
-		if (is_array($extra)) {
194
-			natsort($extra);
195
-			$configFiles = array_merge($configFiles, $extra);
196
-		}
191
+        // Add all files in the config dir ending with the same file name
192
+        $extra = glob($this->configDir.'*.'.$this->configFileName);
193
+        if (is_array($extra)) {
194
+            natsort($extra);
195
+            $configFiles = array_merge($configFiles, $extra);
196
+        }
197 197
 
198
-		// Include file and merge config
199
-		foreach ($configFiles as $file) {
200
-			$fileExistsAndIsReadable = file_exists($file) && is_readable($file);
201
-			$filePointer = $fileExistsAndIsReadable ? fopen($file, 'r') : false;
202
-			if($file === $this->configFilePath &&
203
-				$filePointer === false) {
204
-				// Opening the main config might not be possible, e.g. if the wrong
205
-				// permissions are set (likely on a new installation)
206
-				continue;
207
-			}
198
+        // Include file and merge config
199
+        foreach ($configFiles as $file) {
200
+            $fileExistsAndIsReadable = file_exists($file) && is_readable($file);
201
+            $filePointer = $fileExistsAndIsReadable ? fopen($file, 'r') : false;
202
+            if($file === $this->configFilePath &&
203
+                $filePointer === false) {
204
+                // Opening the main config might not be possible, e.g. if the wrong
205
+                // permissions are set (likely on a new installation)
206
+                continue;
207
+            }
208 208
 
209
-			// Try to acquire a file lock
210
-			if(!flock($filePointer, LOCK_SH)) {
211
-				throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file));
212
-			}
209
+            // Try to acquire a file lock
210
+            if(!flock($filePointer, LOCK_SH)) {
211
+                throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file));
212
+            }
213 213
 
214
-			unset($CONFIG);
215
-			include $file;
216
-			if(isset($CONFIG) && is_array($CONFIG)) {
217
-				$this->cache = array_merge($this->cache, $CONFIG);
218
-			}
214
+            unset($CONFIG);
215
+            include $file;
216
+            if(isset($CONFIG) && is_array($CONFIG)) {
217
+                $this->cache = array_merge($this->cache, $CONFIG);
218
+            }
219 219
 
220
-			// Close the file pointer and release the lock
221
-			flock($filePointer, LOCK_UN);
222
-			fclose($filePointer);
223
-		}
224
-	}
220
+            // Close the file pointer and release the lock
221
+            flock($filePointer, LOCK_UN);
222
+            fclose($filePointer);
223
+        }
224
+    }
225 225
 
226
-	/**
227
-	 * Writes the config file
228
-	 *
229
-	 * Saves the config to the config file.
230
-	 *
231
-	 * @throws HintException If the config file cannot be written to
232
-	 * @throws \Exception If no file lock can be acquired
233
-	 */
234
-	private function writeData() {
235
-		// Create a php file ...
236
-		$content = "<?php\n";
237
-		$content .= '$CONFIG = ';
238
-		$content .= var_export($this->cache, true);
239
-		$content .= ";\n";
226
+    /**
227
+     * Writes the config file
228
+     *
229
+     * Saves the config to the config file.
230
+     *
231
+     * @throws HintException If the config file cannot be written to
232
+     * @throws \Exception If no file lock can be acquired
233
+     */
234
+    private function writeData() {
235
+        // Create a php file ...
236
+        $content = "<?php\n";
237
+        $content .= '$CONFIG = ';
238
+        $content .= var_export($this->cache, true);
239
+        $content .= ";\n";
240 240
 
241
-		touch ($this->configFilePath);
242
-		$filePointer = fopen($this->configFilePath, 'r+');
241
+        touch ($this->configFilePath);
242
+        $filePointer = fopen($this->configFilePath, 'r+');
243 243
 
244
-		// Prevent others not to read the config
245
-		chmod($this->configFilePath, 0640);
244
+        // Prevent others not to read the config
245
+        chmod($this->configFilePath, 0640);
246 246
 
247
-		// File does not exist, this can happen when doing a fresh install
248
-		if(!is_resource ($filePointer)) {
249
-			// TODO fix this via DI once it is very clear that this doesn't cause side effects due to initialization order
250
-			// currently this breaks app routes but also could have other side effects especially during setup and exception handling
251
-			$url = \OC::$server->getURLGenerator()->linkToDocs('admin-dir_permissions');
252
-			throw new HintException(
253
-				"Can't write into config directory!",
254
-				'This can usually be fixed by '
255
-				.'<a href="' . $url . '" target="_blank" rel="noreferrer">giving the webserver write access to the config directory</a>.');
256
-		}
247
+        // File does not exist, this can happen when doing a fresh install
248
+        if(!is_resource ($filePointer)) {
249
+            // TODO fix this via DI once it is very clear that this doesn't cause side effects due to initialization order
250
+            // currently this breaks app routes but also could have other side effects especially during setup and exception handling
251
+            $url = \OC::$server->getURLGenerator()->linkToDocs('admin-dir_permissions');
252
+            throw new HintException(
253
+                "Can't write into config directory!",
254
+                'This can usually be fixed by '
255
+                .'<a href="' . $url . '" target="_blank" rel="noreferrer">giving the webserver write access to the config directory</a>.');
256
+        }
257 257
 
258
-		// Try to acquire a file lock
259
-		if(!flock($filePointer, LOCK_EX)) {
260
-			throw new \Exception(sprintf('Could not acquire an exclusive lock on the config file %s', $this->configFilePath));
261
-		}
258
+        // Try to acquire a file lock
259
+        if(!flock($filePointer, LOCK_EX)) {
260
+            throw new \Exception(sprintf('Could not acquire an exclusive lock on the config file %s', $this->configFilePath));
261
+        }
262 262
 
263
-		// Write the config and release the lock
264
-		ftruncate ($filePointer, 0);
265
-		fwrite($filePointer, $content);
266
-		fflush($filePointer);
267
-		flock($filePointer, LOCK_UN);
268
-		fclose($filePointer);
263
+        // Write the config and release the lock
264
+        ftruncate ($filePointer, 0);
265
+        fwrite($filePointer, $content);
266
+        fflush($filePointer);
267
+        flock($filePointer, LOCK_UN);
268
+        fclose($filePointer);
269 269
 
270
-		// Try invalidating the opcache just for the file we wrote...
271
-		if (!\OC_Util::deleteFromOpcodeCache($this->configFilePath)) {
272
-			// But if that doesn't work, clear the whole cache.
273
-			\OC_Util::clearOpcodeCache();
274
-		}
275
-	}
270
+        // Try invalidating the opcache just for the file we wrote...
271
+        if (!\OC_Util::deleteFromOpcodeCache($this->configFilePath)) {
272
+            // But if that doesn't work, clear the whole cache.
273
+            \OC_Util::clearOpcodeCache();
274
+        }
275
+    }
276 276
 }
277 277
 
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
 			return $this->cache[$key];
91 91
 		}
92 92
 
93
-		$envValue = getenv(self::ENV_PREFIX . $key);
93
+		$envValue = getenv(self::ENV_PREFIX.$key);
94 94
 		if ($envValue !== false) {
95 95
 			$this->cache[$key] = $envValue;
96 96
 			return $envValue;
@@ -199,7 +199,7 @@  discard block
 block discarded – undo
199 199
 		foreach ($configFiles as $file) {
200 200
 			$fileExistsAndIsReadable = file_exists($file) && is_readable($file);
201 201
 			$filePointer = $fileExistsAndIsReadable ? fopen($file, 'r') : false;
202
-			if($file === $this->configFilePath &&
202
+			if ($file === $this->configFilePath &&
203 203
 				$filePointer === false) {
204 204
 				// Opening the main config might not be possible, e.g. if the wrong
205 205
 				// permissions are set (likely on a new installation)
@@ -207,13 +207,13 @@  discard block
 block discarded – undo
207 207
 			}
208 208
 
209 209
 			// Try to acquire a file lock
210
-			if(!flock($filePointer, LOCK_SH)) {
210
+			if (!flock($filePointer, LOCK_SH)) {
211 211
 				throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file));
212 212
 			}
213 213
 
214 214
 			unset($CONFIG);
215 215
 			include $file;
216
-			if(isset($CONFIG) && is_array($CONFIG)) {
216
+			if (isset($CONFIG) && is_array($CONFIG)) {
217 217
 				$this->cache = array_merge($this->cache, $CONFIG);
218 218
 			}
219 219
 
@@ -238,30 +238,30 @@  discard block
 block discarded – undo
238 238
 		$content .= var_export($this->cache, true);
239 239
 		$content .= ";\n";
240 240
 
241
-		touch ($this->configFilePath);
241
+		touch($this->configFilePath);
242 242
 		$filePointer = fopen($this->configFilePath, 'r+');
243 243
 
244 244
 		// Prevent others not to read the config
245 245
 		chmod($this->configFilePath, 0640);
246 246
 
247 247
 		// File does not exist, this can happen when doing a fresh install
248
-		if(!is_resource ($filePointer)) {
248
+		if (!is_resource($filePointer)) {
249 249
 			// TODO fix this via DI once it is very clear that this doesn't cause side effects due to initialization order
250 250
 			// currently this breaks app routes but also could have other side effects especially during setup and exception handling
251 251
 			$url = \OC::$server->getURLGenerator()->linkToDocs('admin-dir_permissions');
252 252
 			throw new HintException(
253 253
 				"Can't write into config directory!",
254 254
 				'This can usually be fixed by '
255
-				.'<a href="' . $url . '" target="_blank" rel="noreferrer">giving the webserver write access to the config directory</a>.');
255
+				.'<a href="'.$url.'" target="_blank" rel="noreferrer">giving the webserver write access to the config directory</a>.');
256 256
 		}
257 257
 
258 258
 		// Try to acquire a file lock
259
-		if(!flock($filePointer, LOCK_EX)) {
259
+		if (!flock($filePointer, LOCK_EX)) {
260 260
 			throw new \Exception(sprintf('Could not acquire an exclusive lock on the config file %s', $this->configFilePath));
261 261
 		}
262 262
 
263 263
 		// Write the config and release the lock
264
-		ftruncate ($filePointer, 0);
264
+		ftruncate($filePointer, 0);
265 265
 		fwrite($filePointer, $content);
266 266
 		fflush($filePointer);
267 267
 		flock($filePointer, LOCK_UN);
Please login to merge, or discard this patch.