Attach logger to projectlist as observer
Attach logger to projectlist as observer

--- a/include/DebugLog.class.php
+++ b/include/DebugLog.class.php
@@ -6,7 +6,7 @@
  * @copyright Copyright (c) 2010 Christopher Han
  * @package GitPHP
  */
-class GitPHP_DebugLog
+class GitPHP_DebugLog implements GitPHP_Observer_Interface
 {
 	/**
 	 * Stores the singleton instance
@@ -209,5 +209,28 @@
 		return $data;
 	}
 
+	/**
+	 * Notify that observable object changed
+	 *
+	 * @param GitPHP_Observable_Interface $object object
+	 * @param int $changeType type of change
+	 * @param array $args argument array
+	 */
+	public function ObjectChanged($object, $changeType, $args = array())
+	{
+		if ($changeType !== GitPHP_Observer_Interface::LoggableChange)
+			return;
+
+		if (!$this->enabled)
+			return;
+
+		if (!isset($args[0]) || empty($args[0]))
+			return;
+
+		$msg = $args[0];
+
+		$this->Log($msg);
+	}
+
 }
 

--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -128,6 +128,8 @@
 		} else {
 			$this->projectList = GitPHP_ProjectList::Instantiate(GITPHP_CONFIGDIR . 'gitphp.conf.php', true);
 		}
+
+		$this->projectList->AddObserver(GitPHP_DebugLog::GetInstance());
 
 	}
 

--- a/include/git/GitObjectManager.class.php
+++ b/include/git/GitObjectManager.class.php
@@ -288,8 +288,9 @@
 	 *
 	 * @param GitPHP_Observable_Interface $object object
 	 * @param int $changeType type of change
-	 */
-	public function ObjectChanged($object, $changeType)
+	 * @param array $args argument array
+	 */
+	public function ObjectChanged($object, $changeType, $args = array())
 	{
 		if (!$object)
 			return;

--- a/include/git/Observer.interface.php
+++ b/include/git/Observer.interface.php
@@ -17,11 +17,19 @@
 	const CacheableDataChange = 1;
 
 	/**
+	 * Change type for logger message
+	 *
+	 * @const
+	 */
+	const LoggableChange = 2;
+
+	/**
 	 * Notify that observable object changed
 	 *
 	 * @param GitPHP_Observable_Interface $object object
 	 * @param int $changeType type of change
+	 * @param array $args argument array
 	 */
-	public function ObjectChanged($object, $changeType);
+	public function ObjectChanged($object, $changeType, $args = array());
 }
 

--- a/include/git/projectlist/ProjectListArray.class.php
+++ b/include/git/projectlist/ProjectListArray.class.php
@@ -60,7 +60,7 @@
 					}
 				}
 			} catch (Exception $e) {
-				GitPHP_DebugLog::GetInstance()->Log($e->getMessage());
+				$this->Log($e->getMessage());
 			}
 		}
 	}

--- a/include/git/projectlist/ProjectListArrayLegacy.class.php
+++ b/include/git/projectlist/ProjectListArrayLegacy.class.php
@@ -49,7 +49,7 @@
 							unset($projObj);
 						}
 					} catch (Exception $e) {
-						GitPHP_DebugLog::GetInstance()->Log($e->getMessage());
+						$this->Log($e->getMessage());
 					}
 				}
 			}

--- a/include/git/projectlist/ProjectListBase.class.php
+++ b/include/git/projectlist/ProjectListBase.class.php
@@ -7,7 +7,7 @@
  * @package GitPHP
  * @subpackage Git\ProjectList
  */
-abstract class GitPHP_ProjectListBase implements Iterator
+abstract class GitPHP_ProjectListBase implements Iterator, GitPHP_Observable_Interface
 {
 	/**
 	 * Project name sort
@@ -80,11 +80,18 @@
 	protected $cache = null;
 
 	/**
-	 * Memory cache instance for all project
+	 * Memory cache instance for all projects
 	 *
 	 * @var GitPHP_MemoryCache
 	 */
 	protected $memoryCache = null;
+
+	/**
+	 * Observers
+	 *
+	 * @var GitPHP_Observer_Interface[]
+	 */
+	protected $observers = array();
 
 	/**
 	 * Constructor
@@ -560,5 +567,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));
+		}
+	}
+
 }
 

--- a/include/git/projectlist/ProjectListDirectory.class.php
+++ b/include/git/projectlist/ProjectListDirectory.class.php
@@ -59,7 +59,7 @@
 		if (!(is_dir($dir) && is_readable($dir)))
 			return;
 
-		GitPHP_DebugLog::GetInstance()->Log(sprintf('Searching directory %1$s', $dir));
+		$this->Log(sprintf('Searching directory %1$s', $dir));
 
 		if ($dh = opendir($dir)) {
 			$trimlen = strlen(GitPHP_Util::AddSlash($this->projectRoot)) + 1;
@@ -67,7 +67,7 @@
 				$fullPath = $dir . '/' . $file;
 				if ((strpos($file, '.') !== 0) && is_dir($fullPath)) {
 					if (is_file($fullPath . '/HEAD')) {
-						GitPHP_DebugLog::GetInstance()->Log(sprintf('Found project %1$s', $fullPath));
+						$this->Log(sprintf('Found project %1$s', $fullPath));
 						$projectPath = substr($fullPath, $trimlen);
 						if (!isset($this->projects[$projectPath])) {
 							$project = $this->InstantiateProject($projectPath);
@@ -80,7 +80,7 @@
 						$this->RecurseDir($fullPath);
 					}
 				} else {
-					GitPHP_DebugLog::GetInstance()->Log(sprintf('Skipping %1$s', $fullPath));
+					$this->Log(sprintf('Skipping %1$s', $fullPath));
 				}
 			}
 			closedir($dh);
@@ -107,7 +107,7 @@
 			}
 
 			if ($this->exportedOnly && !$project->GetDaemonEnabled()) {
-				GitPHP_DebugLog::GetInstance()->Log(sprintf('Project %1$s not enabled for export', $project->GetPath()));
+				$this->Log(sprintf('Project %1$s not enabled for export', $project->GetPath()));
 				return null;
 			}
 
@@ -122,7 +122,7 @@
 			return $project;
 
 		} catch (Exception $e) {
-			GitPHP_DebugLog::GetInstance()->Log($e->getMessage());
+			$this->Log($e->getMessage());
 		}
 
 		return null;

--- a/include/git/projectlist/ProjectListFile.class.php
+++ b/include/git/projectlist/ProjectListFile.class.php
@@ -83,7 +83,7 @@
 						$owner = $lineData['owner'];
 					}
 				} else {
-					GitPHP_DebugLog::GetInstance()->Log(sprintf('%1$s is not a git project', $projectRoot . $proj));
+					$this->Log(sprintf('%1$s is not a git project', $projectRoot . $proj));
 				}
 				break;
 			}

--- a/include/git/projectlist/ProjectListScmManager.class.php
+++ b/include/git/projectlist/ProjectListScmManager.class.php
@@ -84,17 +84,17 @@
 			return null;
 
 		if (!(isset($data['type']) && ($data['type'] == 'git'))) {
-			GitPHP_DebugLog::GetInstance()->Log(sprintf('%1$s is not a git project', $proj));
+			$this->Log(sprintf('%1$s is not a git project', $proj));
 			return null;
 		}
 
 		if (!(isset($data['public']) && ($data['public'] == true))) {
-			GitPHP_DebugLog::GetInstance()->Log(sprintf('%1$s is not public', $proj));
+			$this->Log(sprintf('%1$s is not public', $proj));
 			return null;
 		}
 
 		if (!is_file(GitPHP_Util::AddSlash($this->projectRoot) . $proj . '/HEAD')) {
-			GitPHP_DebugLog::GetInstance()->Log(sprintf('%1$s is not a git project', $proj));
+			$this->Log(sprintf('%1$s is not a git project', $proj));
 		}
 
 		$projectObj = new GitPHP_Project($this->projectRoot, $proj);

comments