Merge pull request #21283 from owncloud/cleanup_config

Cleanup OC_Config mess
remotes/origin/appinfo-object
Thomas Müller 2015-12-18 14:54:38 +07:00
commit a743047e82
10 changed files with 56 additions and 128 deletions

@ -112,6 +112,11 @@ class OC {
*/ */
public static $server = null; public static $server = null;
/**
* @var \OC\Config
*/
private static $config = null;
/** /**
* @throws \RuntimeException when the 3rdparty directory is missing or * @throws \RuntimeException when the 3rdparty directory is missing or
* the app path list is empty or contains an invalid path * the app path list is empty or contains an invalid path
@ -124,7 +129,7 @@ class OC {
} else { } else {
self::$configDir = OC::$SERVERROOT . '/config/'; self::$configDir = OC::$SERVERROOT . '/config/';
} }
OC_Config::$object = new \OC\Config(self::$configDir); self::$config = new \OC\Config(self::$configDir);
OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen(OC::$SERVERROOT))); OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen(OC::$SERVERROOT)));
/** /**
@ -137,7 +142,7 @@ class OC {
'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'],
], ],
]; ];
$fakeRequest = new \OC\AppFramework\Http\Request($params, null, new \OC\AllConfig(new \OC\SystemConfig())); $fakeRequest = new \OC\AppFramework\Http\Request($params, null, new \OC\AllConfig(new \OC\SystemConfig(self::$config)));
$scriptName = $fakeRequest->getScriptName(); $scriptName = $fakeRequest->getScriptName();
if (substr($scriptName, -1) == '/') { if (substr($scriptName, -1) == '/') {
$scriptName .= 'index.php'; $scriptName .= 'index.php';
@ -152,7 +157,7 @@ class OC {
if (OC::$CLI) { if (OC::$CLI) {
OC::$WEBROOT = OC_Config::getValue('overwritewebroot', ''); OC::$WEBROOT = self::$config->getValue('overwritewebroot', '');
} else { } else {
if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) {
OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI));
@ -165,7 +170,7 @@ class OC {
// This most likely means that we are calling from CLI. // This most likely means that we are calling from CLI.
// However some cron jobs still need to generate // However some cron jobs still need to generate
// a web URL, so we use overwritewebroot as a fallback. // a web URL, so we use overwritewebroot as a fallback.
OC::$WEBROOT = OC_Config::getValue('overwritewebroot', ''); OC::$WEBROOT = self::$config->getValue('overwritewebroot', '');
} }
// Resolve /owncloud to /owncloud/ to ensure to always have a trailing // Resolve /owncloud to /owncloud/ to ensure to always have a trailing
@ -178,8 +183,8 @@ class OC {
} }
// search the 3rdparty folder // search the 3rdparty folder
OC::$THIRDPARTYROOT = OC_Config::getValue('3rdpartyroot', null); OC::$THIRDPARTYROOT = self::$config->getValue('3rdpartyroot', null);
OC::$THIRDPARTYWEBROOT = OC_Config::getValue('3rdpartyurl', null); OC::$THIRDPARTYWEBROOT = self::$config->getValue('3rdpartyurl', null);
if (empty(OC::$THIRDPARTYROOT) && empty(OC::$THIRDPARTYWEBROOT)) { if (empty(OC::$THIRDPARTYROOT) && empty(OC::$THIRDPARTYWEBROOT)) {
if (file_exists(OC::$SERVERROOT . '/3rdparty')) { if (file_exists(OC::$SERVERROOT . '/3rdparty')) {
@ -197,7 +202,7 @@ class OC {
} }
// search the apps folder // search the apps folder
$config_paths = OC_Config::getValue('apps_paths', array()); $config_paths = self::$config->getValue('apps_paths', array());
if (!empty($config_paths)) { if (!empty($config_paths)) {
foreach ($config_paths as $paths) { foreach ($config_paths as $paths) {
if (isset($paths['url']) && isset($paths['path'])) { if (isset($paths['url']) && isset($paths['path'])) {
@ -517,7 +522,7 @@ class OC {
} }
// setup the basic server // setup the basic server
self::$server = new \OC\Server(\OC::$WEBROOT); self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
\OC::$server->getEventLogger()->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd); \OC::$server->getEventLogger()->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd);
\OC::$server->getEventLogger()->start('boot', 'Initialize'); \OC::$server->getEventLogger()->start('boot', 'Initialize');

@ -96,7 +96,7 @@ class OC_App {
* if $types is set, only apps of those types will be loaded * if $types is set, only apps of those types will be loaded
*/ */
public static function loadApps($types = null) { public static function loadApps($types = null) {
if (OC_Config::getValue('maintenance', false)) { if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
return false; return false;
} }
// Load the enabled apps here // Load the enabled apps here
@ -239,7 +239,7 @@ class OC_App {
* @return string[] * @return string[]
*/ */
public static function getEnabledApps($forceRefresh = false, $all = false) { public static function getEnabledApps($forceRefresh = false, $all = false) {
if (!OC_Config::getValue('installed', false)) { if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {
return array(); return array();
} }
// in incognito mode or when logged out, $user will be false, // in incognito mode or when logged out, $user will be false,
@ -374,7 +374,7 @@ class OC_App {
$settings = array(); $settings = array();
// by default, settings only contain the help menu // by default, settings only contain the help menu
if (OC_Util::getEditionString() === '' && if (OC_Util::getEditionString() === '' &&
OC_Config::getValue('knowledgebaseenabled', true) == true \OC::$server->getSystemConfig()->getValue('knowledgebaseenabled', true) == true
) { ) {
$settings = array( $settings = array(
array( array(
@ -455,7 +455,7 @@ class OC_App {
* @return string|false * @return string|false
*/ */
public static function getInstallPath() { public static function getInstallPath() {
if (OC_Config::getValue('appstoreenabled', true) == false) { if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) {
return false; return false;
} }

@ -657,7 +657,7 @@ class OC_Helper {
*/ */
public static function getStorageInfo($path, $rootInfo = null) { public static function getStorageInfo($path, $rootInfo = null) {
// return storage info without adding mount points // return storage info without adding mount points
$includeExtStorage = \OC_Config::getValue('quota_include_external_storage', false); $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false);
if (!$rootInfo) { if (!$rootInfo) {
$rootInfo = \OC\Files\Filesystem::getFileInfo($path, false); $rootInfo = \OC\Files\Filesystem::getFileInfo($path, false);

@ -1,94 +0,0 @@
<?php
/**
* @author Bart Visscher <bartv@thisnet.nl>
* @author Joas Schilling <nickvergessen@owncloud.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Lukas Reschke <lukas@owncloud.com>
* @author Michael Gapczynski <GapczynskiM@gmail.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
/**
* This class is responsible for reading and writing config.php, the very basic
* configuration file of ownCloud.
*
* @deprecated use \OC::$server->getConfig() to get an \OCP\Config instance
*/
class OC_Config {
/** @var \OC\Config */
public static $object;
/**
* Lists all available config keys
* @return array an array of key names
*
* This function returns all keys saved in config.php. Please note that it
* does not return the values.
*/
public static function getKeys() {
return self::$object->getKeys();
}
/**
* Gets a value from config.php
* @param string $key key
* @param mixed $default = null default value
* @return mixed the value or $default
*
* This function gets the value from config.php. If it does not exist,
* $default will be returned.
*/
public static function getValue($key, $default = null) {
return self::$object->getValue($key, $default);
}
/**
* Sets a value
* @param string $key key
* @param mixed $value value
*
* This function sets the value and writes the config.php.
*
*/
public static function setValue($key, $value) {
self::$object->setValue($key, $value);
}
/**
* Sets and deletes values and writes the config.php
*
* @param array $configs Associative array with `key => value` pairs
* If value is null, the config key will be deleted
*/
public static function setValues(array $configs) {
self::$object->setValues($configs);
}
/**
* Removes a key from the config
* @param string $key key
*
* This function removes a key from the config.php.
*/
public static function deleteKey($key) {
self::$object->deleteKey($key);
}
}

@ -84,8 +84,9 @@ class Server extends SimpleContainer implements IServerContainer {
/** /**
* @param string $webRoot * @param string $webRoot
* @param \OC\Config $config
*/ */
public function __construct($webRoot) { public function __construct($webRoot, \OC\Config $config) {
parent::__construct(); parent::__construct();
$this->webRoot = $webRoot; $this->webRoot = $webRoot;
@ -238,8 +239,8 @@ class Server extends SimpleContainer implements IServerContainer {
$c->getSystemConfig() $c->getSystemConfig()
); );
}); });
$this->registerService('SystemConfig', function ($c) { $this->registerService('SystemConfig', function ($c) use ($config) {
return new \OC\SystemConfig(); return new \OC\SystemConfig($config);
}); });
$this->registerService('AppConfig', function ($c) { $this->registerService('AppConfig', function ($c) {
return new \OC\AppConfig(\OC_DB::getConnection()); return new \OC\AppConfig(\OC_DB::getConnection());

@ -44,12 +44,19 @@ class SystemConfig {
'objectstore' => ['arguments' => ['password' => true]], 'objectstore' => ['arguments' => ['password' => true]],
]; ];
/** @var Config */
private $config;
public function __construct(Config $config) {
$this->config = $config;
}
/** /**
* Lists all available config keys * Lists all available config keys
* @return array an array of key names * @return array an array of key names
*/ */
public function getKeys() { public function getKeys() {
return \OC_Config::getKeys(); return $this->config->getKeys();
} }
/** /**
@ -59,7 +66,7 @@ class SystemConfig {
* @param mixed $value the value that should be stored * @param mixed $value the value that should be stored
*/ */
public function setValue($key, $value) { public function setValue($key, $value) {
\OC_Config::setValue($key, $value); $this->config->setValue($key, $value);
} }
/** /**
@ -69,7 +76,7 @@ class SystemConfig {
* If value is null, the config key will be deleted * If value is null, the config key will be deleted
*/ */
public function setValues(array $configs) { public function setValues(array $configs) {
\OC_Config::setValues($configs); $this->config->setValues($configs);
} }
/** /**
@ -80,7 +87,7 @@ class SystemConfig {
* @return mixed the value or $default * @return mixed the value or $default
*/ */
public function getValue($key, $default = '') { public function getValue($key, $default = '') {
return \OC_Config::getValue($key, $default); return $this->config->getValue($key, $default);
} }
/** /**
@ -106,7 +113,7 @@ class SystemConfig {
* @param string $key the key of the value, under which it was saved * @param string $key the key of the value, under which it was saved
*/ */
public function deleteValue($key) { public function deleteValue($key) {
\OC_Config::deleteKey($key); $this->config->deleteKey($key);
} }
/** /**

@ -130,7 +130,7 @@ class OC_User {
*/ */
public static function setupBackends() { public static function setupBackends() {
OC_App::loadApps(array('prelogin')); OC_App::loadApps(array('prelogin'));
$backends = OC_Config::getValue('user_backends', array()); $backends = \OC::$server->getSystemConfig()->getValue('user_backends', array());
foreach ($backends as $i => $config) { foreach ($backends as $i => $config) {
$class = $config['class']; $class = $config['class'];
$arguments = $config['arguments']; $arguments = $config['arguments'];
@ -498,7 +498,7 @@ class OC_User {
if ($user) { if ($user) {
return $user->getHome(); return $user->getHome();
} else { } else {
return OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid; return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
} }
} }

@ -72,7 +72,7 @@ class OC_Util {
private static function initLocalStorageRootFS() { private static function initLocalStorageRootFS() {
// mount local file backend as root // mount local file backend as root
$configDataDirectory = OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data"); $configDataDirectory = \OC::$server->getSystemConfig()->getValue("datadirectory", OC::$SERVERROOT . "/data");
//first set up the local "root" storage //first set up the local "root" storage
\OC\Files\Filesystem::initMountManager(); \OC\Files\Filesystem::initMountManager();
if (!self::$rootMounted) { if (!self::$rootMounted) {
@ -184,7 +184,7 @@ class OC_Util {
OC_Hook::emit('OC_Filesystem', 'preSetup', array('user' => $user)); OC_Hook::emit('OC_Filesystem', 'preSetup', array('user' => $user));
//check if we are using an object storage //check if we are using an object storage
$objectStore = OC_Config::getValue('objectstore'); $objectStore = \OC::$server->getSystemConfig()->getValue('objectstore', null);
if (isset($objectStore)) { if (isset($objectStore)) {
self::initObjectStoreRootFS($objectStore); self::initObjectStoreRootFS($objectStore);
} else { } else {
@ -848,7 +848,7 @@ class OC_Util {
public static function checkDatabaseVersion() { public static function checkDatabaseVersion() {
$l = \OC::$server->getL10N('lib'); $l = \OC::$server->getL10N('lib');
$errors = array(); $errors = array();
$dbType = \OC_Config::getValue('dbtype', 'sqlite'); $dbType = \OC::$server->getSystemConfig()->getValue('dbtype', 'sqlite');
if ($dbType === 'pgsql') { if ($dbType === 'pgsql') {
// check PostgreSQL version // check PostgreSQL version
try { try {
@ -1108,11 +1108,11 @@ class OC_Util {
* @return string * @return string
*/ */
public static function getInstanceId() { public static function getInstanceId() {
$id = OC_Config::getValue('instanceid', null); $id = \OC::$server->getSystemConfig()->getValue('instanceid', null);
if (is_null($id)) { if (is_null($id)) {
// We need to guarantee at least one letter in instanceid so it can be used as the session_name // We need to guarantee at least one letter in instanceid so it can be used as the session_name
$id = 'oc' . \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); $id = 'oc' . \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
OC_Config::$object->setValue('instanceid', $id); \OC::$server->getSystemConfig()->setValue('instanceid', $id);
} }
return $id; return $id;
} }
@ -1364,7 +1364,7 @@ class OC_Util {
* @return string the theme * @return string the theme
*/ */
public static function getTheme() { public static function getTheme() {
$theme = OC_Config::getValue("theme", ''); $theme = \OC::$server->getSystemConfig()->getValue("theme", '');
if ($theme === '') { if ($theme === '') {
if (is_dir(OC::$SERVERROOT . '/themes/default')) { if (is_dir(OC::$SERVERROOT . '/themes/default')) {

@ -28,7 +28,9 @@ class TestAllConfig extends \Test\TestCase {
$connection = $this->connection; $connection = $this->connection;
} }
if($systemConfig === null) { if($systemConfig === null) {
$systemConfig = $this->getMock('\OC\SystemConfig'); $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
->disableOriginalConstructor()
->getMock();
} }
return new \OC\AllConfig($systemConfig, $connection); return new \OC\AllConfig($systemConfig, $connection);
} }
@ -89,7 +91,9 @@ class TestAllConfig extends \Test\TestCase {
public function testSetUserValueWithPreCondition() { public function testSetUserValueWithPreCondition() {
// mock the check for the database to run the correct SQL statements for each database type // mock the check for the database to run the correct SQL statements for each database type
$systemConfig = $this->getMock('\OC\SystemConfig'); $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
->disableOriginalConstructor()
->getMock();
$systemConfig->expects($this->once()) $systemConfig->expects($this->once())
->method('getValue') ->method('getValue')
->with($this->equalTo('dbtype'), ->with($this->equalTo('dbtype'),
@ -133,7 +137,9 @@ class TestAllConfig extends \Test\TestCase {
*/ */
public function testSetUserValueWithPreConditionFailure() { public function testSetUserValueWithPreConditionFailure() {
// mock the check for the database to run the correct SQL statements for each database type // mock the check for the database to run the correct SQL statements for each database type
$systemConfig = $this->getMock('\OC\SystemConfig'); $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
->disableOriginalConstructor()
->getMock();
$systemConfig->expects($this->once()) $systemConfig->expects($this->once())
->method('getValue') ->method('getValue')
->with($this->equalTo('dbtype'), ->with($this->equalTo('dbtype'),
@ -394,7 +400,9 @@ class TestAllConfig extends \Test\TestCase {
public function testGetUsersForUserValue() { public function testGetUsersForUserValue() {
// mock the check for the database to run the correct SQL statements for each database type // mock the check for the database to run the correct SQL statements for each database type
$systemConfig = $this->getMock('\OC\SystemConfig'); $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
->disableOriginalConstructor()
->getMock();
$systemConfig->expects($this->once()) $systemConfig->expects($this->once())
->method('getValue') ->method('getValue')
->with($this->equalTo('dbtype'), ->with($this->equalTo('dbtype'),

@ -38,7 +38,8 @@ class Server extends \Test\TestCase {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->server = new \OC\Server(''); $config = new \OC\Config(\OC::$configDir);
$this->server = new \OC\Server('', $config);
} }
public function dataTestQuery() { public function dataTestQuery() {