Implement Blob, Tree and Lazy, add tests for Tree.
[phpgit.git] / library / Git / Blob.php
blob9ba2bf4aa5a605c8e63914d79bd1350aab0676ae
1 <?php
3 /**
4 * Represents a binary large object in Git, usually a file in a tree.
5 */
6 class Git_Blob
8 const DEFAULT_MIME_TYPE = 'text/plain';
9 /**
10 * Instance of Git_Repo.
12 public $repo;
13 public $id;
14 public $mode;
15 public $name;
16 private $_size;
17 private $_data;
18 /**
19 * Creates an unbaked Blob containing just the specified attributes.
21 public function __construct($repo, $kwargs) {
22 $this->repo = $repo;
23 foreach ($kwargs as $k => $v) {
24 $this->$k = $v;
27 /**
28 * Size of this blob in bytes.
30 public function size() {
31 if (is_null($this->_size)) {
32 $this->_size = (int) $this->repo->git->catFile($this->id, array('s' => true));
34 return $this->_size;
36 /**
37 * Returns the binary contents of this blob.
39 public function data() {
40 if (is_null($this->_data)) {
41 $this->_data = $this->repo->git->catFile($this->id, array('p' => true));
43 return $this->_data;
45 // This function isn't implemented because we don't really have a
46 // good MIME implementation by default on PHP. Will implement after
47 // more investigation.
48 // public function mimeType() {
49 /**
50 * Basename of this blob.
52 public function basename() {
53 return basename($this->__get('name'));
55 // public static function blame($repo, $commit, $file) {
56 public function __toString() {
57 return '(Git_Blob "' . $this->id . '")';
59 public function __get($name) {
60 if (method_exists($this, $name)) return $this->$name();
61 else return $this->$name;