Use load strategies on tag list
Use load strategies on tag list

--- a/include/git/TagList.class.php
+++ b/include/git/TagList.class.php
@@ -9,6 +9,42 @@
  */
 class GitPHP_TagList extends GitPHP_RefList
 {
+	/**
+	 * Data load strategy
+	 *
+	 * @var GitPHP_TagListLoadStrategy_Interface
+	 */
+	protected $strategy;
+
+	/**
+	 * Constructor
+	 *
+	 * @param GitPHP_Project $project project
+	 * @param GitPHP_TagListLoadStrategy_Interface $strategy load strategy
+	 */
+	public function __construct($project, GitPHP_TagListLoadStrategy_Interface $strategy)
+	{
+		parent::__construct($project);
+
+		if (!$strategy)
+			throw new Exception('Tag list load strategy is required');
+
+		$this->SetStrategy($strategy);
+	}
+
+	/**
+	 * Set the load strategy
+	 *
+	 * @param GitPHP_TagListLoadStrategy_Interface $strategy load strategy
+	 */
+	public function SetStrategy(GitPHP_TagListLoadStrategy_Interface $strategy)
+	{
+		if (!$strategy)
+			return;
+
+		$this->strategy = $strategy;
+	}
+
 	/**
 	 * Gets the tags
 	 *
@@ -35,10 +71,7 @@
 	{
 		$this->dataLoaded = true;
 
-		if ($this->compat)
-			$this->refs = $this->ReadRefListGit('tags');
-		else
-			$this->refs = $this->ReadRefListRaw('tags');
+		$this->refs = $this->strategy->Load($this);
 	}
 
 	/**
@@ -70,46 +103,7 @@
 	 */
 	public function GetOrderedTags($order, $count = 0)
 	{
-		if (!$this->dataLoaded)
-			$this->LoadData();
-
-		if ($this->compat) {
-			$ordered = $this->GetOrderedRefsGit('tags', $order, $count);
-			$tags = array();
-			foreach ($ordered as $tag) {
-				if (isset($this->refs[$tag])) {
-					$tags[] = $this->project->GetObjectManager()->GetTag($tag, $this->refs[$tag]);
-				}
-			}
-			return $tags;
-		} else {
-			return $this->GetOrderedTagsRaw($order, $count);
-		}
-	}
-
-	/**
-	 * Get tags in a specific order
-	 *
-	 * @param string $order order to use
-	 * @param int $count limit the number of results
-	 * @return GitPHP_Tag[] array of tags
-	 */
-	private function GetOrderedTagsRaw($order, $count = 0)
-	{
-		$tags = array();
-		foreach ($this->refs as $tag => $hash) {
-			$tags[] = $this->project->GetObjectManager()->GetTag($tag, $hash);
-		}
-
-		/* TODO add different orders */
-		if ($order == '-creatordate') {
-			usort($tags, array('GitPHP_Tag', 'CompareCreationEpoch'));
-		}
-
-		if (($count > 0) && (count($tags) > $count)) {
-			$tags = array_slice($tags, 0, $count);
-		}
-		return $tags;
+		return $this->strategy->LoadOrdered($this, $order, $count);
 	}
 
 	/**

--- /dev/null
+++ b/include/git/TagListLoadStrategy.interface.php
@@ -1,1 +1,13 @@
+<?php
+/**
+ * Marker interface for tag list load strategies
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Git
+ */
+interface GitPHP_TagListLoadStrategy_Interface extends GitPHP_RefListLoadStrategy_Interface
+{
+}
 

--- /dev/null
+++ b/include/git/TagListLoad_Git.class.php
@@ -1,1 +1,50 @@
+<?php
+/**
+ * Tag list load strategy using git exe
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Git
+ */
+class GitPHP_TagListLoad_Git extends GitPHP_RefListLoad_Git implements GitPHP_TagListLoadStrategy_Interface
+{
+	/**
+	 * Loads the tag list
+	 *
+	 * @param GitPHP_TagList $tagList tag list
+	 * @return array array of tag hashes
+	 */
+	public function Load($tagList)
+	{
+		return $this->GetRefs($tagList, 'tags');
+	}
 
+	/**
+	 * Loads sorted tags
+	 *
+	 * @param GitPHP_TagList $tagList tag list
+	 * @param string $order list order
+	 * @param integer $count number to load
+	 */
+	public function LoadOrdered($tagList, $order, $count = 0)
+	{
+		if (!$tagList)
+			return;
+
+		$ordered = $this->GetOrderedRefs($tagList, 'tags', $order, $count);
+
+		if (!$ordered)
+			return;
+
+		$tagObjs = array();
+		foreach ($ordered as $tag) {
+			if ($tagList->Exists($tag)) {
+				$tagObjs[] = $tagList->GetTag($tag);
+			}
+		}
+
+		return $tagObjs;
+	}
+}
+

--- /dev/null
+++ b/include/git/TagListLoad_Raw.class.php
@@ -1,1 +1,52 @@
+<?php
+/**
+ * Tag list load strategy using raw objects
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Git
+ */
+class GitPHP_TagListLoad_Raw extends GitPHP_RefListLoad_Raw implements GitPHP_TagListLoadStrategy_Interface
+{
+	/**
+	 * Loads the tag list
+	 *
+	 * @param GitPHP_TagList $tagList tag list
+	 * @return array array of tag hashes
+	 */
+	public function Load($tagList)
+	{
+		return $this->GetRefs($tagList, 'tags');
+	}
 
+	/**
+	 * Loads sorted tags
+	 *
+	 * @param GitPHP_TagList $tagList tag list
+	 * @param string $order list order
+	 * @param integer $count number to load
+	 */
+	public function LoadOrdered($tagList, $order, $count = 0)
+	{
+		if (!$tagList)
+			return;
+
+		if (empty($order))
+			return;
+
+		$tags = $tagList->GetTags();
+
+		/* TODO add different orders */
+		if ($order == '-creatordate') {
+			usort($tags, array('GitPHP_Tag', 'CompareCreationEpoch'));
+		}
+
+		if (($count > 0) && (count($tags) > $count)) {
+			$tags = array_slice($tags, 0, $count);
+		}
+
+		return $tags;
+	}
+}
+

--- a/include/git/projectlist/ProjectListBase.class.php
+++ b/include/git/projectlist/ProjectListBase.class.php
@@ -245,7 +245,13 @@
 		$headList->SetCompat($compat);
 		$project->SetHeadList($headList);
 
-		$tagList = new GitPHP_TagList($project);
+		$tagListStrategy = null;
+		if ($compat) {
+			$tagListStrategy = new GitPHP_TagListLoad_Git(GitPHP_GitExe::GetInstance());
+		} else {
+			$tagListStrategy = new GitPHP_TagListLoad_Raw();
+		}
+		$tagList = new GitPHP_TagList($project, $tagListStrategy);
 		$tagList->SetCompat($compat);
 		$project->SetTagList($tagList);
 

comments