Move blob size loading to strategy
Move blob size loading to strategy

--- a/include/git/blob/Blob.class.php
+++ b/include/git/blob/Blob.class.php
@@ -129,7 +129,9 @@
 			return $this->size;
 		}
 
-		return strlen($this->GetData());
+		$this->size = $this->strategy->Size($this);
+		
+		return $this->size;
 	}
 
 	/**

--- a/include/git/blob/BlobLoadStrategy.interface.php
+++ b/include/git/blob/BlobLoadStrategy.interface.php
@@ -16,5 +16,13 @@
 	 * @return string blob data
 	 */
 	public function Load($blob);
+
+	/**
+	 * Gets the size of a blob
+	 *
+	 * @param GitPHP_Blob $blob blob
+	 * @return int blob size
+	 */
+	public function Size($blob);
 }
 

--- a/include/git/blob/BlobLoad_Git.class.php
+++ b/include/git/blob/BlobLoad_Git.class.php
@@ -46,5 +46,23 @@
 
 		return $this->exe->Execute($blob->GetProject()->GetPath(), GIT_CAT_FILE, $args);
 	}
+
+	/**
+	 * Gets the size of a blob
+	 *
+	 * @param GitPHP_Blob $blob blob
+	 * @return int blob size
+	 */
+	public function Size($blob)
+	{
+		if (!$blob)
+			return;
+
+		$args = array();
+		$args[] = '-s';
+		$args[] = $blob->GetHash();
+
+		return $this->exe->Execute($blob->GetProject()->GetPath(), GIT_CAT_FILE, $args);
+	}
 }
 

--- a/include/git/blob/BlobLoad_Raw.class.php
+++ b/include/git/blob/BlobLoad_Raw.class.php
@@ -42,5 +42,19 @@
 
 		return $this->objectLoader->GetObject($blob->GetHash());
 	}
+
+	/**
+	 * Gets the size of a blob
+	 *
+	 * @param GitPHP_Blob $blob blob
+	 * @return int blob size
+	 */
+	public function Size($blob)
+	{
+		if (!$blob)
+			return;
+
+		return strlen($blob->GetData());
+	}
 }
 

--- a/test/git/blob/BlobTest.php
+++ b/test/git/blob/BlobTest.php
@@ -25,7 +25,7 @@
 	public function testSize()
 	{
 		$strategymock = $this->getMock('GitPHP_BlobLoadStrategy_Interface');
-		$strategymock->expects($this->once())->method('Load')->with($this->isInstanceOf('GitPHP_Blob'))->will($this->returnValue("data line 1\ndata line 2"));
+		$strategymock->expects($this->once())->method('Size')->with($this->isInstanceOf('GitPHP_Blob'))->will($this->returnValue(23));
 
 		$projectmock = $this->getMockBuilder('GitPHP_Project')->disableOriginalConstructor()->getMock();
 		

comments