Remove object cache singleton
[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
@@ -7,33 +7,33 @@
  * @package GitPHP
  * @subpackage Git\ProjectList
  */
-abstract class GitPHP_ProjectListBase implements Iterator
+abstract class GitPHP_ProjectListBase implements Iterator, GitPHP_Observable_Interface
 {
 	/**
 	 * 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
 	 */
@@ -78,6 +78,34 @@
 	 * @var GitPHP_Cache
 	 */
 	protected $cache = null;
+
+	/**
+	 * Memory cache instance for all projects
+	 *
+	 * @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
+	 *
+	 * @var GitPHP_Observer_Interface[]
+	 */
+	protected $observers = array();
 
 	/**
 	 * Constructor
@@ -89,18 +117,91 @@
 		$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 Exception(sprintf(__('%1$s is not a directory'), $this->projectRoot));
-		}
-
-		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
+	 *
+	 * @return GitPHP_MemoryCache|null
+	 */
+	public function GetMemoryCache()
+	{
+		return $this->memoryCache;
+	}
+
+	/**
+	 * Set memory cache instance
+	 *
+	 * @param GitPHP_MemoryCache|null $memoryCache memory cache instance
+	 */
+	public function SetMemoryCache($memoryCache)
+	{
+		$this->memoryCache = $memoryCache;
+	}
+
+	/**
+	 * Get object cache instance
+	 *
+	 * @return GitPHP_Cache|null object cache
+	 */
+	public function GetCache()
+	{
+		return $this->cache;
+	}
+
+	/**
+	 * Set object cache instance
+	 *
+	 * @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;
 	}
 
 	/**
@@ -132,7 +233,7 @@
 			return $this->projects[$project];
 
 		if (!$this->projectsLoaded) {
-			$projObj = $this->InstantiateProject($project);
+			$projObj = $this->LoadProject($project);
 			$this->projects[$project] = $projObj;
 			return $projObj;
 		}
@@ -141,16 +242,14 @@
 	}
 
 	/**
-	 * Instantiates a project object
+	 * Loads a project
 	 *
 	 * @param string $proj project
 	 * @return return GitPHP_Project project object
 	 */
-	protected function InstantiateProject($proj)
+	protected function LoadProject($proj)
 	{
 		$project = new GitPHP_Project(GitPHP_Util::AddSlash($this->projectRoot), $proj);
-
-		$this->InjectProjectDependencies($project);
 
 		$this->ApplyGlobalConfig($project);
 
@@ -160,6 +259,8 @@
 			$this->ApplyProjectSettings($project, $this->projectSettings[$proj]);
 		}
 
+		$this->InjectProjectDependencies($project);
+
 		return $project;
 	}
 
@@ -173,9 +274,47 @@
 		if (!$project)
 			return;
 
+		$compat = $project->GetCompat();
+
+		$loader = null;
+		if ($compat) {
+			$project->SetStrategy(new GitPHP_ProjectLoad_Git($this->exe));
+		} else {
+			$loader = new GitPHP_GitObjectLoader($project);
+			$project->SetStrategy(new GitPHP_ProjectLoad_Raw($loader));
+		}
+
+		$headListStrategy = null;
+		if ($compat) {
+			$headListStrategy = new GitPHP_HeadListLoad_Git($this->exe);
+		} else {
+			$headListStrategy = new GitPHP_HeadListLoad_Raw();
+		}
+		$headList = new GitPHP_HeadList($project, $headListStrategy);
+		$project->SetHeadList($headList);
+
+		$tagListStrategy = null;
+		if ($compat) {
+			$tagListStrategy = new GitPHP_TagListLoad_Git($this->exe);
+		} else {
+			$tagListStrategy = new GitPHP_TagListLoad_Raw();
+		}
+		$tagList = new GitPHP_TagList($project, $tagListStrategy);
+		$project->SetTagList($tagList);
+
+		$manager = new GitPHP_GitObjectManager($project);
+		$manager->SetCompat($compat);
+		if (!$compat) {
+			$manager->SetObjectLoader($loader);
+		}
+		$manager->SetExe($this->exe);
+		if ($this->memoryCache) {
+			$manager->SetMemoryCache($this->memoryCache);
+		}
 		if ($this->cache) {
-			$project->GetObjectManager()->SetCache($this->cache);
-		}
+			$manager->SetCache($this->cache);
+		}
+		$project->SetObjectManager($manager);
 	}
 
 	/**
@@ -183,7 +322,7 @@
 	 *
 	 * @return mixed project config
 	 */
-	public function GetConfig()
+	public function GetProjectListConfig()
 	{
 		return $this->projectConfig;
 	}
@@ -193,7 +332,7 @@
 	 *
 	 * @return array
 	 */
-	public function GetSettings()
+	public function GetProjectSettings()
 	{
 		return $this->projectSettings;
 	}
@@ -223,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')) {
@@ -257,6 +398,10 @@
 			$project->SetAbbreviateLength($config->GetValue('core.abbrev'));
 		}
 
+		if ($config->HasValue('gitphp.allowedusers')) {
+			$project->SetAllowedUsers($config->GetValue('gitphp.allowedusers', true));
+		}
+
 	}
 
 	/**
@@ -269,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);
 		}
 	}
 
@@ -308,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]);
+		}
 	}
 
 	/**
@@ -461,6 +624,9 @@
 		if (isset($projData['website']) && is_string($projData['website'])) {
 			$project->SetWebsite($projData['website']);
 		}
+		if (!empty($projData['allowedusers'])) {
+			$project->SetAllowedUsers($projData['allowedusers']);
+		}
 	}
 
 	/**
@@ -468,7 +634,7 @@
 	 *
 	 * @param array $settings the array of settings
 	 */
-	public function SetSettings($settings)
+	public function SetProjectSettings($settings)
 	{
 		if ((!$settings) || (count($settings) < 1))
 			return;
@@ -503,5 +669,54 @@
 		}
 	}
 
+	/**
+	 * Add a new observer
+	 *
+	 * @param GitPHP_Observer_Interface $observer observer
+	 */
+	public function AddObserver($observer)
+	{
+		if (!$observer)
+			return;
+
+		if (array_search($observer, $this->observers) !== false)
+			return;
+
+		$this->observers[] = $observer;
+	}
+
+	/**
+	 * Remove an observer
+	 *
+	 * @param GitPHP_Observer_Interface $observer observer
+	 */
+	public function RemoveObserver($observer)
+	{
+		if (!$observer)
+			return;
+
+		$key = array_search($observer, $this->observers);
+
+		if ($key === false)
+			return;
+
+		unset($this->observers[$key]);
+	}
+
+	/**
+	 * Log a message to observers
+	 *
+	 * @param string $message message
+	 */
+	protected function Log($message)
+	{
+		if (empty($message))
+			return;
+
+		foreach ($this->observers as $observer) {
+			$observer->ObjectChanged($this, GitPHP_Observer_Interface::LoggableChange, array($message));
+		}
+	}
+
 }
 

comments