Use load strategies for project
[gitphp.git] / include / git / projectlist / ProjectListBase.class.php
blob:a/include/git/projectlist/ProjectListBase.class.php -> blob:b/include/git/projectlist/ProjectListBase.class.php
--- a/include/git/projectlist/ProjectListBase.class.php
+++ b/include/git/projectlist/ProjectListBase.class.php
@@ -12,28 +12,28 @@
 	/**
 	 * Project name sort
 	 *
-	 * @const
+	 * @var string
 	 */
 	const ProjectSort = 'project';
 
 	/**
 	 * Project description sort
 	 *
-	 * @const
+	 * @var string
 	 */
 	const DescriptionSort = 'descr';
 
 	/**
 	 * Project owner sort
 	 *
-	 * @const
+	 * @var string
 	 */
 	const OwnerSort = 'owner';
 
 	/**
 	 * Project age sort
 	 *
-	 * @const
+	 * @var string
 	 */
 	const AgeSort = 'age';
 
@@ -52,7 +52,7 @@
 	protected $projectsLoaded = false;
 
 	/**
-	 * The project configuration
+	 * The projectlist configuration
 	 *
 	 * @var string
 	 */
@@ -85,6 +85,20 @@
 	 * @var GitPHP_MemoryCache
 	 */
 	protected $memoryCache = null;
+
+	/**
+	 * Executable for all projects
+	 *
+	 * @var GitPHP_GitExe
+	 */
+	protected $exe = null;
+
+	/**
+	 * Config provider
+	 *
+	 * @var GitPHP_Config
+	 */
+	protected $config = null;
 
 	/**
 	 * Observers
@@ -103,26 +117,36 @@
 		$this->projects = array();
 		$this->projectRoot = GitPHP_Util::AddSlash($projectRoot);
 		if (empty($this->projectRoot)) {
-			throw new GitPHP_MessageException(__('A projectroot must be set in the config'), true, 500);
+			throw new GitPHP_MissingProjectrootException();
 		}
 		if (!is_dir($this->projectRoot)) {
-			throw new GitPHP_MessageException(sprintf(__('%1$s is not a directory'), $this->projectRoot));
-		}
-
-		$this->memoryCache = new GitPHP_MemoryCache(GitPHP_Config::GetInstance()->GetValue('objectmemory', 0));
-
-		if (GitPHP_Config::GetInstance()->GetValue('objectcache', false)) {
-			$this->cache = new GitPHP_Cache();
-			$this->cache->SetServers(GitPHP_Config::GetInstance()->GetValue('memcache', null));
-			$this->cache->SetEnabled(true);
-			$this->cache->SetLifetime(GitPHP_Config::GetInstance()->GetValue('objectcachelifetime', 86400));
-		}
+			throw new GitPHP_InvalidDirectoryConfigurationException($this->projectRoot);
+		}
+	}
+
+	/**
+	 * Get config provider
+	 *
+	 * @return GitPHP_Config
+	 */
+	public function GetConfig()
+	{
+		return $this->config;
+	}
+
+	/**
+	 * Set config provider
+	 *
+	 * @param GitPHP_Config $config config provider
+	 */
+	public function SetConfig($config)
+	{
+		$this->config = $config;
 	}
 
 	/**
 	 * Get memory cache instance
 	 *
-	 * @access public
 	 * @return GitPHP_MemoryCache|null
 	 */
 	public function GetMemoryCache()
@@ -133,7 +157,6 @@
 	/**
 	 * Set memory cache instance
 	 *
-	 * @access public
 	 * @param GitPHP_MemoryCache|null $memoryCache memory cache instance
 	 */
 	public function SetMemoryCache($memoryCache)
@@ -144,7 +167,6 @@
 	/**
 	 * Get object cache instance
 	 *
-	 * @access public
 	 * @return GitPHP_Cache|null object cache
 	 */
 	public function GetCache()
@@ -155,12 +177,31 @@
 	/**
 	 * Set object cache instance
 	 *
-	 * @access public
 	 * @param GitPHP_Cache|null $cache object cache instance
 	 */
 	public function SetCache($cache)
 	{
 		$this->cache = $cache;
+	}
+
+	/**
+	 * Get executable
+	 *
+	 * @return GitPHP_GitExe executable
+	 */
+	public function GetExe()
+	{
+		return $this->exe;
+	}
+
+	/**
+	 * Set executable
+	 *
+	 * @param GitPHP_GitExe $exe executable
+	 */
+	public function SetExe($exe)
+	{
+		$this->exe = $exe;
 	}
 
 	/**
@@ -235,15 +276,17 @@
 
 		$compat = $project->GetCompat();
 
+		$loader = null;
 		if ($compat) {
-			$project->SetStrategy(new GitPHP_ProjectLoad_Git(GitPHP_GitExe::GetInstance()));
+			$project->SetStrategy(new GitPHP_ProjectLoad_Git($this->exe));
 		} else {
-			$project->SetStrategy(new GitPHP_ProjectLoad_Raw());
+			$loader = new GitPHP_GitObjectLoader($project);
+			$project->SetStrategy(new GitPHP_ProjectLoad_Raw($loader));
 		}
 
 		$headListStrategy = null;
 		if ($compat) {
-			$headListStrategy = new GitPHP_HeadListLoad_Git(GitPHP_GitExe::GetInstance());
+			$headListStrategy = new GitPHP_HeadListLoad_Git($this->exe);
 		} else {
 			$headListStrategy = new GitPHP_HeadListLoad_Raw();
 		}
@@ -252,20 +295,19 @@
 
 		$tagListStrategy = null;
 		if ($compat) {
-			$tagListStrategy = new GitPHP_TagListLoad_Git(GitPHP_GitExe::GetInstance());
+			$tagListStrategy = new GitPHP_TagListLoad_Git($this->exe);
 		} else {
 			$tagListStrategy = new GitPHP_TagListLoad_Raw();
 		}
 		$tagList = new GitPHP_TagList($project, $tagListStrategy);
 		$project->SetTagList($tagList);
 
-		if (!$compat) {
-			$loader = new GitPHP_GitObjectLoader($project);
-			$project->SetObjectLoader($loader);
-		}
-
 		$manager = new GitPHP_GitObjectManager($project);
 		$manager->SetCompat($compat);
+		if (!$compat) {
+			$manager->SetObjectLoader($loader);
+		}
+		$manager->SetExe($this->exe);
 		if ($this->memoryCache) {
 			$manager->SetMemoryCache($this->memoryCache);
 		}
@@ -280,7 +322,7 @@
 	 *
 	 * @return mixed project config
 	 */
-	public function GetConfig()
+	public function GetProjectListConfig()
 	{
 		return $this->projectConfig;
 	}
@@ -290,7 +332,7 @@
 	 *
 	 * @return array
 	 */
-	public function GetSettings()
+	public function GetProjectSettings()
 	{
 		return $this->projectSettings;
 	}
@@ -320,6 +362,8 @@
 
 		if ($config->HasValue('gitphp.description')) {
 			$project->SetDescription($config->GetValue('gitphp.description'));
+		} else if ($config->HasValue('gitweb.description')) {
+			$project->SetDescription($config->GetValue('gitweb.description'));
 		}
 
 		if ($config->HasValue('gitphp.category')) {
@@ -354,6 +398,10 @@
 			$project->SetAbbreviateLength($config->GetValue('core.abbrev'));
 		}
 
+		if ($config->HasValue('gitphp.allowedusers')) {
+			$project->SetAllowedUsers($config->GetValue('gitphp.allowedusers', true));
+		}
+
 	}
 
 	/**
@@ -366,30 +414,35 @@
 		if (!$project)
 			return;
 
-		$config = GitPHP_Config::GetInstance();
-
-		if ($config->HasKey('cloneurl')) {
-			$project->SetCloneUrl(GitPHP_Util::AddSlash($config->GetValue('cloneurl'), false) . $project->GetProject());
-		}
-
-		if ($config->HasKey('pushurl')) {
-			$project->SetPushUrl(GitPHP_Util::AddSlash($config->GetValue('pushurl'), false) . $project->GetProject());
-		}
-
-		if ($config->HasKey('bugpattern')) {
-			$project->SetBugPattern($config->GetValue('bugpattern'));
-		}
-
-		if ($config->HasKey('bugurl')) {
-			$project->SetBugUrl($config->GetValue('bugurl'));
-		}
-
-		if ($config->HasKey('compat')) {
-			$project->SetCompat($config->GetValue('compat'));
-		}
-
-		if ($config->HasKey('uniqueabbrev')) {
-			$project->SetUniqueAbbreviation($config->GetValue('uniqueabbrev'));
+		if (!$this->config)
+			return;
+
+		if ($this->config->GetValue('cloneurl')) {
+			$project->SetCloneUrl(GitPHP_Util::AddSlash($this->config->GetValue('cloneurl'), false) . $project->GetProject());
+		}
+
+		if ($this->config->GetValue('pushurl')) {
+			$project->SetPushUrl(GitPHP_Util::AddSlash($this->config->GetValue('pushurl'), false) . $project->GetProject());
+		}
+
+		if ($this->config->GetValue('bugpattern')) {
+			$project->SetBugPattern($this->config->GetValue('bugpattern'));
+		}
+
+		if ($this->config->GetValue('bugurl')) {
+			$project->SetBugUrl($this->config->GetValue('bugurl'));
+		}
+
+		if ($this->config->HasKey('compat')) {
+			$project->SetCompat($this->config->GetValue('compat'));
+		}
+
+		if ($this->config->HasKey('uniqueabbrev')) {
+			$project->SetUniqueAbbreviation($this->config->GetValue('uniqueabbrev'));
+		}
+
+		if ($this->config->GetValue('abbreviateurl')) {
+			$project->SetUniqueAbbreviation(true);
 		}
 	}
 
@@ -405,6 +458,19 @@
 		$this->Sort();
 
 		$this->ApplySettings();
+	}
+
+	/**
+	 * Filter projects by user access
+	 *
+	 * @param string $username username
+	 */
+	public function FilterByUser($username)
+	{
+		foreach ($this->projects as $path => $project) {
+			if (!$project->UserCanAccess($username))
+				unset($this->projects[$path]);
+		}
 	}
 
 	/**
@@ -558,6 +624,9 @@
 		if (isset($projData['website']) && is_string($projData['website'])) {
 			$project->SetWebsite($projData['website']);
 		}
+		if (!empty($projData['allowedusers'])) {
+			$project->SetAllowedUsers($projData['allowedusers']);
+		}
 	}
 
 	/**
@@ -565,7 +634,7 @@
 	 *
 	 * @param array $settings the array of settings
 	 */
-	public function SetSettings($settings)
+	public function SetProjectSettings($settings)
 	{
 		if ((!$settings) || (count($settings) < 1))
 			return;

comments