Implement Blob, Tree and Lazy, add tests for Tree.
[phpgit.git] / library / Git / Lazy.php
blob7cdde2fdb1ad416d50486d8914eff03241054f92
1 <?php
3 /**
4 * Implements lazy loading of properties.
6 * In the Python implementation, this is implemented as a
7 * mix-in. However, we don't have this capability in PHP, so
8 * we implement is as a super-class (as multiple mix-ins
9 * are not used). If this ends up being a problem, rewrite
10 * this class to be a decorator, and create a factory for
11 * Git objects.
13 * @warning Subclasses should be careful to call __get manually
14 * if they're not sure if a particular property will
15 * be initialized or not; because they have full access
16 * __get will not be called automatically.
18 abstract class Git_Lazy {
19 /**
20 * Whether or not this class's contents have been loaded.
22 private $_baked = false;
23 /**
24 * Marks the class as baked; use this if you loaded the
25 * contents yourself.
27 protected function markBaked() {
28 $this->_baked = true;
30 /**
31 * Implements public lazy loading.
33 public function __get($name) {
34 if ($name[0] == '_') throw new Git_Exception('Cannot access private property ' . $name);
35 // Quick and easy way of emulating Python's @property decorator
36 if (method_exists($this, $name)) {
37 return $this->$name();
39 if (isset($this->$name)) return $this->$name;
40 if (!$this->_baked) {
41 $this->bake();
42 $this->_baked = true;
44 return $this->$name;
46 /**
47 * Implements the loading operation; after this method is
48 * called the class should be ready to go.
50 abstract protected function bake();