Move blob size loading to strategy
[gitphp.git] / include / git / blob / Blob.class.php
blob:a/include/git/blob/Blob.class.php -> blob:b/include/git/blob/Blob.class.php
--- a/include/git/blob/Blob.class.php
+++ b/include/git/blob/Blob.class.php
@@ -11,6 +11,13 @@
 {
 
 	/**
+	 * Large blob threshold
+	 *
+	 * @var int
+	 */
+	const LargeBlobSize = 5242880;	// 5 megs
+
+	/**
 	 * The blob data
 	 *
 	 * @var string
@@ -30,6 +37,13 @@
 	 * @var int
 	 */
 	protected $size = null;
+
+	/**
+	 * Whether the data is binary
+	 *
+	 * @var boolean|null
+	 */
+	protected $binary = null;
 
 	/**
 	 * Whether data has been encoded for serialization
@@ -125,11 +139,9 @@
 	 */
 	public function GetSize()
 	{
-		if ($this->size !== null) {
-			return $this->size;
-		}
-
-		$this->size = $this->strategy->Size($this);
+		if ($this->size === null) {
+			$this->size = $this->strategy->Size($this);
+		}
 		
 		return $this->size;
 	}
@@ -151,14 +163,15 @@
 	 */
 	public function IsBinary()
 	{
-		if (!$this->dataRead)
-			$this->ReadData();
-
-		$data = $this->GetData();
-		if (strlen($data) > 8000)
-			$data = substr($data, 0, 8000);
-
-		return strpos($data, chr(0)) !== false;
+		if ($this->binary === null) {
+			$data = $this->GetData();
+			if (strlen($data) > 8000)
+				$data = substr($data, 0, 8000);
+
+			$this->binary = (strpos($data, chr(0)) !== false);
+		}
+
+		return $this->binary;
 	}
 
 	/**
@@ -228,10 +241,15 @@
 	 */
 	public function __sleep()
 	{
-		if (!$this->dataEncoded)
+		$properties = array('data', 'dataRead', 'dataEncoded', 'binary', 'size');
+
+		if ($this->dataRead && strlen($this->data) > GitPHP_Blob::LargeBlobSize) {
+			$this->data = null;
+			$this->dataRead = false;
+		}
+		
+		if ($this->dataRead && !$this->dataEncoded)
 			$this->EncodeData();
-
-		$properties = array('data', 'dataRead', 'dataEncoded');
 
 		return array_merge($properties, parent::__sleep());
 	}

comments