FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Vars.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 /**
6  * Variables component (dynamic configuration).
7  */
8 class Vars extends Component{
9 
10  public $lockItem = 'variables';
11 
12  protected $_filename = null; //!< Storage file (JSON formatted).
13 
14  protected $_items = null;
15 
16  /**
17  * Retrieve a value.
18  * @param string $key
19  * @param string $time Time for historic value (empty = current).
20  * @return mixed Value (false = not found).
21  */
22  public function value($key,$time = null){
23  if(array_key_exists($key,$this->items)){
24  $values = $this->items[$key];
25  if(!$time) return array_pop($values);
26  foreach(array_reverse($values,true) as $start => $value) if(is_numeric($start) && ($start <= $time)) return $value;
27  }
28  return false;
29  }
30  /**
31  * Add a value.
32  * @param string $key
33  * @param mixed $value
34  * @param int $start Time from when on the value is to be used (null = now).
35  * @return bool True on success.
36  */
37  public function add($key,$value,$start = null){
38  if($start === null) $start = time();
39  $lock = $this->component('lock');
40  while(!$lock->request($this->lockItem)) sleep(1);
41  $result = false;
42  try{
43  if(array_key_exists($key,$items = \Rsi\File::jsonDecode($this->_filename))){
44  $items[$key][$start] = $value;
45  ksort($items[$key]);
46  }
47  else $items[$key] = [$start => $value];
48  $result = \Rsi\File::jsonEncode($this->_filename,$items);
49  }
50  catch(\Exception $e){
51  $this->component('log')->info('Unable to add: ' . $e->getMessage(),__FILE__,__LINE__);
52  }
53  $lock->release($this->lockItem);
54  return $result;
55  }
56  /**
57  * All categories.
58  * @return array Key = category, value = needed right for editing.
59  */
60  public function categories(){
61  $categories = \Rsi\Record::get($this->items,null) ?: [];
62  foreach($this->items as $key => $item)
63  if($key && ($category = \Rsi\Record::get($item,'category')) && !array_key_exists($category,$categories))
64  $categories[$category] = null;
65  return $categories;
66  }
67  /**
68  * All config items for a category.
69  * @param string $category
70  * @return array Key = item key, value = edit mask (reg-exp or widget params).
71  */
72  public function items($category){
73  $items = [];
74  foreach($this->items as $key => $item) if(\Rsi\Record::get($item,'category') == $category)
75  $items[$key] = \Rsi\Record::get($item,'mask');
76  return $items;
77  }
78 
79  protected function getItems(){
80  if($this->_items === null){
81  while($this->component('lock')->exists($this->lockItem)) sleep(1);
82  $this->_items = \Rsi\File::jsonDecode($this->_filename);
83  if(!$this->_items) throw new Exception('Could not decode items');
84  }
85  return $this->_items;
86  }
87 
88  protected function _get($key){
89  return $this->value($key);
90  }
91 
92  protected function _set($key,$value){
93  $this->add($key,$value);
94  }
95 
96 }
Variables component (dynamic configuration).
Definition: Vars.php:8
add($key, $value, $start=null)
Add a value.
Definition: Vars.php:37
_set($key, $value)
Definition: Vars.php:92
getItems()
Definition: Vars.php:79
value($key, $time=null)
Retrieve a value.
Definition: Vars.php:22
$_filename
Storage file (JSON formatted).
Definition: Vars.php:12
_get($key)
Definition: Vars.php:88
Basic component class.
Definition: Component.php:8
categories()
All categories.
Definition: Vars.php:60
items($category)
All config items for a category.
Definition: Vars.php:72
component($name)
Get a component (local or default).
Definition: Component.php:80