Start using objects for certain things that never change (project root, project, etc) rather than passing in arguments
Start using objects for certain things that never change (project root, project, etc) rather than passing in arguments

--- a/include/cache.cache_expire.php
+++ b/include/cache.cache_expire.php
@@ -9,26 +9,26 @@
 
 require_once('gitutil.git_read_refs.php');
 
-function cache_expire($projectroot, $project, $projectlist, $expireall = false)
+function cache_expire($expireall = false)
 {
-	global $tpl;
+	global $tpl, $gitphp_current_project;
 
 	if ($expireall) {
 		$tpl->clear_all_cache();
 		return;
 	}
 
-	if ((!isset($projectroot)) || (!isset($project)))
+	if (!$gitphp_current_project)
 		return;
 
-	$headlist = git_read_refs($projectroot, $project, "refs/heads");
+	$headlist = git_read_refs("refs/heads");
 
 	if (count($headlist) > 0) {
 		$age = $headlist[0]['age'];
 
 		$tpl->clear_cache(null, sha1($project), null, $age);
 
-		$tpl->clear_cache('projectlist.tpl', sha1(serialize($projectlist)), null, $age);
+		$tpl->clear_cache('projectlist.tpl', sha1(serialize(GitPHP_ProjectList::GetInstance()->GetConfig())), null, $age);
 	}
 }
 

--- a/include/display.git_heads.php
+++ b/include/display.git_heads.php
@@ -19,7 +19,7 @@
 	if (!$tpl->is_cached('heads.tpl', $cachekey)) {
 		$head = git_read_head($projectroot . $project);
 		$tpl->assign("head",$head);
-		$headlist = git_read_refs($projectroot, $project, "refs/heads");
+		$headlist = git_read_refs("refs/heads");
 		$tpl->assign("headlist",$headlist);
 	}
 	$tpl->display('heads.tpl', $cachekey);

--- a/include/display.git_opml.php
+++ b/include/display.git_opml.php
@@ -10,15 +10,15 @@
  require_once('util.script_url.php');
  require_once('gitutil.git_read_projects.php');
 
-function git_opml($projectroot,$projectlist)
+function git_opml()
 {
 	global $tpl,$gitphp_appstring;
 
-	$cachekey = sha1(serialize($projectlist));
+	$cachekey = sha1(serialize(GitPHP_ProjectList::GetInstance()->GetConfig()));
 
 	if (!$tpl->is_cached('opml.tpl', $cachekey)) {
 		header("Content-type: text/xml; charset=UTF-8");
-		$projlist = git_read_projects($projectroot,$projectlist);
+		$projlist = git_read_projects();
 		$tpl->assign("title", GitPHP_Config::GetInstance()->GetValue('title', $gitphp_appstring));
 		$tpl->assign("self",script_url());
 		$opmllist = array();

--- a/include/display.git_project_index.php
+++ b/include/display.git_project_index.php
@@ -9,19 +9,21 @@
 
  require_once('gitutil.git_read_projects.php');
 
-function git_project_index($projectroot, $projectlist)
+function git_project_index()
 {
 	global $tpl;
 
 	header("Content-type: text/plain; charset=utf-8");
 	header("Content-Disposition: inline; filename=\"index.aux\"");
 
+	$projectlist = GitPHP_ProjectList::GetInstance()->GetConfig();
+
 	$cachekey = sha1(serialize($projectlist));
 
 	if (!$tpl->is_cached('projectindex.tpl', $cachekey)) {
 		if (is_array($projectlist))
 			$tpl->assign("categorized", TRUE);
-		$projlist = git_read_projects($projectroot, $projectlist);
+		$projlist = git_read_projects();
 		$tpl->assign("projlist", $projlist);
 	}
 	$tpl->display('projectindex.tpl', $cachekey);

--- a/include/display.git_project_list.php
+++ b/include/display.git_project_list.php
@@ -13,14 +13,16 @@
  require_once('util.agecmp.php');
  require_once('gitutil.git_read_projects.php');
 
-function git_project_list($projectroot,$projectlist,$order = "project")
+function git_project_list($order = "project")
 {
 	global $tpl;
+
+	$projectlist = GitPHP_ProjectList::GetInstance()->GetConfig();
 
 	$cachekey = sha1(serialize($projectlist)) . "|" . sha1($order);
 
 	if (!$tpl->is_cached('projectlist.tpl', $cachekey)) {
-		$projects = git_read_projects($projectroot,$projectlist, TRUE);
+		$projects = git_read_projects(true);
 		if (is_array($projects)) {
 			if (count($projects) > 0) {
 				if ($order)

--- a/include/display.git_summary.php
+++ b/include/display.git_summary.php
@@ -15,20 +15,25 @@
  require_once(GITPHP_INCLUDEDIR . 'gitutil.read_info_ref.php');
  require_once(GITPHP_INCLUDEDIR . 'git/Project.class.php');
 
-function git_summary($projectroot,$project)
+function git_summary()
 {
-	global $tpl;
+	global $tpl, $gitphp_current_project;
+
+	if (!$gitphp_current_project)
+		return;
+
+	$project = $gitphp_current_project->GetProject();
 
 	$cachekey = sha1($project);
 
 	if (!$tpl->is_cached('project.tpl', $cachekey)) {
-		$projectObj = new GitPHP_Project($project);
+		$projectroot = GitPHP_Config::GetInstance()->GetValue('projectroot');
 
-		$descr = $projectObj->GetDescription();
+		$descr = $gitphp_current_project->GetDescription();
 		$head = git_read_head($projectroot . $project);
 		$commit = git_read_commit($projectroot . $project, $head);
 		$commitdate = date_str($commit['committer_epoch'],$commit['committer_tz']);
-		$owner = $projectObj->GetOwner();
+		$owner = $gitphp_current_project->GetOwner();
 		$refs = read_info_ref($projectroot . $project);
 		$tpl->assign("head",$head);
 		$tpl->assign("description",$descr);
@@ -57,7 +62,7 @@
 		}
 		$tpl->assign("revlist",$revlist);
 
-		$taglist = git_read_refs($projectroot,$project,"refs/tags");
+		$taglist = git_read_refs("refs/tags");
 		if (isset($taglist) && (count($taglist) > 0)) {
 			foreach ($taglist as $i => $tag) {
 				if (isset($tag['comment'])) {
@@ -70,7 +75,7 @@
 			$tpl->assign("taglist",$taglist);
 		}
 
-		$headlist = git_read_refs($projectroot,$project,"refs/heads");
+		$headlist = git_read_refs("refs/heads");
 		if (isset($headlist) && (count($headlist) > 0)) {
 			$tpl->assign("headlist",$headlist);
 		}

--- a/include/display.git_tags.php
+++ b/include/display.git_tags.php
@@ -19,7 +19,7 @@
 	if (!$tpl->is_cached('tags.tpl', $cachekey)) {
 		$head = git_read_head($projectroot . $project);
 		$tpl->assign("head",$head);
-		$taglist = git_read_refs($projectroot, $project, "refs/tags");
+		$taglist = git_read_refs("refs/tags");
 		if (isset($taglist) && (count($taglist) > 0)) {
 			$tpl->assign("taglist",$taglist);
 		}

--- a/include/gitutil.git_read_packed_refs.php
+++ b/include/gitutil.git_read_packed_refs.php
@@ -9,8 +9,16 @@
 
  require_once('gitutil.git_read_hash.php');
 
-function git_read_packed_refs($projectroot, $project, $refdir)
+function git_read_packed_refs($refdir)
 {
+	global $gitphp_current_project;
+
+	if (!$gitphp_current_project)
+		return null;
+
+	$projectroot = GitPHP_Config::GetInstance()->GetValue('projectroot');
+	$project = $gitphp_current_project->GetProject();
+
 	if (!is_file($projectroot . $project . '/packed-refs'))
 		return null;
 
@@ -24,7 +32,7 @@
 		if (preg_match('/^([0-9a-f]{40}) (.+)$/i', trim($i), $regs)) {
 			if (strncmp($refdir, $regs[2], $dirlen) === 0) {
 				$regs[2] = substr($regs[2], $dirlen+1);
-				$refobj = git_read_ref($projectroot, $project, $regs[1], $regs[2]);
+				$refobj = git_read_ref($regs[1], $regs[2]);
 				if (isset($refobj))
 					$reflist[] = $refobj;
 			}

--- a/include/gitutil.git_read_projects.php
+++ b/include/gitutil.git_read_projects.php
@@ -10,10 +10,10 @@
  require_once('gitutil.git_recurse_projects.php');
  require_once('gitutil.git_project_info.php');
 
-function git_read_projects($projectroot,$projectlist,$projdata = FALSE)
+function git_read_projects($projdata = FALSE)
 {
-	if (!isset($projectroot))
-		return "No projectroot set";
+	$projectroot = GitPHP_Config::GetInstance()->GetValue('projectroot');
+	$projectlist = GitPHP_ProjectList::GetInstance()->GetConfig();
 
 	if (!is_dir($projectroot))
 		return "Projectroot is not a directory";

--- a/include/gitutil.git_read_ref.php
+++ b/include/gitutil.git_read_ref.php
@@ -12,8 +12,16 @@
  require_once('gitutil.git_read_tag.php');
  require_once('gitutil.git_read_commit.php');
 
- function git_read_ref($projectroot, $project, $ref_id, $ref_file)
+ function git_read_ref($ref_id, $ref_file)
  {
+ 	global $gitphp_current_project;
+
+	if (!$gitphp_current_project)
+		return null;
+
+	$projectroot = GitPHP_Config::GetInstance()->GetValue('projectroot');
+	$project = $gitphp_current_project->GetProject();
+
 	$type = git_get_type($projectroot . $project, $ref_id);
 
 	if (!$type)

--- a/include/gitutil.git_read_refs.php
+++ b/include/gitutil.git_read_refs.php
@@ -12,8 +12,16 @@
  require_once('gitutil.git_read_ref.php');
  require_once('gitutil.git_read_packed_refs.php');
 
-function git_read_refs($projectroot,$project,$refdir)
+function git_read_refs($refdir)
 {
+	global $gitphp_current_project;
+
+	if (!$gitphp_current_project)
+		return null;
+
+	$projectroot = GitPHP_Config::GetInstance()->GetValue('projectroot');
+	$project = $gitphp_current_project->GetProject();
+
 	if (!is_dir($projectroot . $project . "/" . $refdir))
 		return null;
 	$refs = array();
@@ -38,12 +46,12 @@
 	$reflist = array();
 	foreach ($refs as $i => $ref_file) {
 		$ref_id = git_read_hash($projectroot . $project . "/" . $refdir . "/" . $ref_file);
-		$refobj = git_read_ref($projectroot, $project, $ref_id, $ref_file);
+		$refobj = git_read_ref($ref_id, $ref_file);
 		if (isset($refobj))
 			$reflist[] = $refobj;
 	}
 
-	$packedrefs = git_read_packed_refs($projectroot, $project, $refdir);
+	$packedrefs = git_read_packed_refs($refdir);
 	if (isset($packedrefs) && count($packedrefs) > 0) {
 		foreach ($packedrefs as $packedref) {
 			$found = false;

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -40,11 +40,19 @@
  require_once(GITPHP_INCLUDEDIR . 'git/ProjectList.class.php');
  GitPHP_ProjectList::Instantiate(GITPHP_CONFIGDIR . 'gitphp.conf.php');
 
+ /**
+  * Check for projectroot
+  */
+if (!GitPHP_Config::GetInstance()->GetValue('projectroot', null)) {
+	throw new Exception ('A projectroot must be set in the config.');
+}
+
  $project = null;
+ $gitphp_current_project = null;
 
  if (isset($_GET['p'])) {
- 	if (GitPHP_ProjectList::GetInstance()->HasProject($_GET['p']))
-		$project = str_replace(chr(0), '', $_GET['p']);
+ 	$gitphp_current_project = GitPHP_ProjectList::GetInstance()->GetProject(str_replace(chr(0), '', $_GET['p']));
+	$project = $gitphp_current_project->GetProject();
  }
 
  $extraoutput = FALSE;
@@ -80,7 +88,7 @@
 		$tpl->cache_lifetime = GitPHP_Config::GetInstance()->GetValue('cachelifetime');
 	if (GitPHP_Config::GetInstance()->GetValue('cacheexpire', true) === false) {
 		require_once(GITPHP_INCLUDEDIR . 'cache.cache_expire.php');
-		cache_expire(GitPHP_Config::GetInstance()->GetValue('projectroot'), $project, GitPHP_ProjectList::GetInstance()->GetConfig());
+		cache_expire();
 	}
  }
 
@@ -119,14 +127,14 @@
  if (isset($_GET['a']) && $_GET['a'] == "expire") {
  	require_once(GITPHP_INCLUDEDIR . 'cache.cache_expire.php');
 	require_once(GITPHP_INCLUDEDIR . 'display.git_message.php');
-	cache_expire(null, null, null, true);
+	cache_expire(true);
 	git_message("Cache expired");
  } else if (isset($_GET['a']) && $_GET['a'] == "opml") {
 	require_once(GITPHP_INCLUDEDIR . 'display.git_opml.php');
 	git_opml(GitPHP_Config::GetInstance()->GetValue('projectroot'), GitPHP_ProjectList::GetInstance()->GetConfig());
  } else if (isset($_GET['a']) && $_GET['a'] == "project_index") {
 	require_once(GITPHP_INCLUDEDIR . 'display.git_project_index.php');
-	git_project_index(GitPHP_Config::GetInstance()->GetValue('projectroot'), GitPHP_ProjectList::GetInstance()->GetConfig());
+	git_project_index();
  } else if ($project) {
  	if (!is_dir(GitPHP_Config::GetInstance()->GetValue('projectroot') . $project)) {
 		$tpl->assign("validproject",FALSE);
@@ -139,12 +147,12 @@
 	} else {
 		if (!isset($_GET['a'])) {
 			require_once(GITPHP_INCLUDEDIR . 'display.git_summary.php');
-			git_summary(GitPHP_Config::GetInstance()->GetValue('projectroot'),$project);
+			git_summary();
 		} else {
 			switch ($_GET['a']) {
 				case "summary":
 					require_once(GITPHP_INCLUDEDIR . 'display.git_summary.php');
-					git_summary(GitPHP_Config::GetInstance()->GetValue('projectroot'),$project);
+					git_summary();
 					break;
 				case "tree":
 					require_once(GITPHP_INCLUDEDIR . 'display.git_tree.php');
@@ -233,7 +241,7 @@
 	}
  } else {
 	require_once(GITPHP_INCLUDEDIR . 'display.git_project_list.php');
-	git_project_list(GitPHP_Config::GetInstance()->GetValue('projectroot'), GitPHP_ProjectList::GetInstance()->GetConfig(), (isset($_GET['o']) ? $_GET['o'] : "project"));
+	git_project_list((isset($_GET['o']) ? $_GET['o'] : "project"));
  }
 
  if (GitPHP_Config::GetInstance()->GetValue('debug', false) && $extraoutput)

comments