Move git objects with load strategies into their own directories
[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
@@ -5,12 +5,19 @@
  * @author Christopher Han <xiphux@gmail.com>
  * @copyright Copyright (c) 2010 Christopher Han
  * @package GitPHP
- * @subpackage Git
+ * @subpackage Git\Blob
  */
 class GitPHP_Blob extends GitPHP_FilesystemObject implements GitPHP_Observable_Interface, GitPHP_Cacheable_Interface
 {
 
 	/**
+	 * 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
@@ -119,54 +133,17 @@
 	}
 
 	/**
-	 * Gets a file type from its octal mode
-	 *
-	 * @param string $octMode octal mode
-	 * @param boolean $local true if caller wants localized type
-	 * @return string file type
-	 */
-	public static function FileType($octMode, $local = false)
-	{
-		$mode = octdec($octMode);
-		if (($mode & 0x4000) == 0x4000) {
-			if ($local) {
-				return __('directory');
-			} else {
-				return 'directory';
-			}
-		} else if (($mode & 0xA000) == 0xA000) {
-			if ($local) {
-				return __('symlink');
-			} else {
-				return 'symlink';
-			}
-		} else if (($mode & 0x8000) == 0x8000) {
-			if ($local) {
-				return __('file');
-			} else {
-				return 'file';
-			}
-		}
-
-		if ($local) {
-			return __('unknown');
-		} else {
-			return 'unknown';
-		}
-	}
-
-	/**
 	 * Gets the blob size
 	 *
 	 * @return integer size
 	 */
 	public function GetSize()
 	{
-		if ($this->size !== null) {
-			return $this->size;
-		}
-
-		return strlen($this->GetData());
+		if ($this->size === null) {
+			$this->size = $this->strategy->Size($this);
+		}
+		
+		return $this->size;
 	}
 
 	/**
@@ -186,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;
 	}
 
 	/**
@@ -263,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