Move config defaults to a central location
Move config defaults to a central location

--- a/include/Config.class.php
+++ b/include/Config.class.php
@@ -44,6 +44,13 @@
 	protected $values = array();
 
 	/**
+	 * Stores the default values
+	 *
+	 * @var array
+	 */
+	protected $defaults = array();
+
+	/**
 	 * Stores the config files
 	 *
 	 * @var string[]
@@ -76,6 +83,7 @@
 	 */
 	private function __construct()
 	{
+		$this->InitializeDefaults();
 	}
 
 	/**
@@ -117,15 +125,15 @@
 	 * Gets a config value
 	 *
 	 * @param string $key config key to fetch
-	 * @param mixed $default default config value to return
 	 * @return mixed config value
 	 */
-	public function GetValue($key, $default = null)
+	public function GetValue($key)
 	{
 		if ($this->HasKey($key)) {
 			return $this->values[$key];
 		}
-		return $default;
+
+		return null;
 	}
 
 	/**
@@ -160,5 +168,29 @@
 		return isset($this->values[$key]);
 	}
 
+	/**
+	 * Initializes default config values
+	 */
+	private function InitializeDefaults()
+	{
+		$this->values['objectmemory'] = 0;
+		$this->values['objectcache'] = false;
+		$this->values['objectcachelifetime'] = 86400;
+		$this->values['cache'] = false;
+		$this->values['debug'] = false;
+		$this->values['benchmark'] = false;
+		$this->values['stylesheet'] = 'gitphpskin.css';
+		$this->values['javascript'] = true;
+		$this->values['googlejs'] = false;
+		$this->values['search'] = true;
+		$this->values['filesearch'] = true;
+		$this->values['cacheexpire'] = true;
+		$this->values['largeskip'] = 200;
+		$this->values['filemimetype'] = true;
+		$this->values['geshi'] = true;
+		$this->values['exportedonly'] = false;
+		$this->values['compressformat'] = GITPHP_COMPRESS_ZIP;
+	}
+
 }
 

--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -163,13 +163,13 @@
 		$this->tpl->merge_compiled_includes = true;
 		$this->tpl->addPluginsDir(GITPHP_INCLUDEDIR . 'smartyplugins');
 
-		if ($this->config->GetValue('cache', false)) {
+		if ($this->config->GetValue('cache')) {
 			$this->tpl->caching = Smarty::CACHING_LIFETIME_SAVED;
 			if ($this->config->HasKey('cachelifetime')) {
 				$this->tpl->cache_lifetime = $this->config->GetValue('cachelifetime');
 			}
 
-			$servers = $this->config->GetValue('memcache', null);
+			$servers = $this->config->GetValue('memcache');
 			if (isset($servers) && is_array($servers) && (count($servers) > 0)) {
 				$this->tpl->registerCacheResource('memcache', new GitPHP_CacheResource_Memcache($servers));
 				$this->tpl->caching_type = 'memcache';
@@ -197,9 +197,9 @@
 		if ($this->log)
 			return;
 
-		$debug = $this->config->GetValue('debug', false);
+		$debug = $this->config->GetValue('debug');
 		if ($debug) {
-			$this->log = new GitPHP_DebugLog($debug, $this->config->GetValue('benchmark', false));
+			$this->log = new GitPHP_DebugLog($debug, $this->config->GetValue('benchmark'));
 			$this->log->SetStartTime(GITPHP_START_TIME);
 			$this->log->SetStartMemory(GITPHP_START_MEM);
 			if ($this->exe)
@@ -343,31 +343,39 @@
 
 		$this->tpl->assign('version', $gitphp_version);
 
-		$stylesheet = $this->config->GetValue('stylesheet', 'gitphpskin.css');
+		$stylesheet = $this->config->GetValue('stylesheet');
 		if ($stylesheet == 'gitphp.css') {
 			// backwards compatibility
 			$stylesheet = 'gitphpskin.css';
 		}
 		$this->tpl->assign('stylesheet', preg_replace('/\.css$/', '', $stylesheet));
 
-		$this->tpl->assign('javascript', $this->config->GetValue('javascript', true));
-		$this->tpl->assign('googlejs', $this->config->GetValue('googlejs', false));
-		$this->tpl->assign('pagetitle', $this->config->GetValue('title', $gitphp_appstring));
-		$this->tpl->assign('homelink', $this->config->GetValue('homelink', __('projects')));
+		$this->tpl->assign('javascript', $this->config->GetValue('javascript'));
+		$this->tpl->assign('googlejs', $this->config->GetValue('googlejs'));
+		if ($this->config->HasKey('title')) {
+			$this->tpl->assign('pagetitle', $this->config->GetValue('title'));
+		} else {
+			$this->tpl->assign('pagetitle', $gitphp_appstring);
+		}
+		if ($this->config->HasKey('homelink')) {
+			$this->tpl->assign('homelink', $this->config->GetValue('homelink'));
+		} else {
+			$this->tpl->assign('homelink', __('projects'));
+		}
 		$this->tpl->assign('action', $this->GetName());
 		$this->tpl->assign('actionlocal', $this->GetName(true));
 		if ($this->project)
 			$this->tpl->assign('project', $this->GetProject());
-		if ($this->config->GetValue('search', true))
+		if ($this->config->GetValue('search'))
 			$this->tpl->assign('enablesearch', true);
-		if ($this->config->GetValue('filesearch', true))
+		if ($this->config->GetValue('filesearch'))
 			$this->tpl->assign('filesearch', true);
 		if (isset($this->params['search']))
 			$this->tpl->assign('search', $this->params['search']);
 		if (isset($this->params['searchtype']))
 			$this->tpl->assign('searchtype', $this->params['searchtype']);
 		$this->tpl->assign('currentlocale', GitPHP_Resource::GetLocale());
-		$this->tpl->assign('supportedlocales', GitPHP_Resource::SupportedLocales($this->config->GetValue('debug', false)));
+		$this->tpl->assign('supportedlocales', GitPHP_Resource::SupportedLocales($this->config->GetValue('debug')));
 
 		$scripturl = $_SERVER['SCRIPT_NAME'];
 		$fullscripturl = '';
@@ -425,7 +433,7 @@
 	 */
 	public function Render()
 	{
-		if (($this->config->GetValue('cache', false) == true) && ($this->config->GetValue('cacheexpire', true) === true))
+		if (($this->config->GetValue('cache') == true) && ($this->config->GetValue('cacheexpire') === true))
 			$this->CacheExpire();
 
 		if (!$this->tpl->isCached($this->GetTemplate(), $this->GetFullCacheKey())) {

--- a/include/controller/Controller_Blame.class.php
+++ b/include/controller/Controller_Blame.class.php
@@ -98,7 +98,7 @@
 
 		$this->tpl->assign('tree', $commit->GetTree());
 
-		if ($this->config->GetValue('geshi', true)) {
+		if ($this->config->GetValue('geshi')) {
 			include_once(GITPHP_GESHIDIR . "geshi.php");
 			if (class_exists('GeSHi')) {
 				$geshi = new GeSHi("",'php');

--- a/include/controller/Controller_Blob.class.php
+++ b/include/controller/Controller_Blob.class.php
@@ -82,7 +82,7 @@
 				$headers = array();
 
 				$mime = null;
-				if ($this->config->GetValue('filemimetype', true)) {
+				if ($this->config->GetValue('filemimetype')) {
 					if ((!isset($this->params['hash'])) && (isset($this->params['file']))) {
 						$commit = $this->GetProject()->GetCommit($this->params['hashbase']);
 						$this->params['hash'] = $commit->GetTree()->PathToHash($this->params['file']);
@@ -139,7 +139,7 @@
 		$head = $this->GetProject()->GetHeadCommit();
 		$this->tpl->assign('head', $head);
 
-		if ($this->config->GetValue('filemimetype', true)) {
+		if ($this->config->GetValue('filemimetype')) {
 			$mimeReader = new GitPHP_FileMimeTypeReader($blob);
 			$mimetype = $mimeReader->GetMimeType(true);
 			if ($mimetype == 'image') {
@@ -150,7 +150,7 @@
 			}
 		}
 
-		if ($this->config->GetValue('geshi', true)) {
+		if ($this->config->GetValue('geshi')) {
 			include_once(GITPHP_GESHIDIR . "geshi.php");
 			if (class_exists('GeSHi')) {
 				$geshi = new GeSHi("",'php');

--- a/include/controller/Controller_Log.class.php
+++ b/include/controller/Controller_Log.class.php
@@ -83,7 +83,7 @@
 		$compat = $this->GetProject()->GetCompat();
 		$skip = $this->params['page'] * 100;
 		$strategy = null;
-		if ($compat || ($skip > $this->config->GetValue('largeskip', 200))) {
+		if ($compat || ($skip > $this->config->GetValue('largeskip'))) {
 			$strategy = new GitPHP_LogLoad_Git($this->exe);
 		} else {
 			$strategy = new GitPHP_LogLoad_Raw();

--- a/include/controller/Controller_Search.class.php
+++ b/include/controller/Controller_Search.class.php
@@ -45,7 +45,7 @@
 	{
 		parent::__construct();
 
-		if (!$this->config->GetValue('search', true)) {
+		if (!$this->config->GetValue('search')) {
 			throw new GitPHP_MessageException(__('Search has been disabled'), true);
 		}
 	}
@@ -96,7 +96,7 @@
 			$this->params['searchtype'] = GitPHP_Controller_Search::CommitSearch;
 
 		if ($this->params['searchtype'] == GitPHP_Controller_Search::FileSearch) {
-			if (!$this->config->GetValue('filesearch', true)) {
+			if (!$this->config->GetValue('filesearch')) {
 				throw new GitPHP_MessageException(__('File search has been disabled'), true);
 			}
 

--- a/include/controller/Controller_Snapshot.class.php
+++ b/include/controller/Controller_Snapshot.class.php
@@ -89,7 +89,7 @@
 		if (isset($_GET['fmt']))
 			$this->params['format'] = $_GET['fmt'];
 		else
-			$this->params['format'] = $this->config->GetValue('compressformat', GITPHP_COMPRESS_ZIP);
+			$this->params['format'] = $this->config->GetValue('compressformat');
 	}
 
 	/**
@@ -146,7 +146,7 @@
 	{
 		$this->LoadData();
 
-		$cache = $this->config->GetValue('cache', false);
+		$cache = $this->config->GetValue('cache');
 		$cachehandle = false;
 		$cachefile = '';
 		if ($cache && is_dir(GITPHP_CACHEDIR)) {

--- a/include/git/filemimetype/FileMimeType_Fileinfo.class.php
+++ b/include/git/filemimetype/FileMimeType_Fileinfo.class.php
@@ -25,7 +25,7 @@
 			return false;
 
 		$mime = '';
-		$finfo = @finfo_open(FILEINFO_MIME, GitPHP_Config::GetInstance()->GetValue('magicdb', null));
+		$finfo = @finfo_open(FILEINFO_MIME, GitPHP_Config::GetInstance()->GetValue('magicdb'));
 		if ($finfo) {
 			$mime = finfo_buffer($finfo, $data, FILEINFO_MIME);
 			if ($mime && strpos($mime, '/')) {

--- a/include/git/projectlist/ProjectList.class.php
+++ b/include/git/projectlist/ProjectList.class.php
@@ -43,7 +43,7 @@
 
 		if (!$instance) {
 
-			$instance = new GitPHP_ProjectListDirectory($projectRoot, GitPHP_Config::GetInstance()->GetValue('exportedonly', false));
+			$instance = new GitPHP_ProjectListDirectory($projectRoot, GitPHP_Config::GetInstance()->GetValue('exportedonly'));
 		}
 
 		if (isset($git_projects_settings) && !$legacy)

--- a/include/git/projectlist/ProjectListBase.class.php
+++ b/include/git/projectlist/ProjectListBase.class.php
@@ -116,13 +116,13 @@
 			throw new GitPHP_MessageException(sprintf(__('%1$s is not a directory'), $this->projectRoot), true, 500);
 		}
 
-		$this->memoryCache = new GitPHP_MemoryCache(GitPHP_Config::GetInstance()->GetValue('objectmemory', 0));
-
-		if (GitPHP_Config::GetInstance()->GetValue('objectcache', false)) {
+		$this->memoryCache = new GitPHP_MemoryCache(GitPHP_Config::GetInstance()->GetValue('objectmemory'));
+
+		if (GitPHP_Config::GetInstance()->GetValue('objectcache')) {
 			$this->cache = new GitPHP_Cache();
-			$this->cache->SetServers(GitPHP_Config::GetInstance()->GetValue('memcache', null));
+			$this->cache->SetServers(GitPHP_Config::GetInstance()->GetValue('memcache'));
 			$this->cache->SetEnabled(true);
-			$this->cache->SetLifetime(GitPHP_Config::GetInstance()->GetValue('objectcachelifetime', 86400));
+			$this->cache->SetLifetime(GitPHP_Config::GetInstance()->GetValue('objectcachelifetime'));
 		}
 	}
 

comments