FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Container.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\Storage;
4 
5 abstract class Container extends \Rsi\Thing{
6 
7  protected $_storage = null;
8  protected $_key = null;
9 
10  protected $_data = null;
11  protected $_changed = false;
12 
13  public function __construct($storage,$options,$key){
14  $this->_storage = $storage;
15  $this->configure($options);
16  $this->_key = $key;
17  }
18 
19  public function __destruct(){
20  $this->commit();
21  }
22 
23  abstract protected function open();
24 
25  abstract protected function save();
26 
27  public function read($key,$default = null){
28  return \Rsi\Record::get($this->data,$key,$default);
29  }
30 
31  public function write($key,$value){
32  if($value !== $this->read($key)){
33  \Rsi\Record::set($this->_data,$key,$value);
34  $this->_changed = true;
35  }
36  }
37 
38  public function remove($key){
39  if(\Rsi\Record::exists($this->data,$key)){
40  \Rsi\Record::delete($this->_data,$key);
41  $this->_changed = true;
42  }
43  }
44 
45  public function clear(){
46  $this->_data = null;
47  $this->_changed = true;
48  }
49 
50  public function commit(){
51  if($this->_changed) $this->save();
52  $this->_changed = false;
53  }
54 
55  public function rollback(){
56  $this->_data = null;
57  $this->_changed = false;
58  }
59 
60  protected function _get($key){
61  return $this->read($key);
62  }
63 
64  protected function _set($key,$value){
65  $this->write($key,$value);
66  }
67 
68  protected function getData(){
69  if($this->_data === null) $this->open();
70  return $this->_data;
71  }
72 
73 }
Basic object.
Definition: Thing.php:13
configure($config)
Configure the object.
Definition: Thing.php:45
read($key, $default=null)
Definition: Container.php:27
__construct($storage, $options, $key)
Definition: Container.php:13