* introduce SystemConfig to avoid DI circle (used by database connection which is itself needed by AllConfig that itself contains the methods to access the config.php which then would need the database connection - did you get it? ;))
* use DI container and use that method in legacy code paths (for easier refactoring later)
* create and use getSystemConfig instead of query() in DI container
* Class to combine all the configuration options ownCloud offers
*/
class AllConfig implements \OCP\IConfig {
+ /** @var SystemConfig */
+ private $systemConfig;
+
+ /**
+ * @param SystemConfig $systemConfig
+ */
+ function __construct(SystemConfig $systemConfig) {
+ $this->systemConfig = $systemConfig;
+ }
+
/**
* Sets a new system wide value
*
* @param mixed $value the value that should be stored
*/
public function setSystemValue($key, $value) {
- \OCP\Config::setSystemValue($key, $value);
+ $this->systemConfig->setValue($key, $value);
}
/**
* @return mixed the value or $default
*/
public function getSystemValue($key, $default = '') {
- return \OCP\Config::getSystemValue($key, $default);
+ return $this->systemConfig->getValue($key, $default);
}
/**
* @param string $key the key of the value, under which it was saved
*/
public function deleteSystemValue($key) {
- \OCP\Config::deleteSystemValue($key);
+ $this->systemConfig->deleteValue($key);
}
/**
/**
* Create the connection parameters for the config
*
- * @param \OCP\IConfig $config
+ * @param \OC\SystemConfig $config
* @return array
*/
public function createConnectionParams($config) {
- $type = $config->getSystemValue('dbtype', 'sqlite');
+ $type = $config->getValue('dbtype', 'sqlite');
$connectionParams = array(
- 'user' => $config->getSystemValue('dbuser', ''),
- 'password' => $config->getSystemValue('dbpassword', ''),
+ 'user' => $config->getValue('dbuser', ''),
+ 'password' => $config->getValue('dbpassword', ''),
);
- $name = $config->getSystemValue('dbname', 'owncloud');
+ $name = $config->getValue('dbname', 'owncloud');
if ($this->normalizeType($type) === 'sqlite3') {
- $datadir = $config->getSystemValue("datadirectory", \OC::$SERVERROOT . '/data');
+ $datadir = $config->getValue("datadirectory", \OC::$SERVERROOT . '/data');
$connectionParams['path'] = $datadir . '/' . $name . '.db';
} else {
- $host = $config->getSystemValue('dbhost', '');
+ $host = $config->getValue('dbhost', '');
if (strpos($host, ':')) {
// Host variable may carry a port or socket.
list($host, $portOrSocket) = explode(':', $host, 2);
$connectionParams['dbname'] = $name;
}
- $connectionParams['tablePrefix'] = $config->getSystemValue('dbtableprefix', 'oc_');
- $connectionParams['sqlite.journal_mode'] = $config->getSystemValue('sqlite.journal_mode', 'WAL');
+ $connectionParams['tablePrefix'] = $config->getValue('dbtableprefix', 'oc_');
+ $connectionParams['sqlite.journal_mode'] = $config->getValue('sqlite.journal_mode', 'WAL');
//additional driver options, eg. for mysql ssl
- $driverOptions = $config->getSystemValue('dbdriveroptions', null);
+ $driverOptions = $config->getValue('dbdriveroptions', null);
if ($driverOptions) {
$connectionParams['driverOptions'] = $driverOptions;
}
$this->registerService('NavigationManager', function ($c) {
return new \OC\NavigationManager();
});
- $this->registerService('AllConfig', function ($c) {
- return new \OC\AllConfig();
+ $this->registerService('AllConfig', function (Server $c) {
+ return new \OC\AllConfig(
+ $c->getSystemConfig()
+ );
+ });
+ $this->registerService('SystemConfig', function ($c) {
+ return new \OC\SystemConfig();
});
$this->registerService('AppConfig', function ($c) {
return new \OC\AppConfig(\OC_DB::getConnection());
});
$this->registerService('DatabaseConnection', function (Server $c) {
$factory = new \OC\DB\ConnectionFactory();
- $type = $c->getConfig()->getSystemValue('dbtype', 'sqlite');
+ $systemConfig = $c->getSystemConfig();
+ $type = $systemConfig->getValue('dbtype', 'sqlite');
if (!$factory->isValidType($type)) {
throw new \OC\DatabaseException('Invalid database type');
}
- $connectionParams = $factory->createConnectionParams($c->getConfig());
+ $connectionParams = $factory->createConnectionParams($systemConfig);
$connection = $factory->getConnection($type, $connectionParams);
$connection->getConfiguration()->setSQLLogger($c->getQueryLogger());
return $connection;
return $this->query('AllConfig');
}
+ /**
+ * For internal use only
+ *
+ * @return \OC\SystemConfig
+ */
+ function getSystemConfig() {
+ return $this->query('SystemConfig');
+ }
+
/**
* Returns the app config manager
*
--- /dev/null
+<?php
+/**
+ * Copyright (c) 2014 Morris Jobke <hey@morrisjobke.de>
+ * 2013 Bart Visscher <bartv@thisnet.nl>
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ *
+ */
+
+namespace OC;
+
+/**
+ * Class which provides access to the system config values stored in config.php
+ * Internal class for bootstrap only.
+ * fixes cyclic DI: AllConfig needs AppConfig needs Database needs AllConfig
+ */
+class SystemConfig {
+ /**
+ * Sets a new system wide value
+ *
+ * @param string $key the key of the value, under which will be saved
+ * @param mixed $value the value that should be stored
+ */
+ public function setValue($key, $value) {
+ \OC_Config::setValue($key, $value);
+ }
+
+ /**
+ * Looks up a system wide defined value
+ *
+ * @param string $key the key of the value, under which it was saved
+ * @param mixed $default the default value to be returned if the value isn't set
+ * @return mixed the value or $default
+ */
+ public function getValue($key, $default = '') {
+ return \OC_Config::getValue($key, $default);
+ }
+
+ /**
+ * Delete a system wide defined value
+ *
+ * @param string $key the key of the value, under which it was saved
+ */
+ public function deleteValue($key) {
+ \OC_Config::deleteKey($key);
+ }
+}
/**
* This class provides functions to read and write configuration data.
* configuration can be on a system, application or user level
+ * @deprecated use methods of \OCP\IConfig
*/
class Config {
/**
* @param string $key key
* @param mixed $default = null default value
* @return mixed the value or $default
+ * @deprecated use method getSystemValue of \OCP\IConfig
*
* This function gets the value from config.php. If it does not exist,
* $default will be returned.
*/
public static function getSystemValue( $key, $default = null ) {
- return \OC_Config::getValue( $key, $default );
+ return \OC::$server->getConfig()->getSystemValue( $key, $default );
}
/**
* @param string $key key
* @param mixed $value value
* @return bool
+ * @deprecated use method setSystemValue of \OCP\IConfig
*
* This function sets the value and writes the config.php. If the file can
* not be written, false will be returned.
*/
public static function setSystemValue( $key, $value ) {
try {
- \OC_Config::setValue( $key, $value );
+ \OC::$server->getConfig()->setSystemValue( $key, $value );
} catch (\Exception $e) {
return false;
}
/**
* Deletes a value from config.php
* @param string $key key
+ * @deprecated use method deleteSystemValue of \OCP\IConfig
*
* This function deletes the value from config.php.
*/
public static function deleteSystemValue( $key ) {
- return \OC_Config::deleteKey( $key );
+ \OC::$server->getConfig()->deleteSystemValue( $key );
}
/**
->method('implementsActions')
->will($this->returnValue(false));
- $allConfig = new AllConfig();
+ $allConfig = $this->getMockBuilder('\OC\AllConfig')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $allConfig->expects($this->any())
+ ->method('getUserValue')
+ ->will($this->returnValue(true));
+ $allConfig->expects($this->any())
+ ->method('getSystemValue')
+ ->with($this->equalTo('datadirectory'))
+ ->will($this->returnValue('arbitrary/path'));
$user = new \OC\User\User('foo', $backend, null, $allConfig);
- $this->assertEquals(\OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data") . '/foo', $user->getHome());
+ $this->assertEquals('arbitrary/path/foo', $user->getHome());
}
public function testCanChangePassword() {