FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Component.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 /**
6  * Basic component class.
7  */
8 class Component extends \Rsi\Thing{
9 
10  public $filemtimeTtl = 0;
11 
12  protected $_fred = null;
13  protected $_config = null;
14  protected $_name = null;
15 
16  protected $_components = []; //!< Local components (key = component name, value = component).
17  protected $_session = null;
18 
19  public function __construct($fred,$config = null){
20  $this->_fred = $fred;
21  $this->_config = $config;
22  $this->publish('fred');
23  $this->init();
24  }
25 
26  public function __destruct(){
27  $this->done();
28  }
29 
30  protected function init(){
31  $this->configure($this->_config);
32  }
33 
34  protected function done(){
35  }
36  /**
37  * Public configuration.
38  * @return array Public configuration for this component in key => value pairs.
39  */
40  public function clientConfig(){
41  if($config = $this->config('client')) foreach($config as $key => &$value)
42  if(is_string($value)) $value = $this->component('trans')->str($value);
43  unset($value);
44  return $config ?: [];
45  }
46  /**
47  * Retrieve a config value.
48  * @param string|array $key Key to look at. An array indicates a nested key. If the array is ['foo' => ['bar' => 'acme']],
49  * then the nested key for the 'acme' value will be ['foo','bar'].
50  * @param mixed $default Default value if the key does not exist.
51  * @return mixed Found value, or default value if the key does not exist.
52  */
53  public function config($key,$default = null){
54  return \Rsi\Record::get($this->_config,$key,$default);
55  }
56  /**
57  * Ping function.
58  */
59  public function ping(){
60  }
61  /**
62  * Filemtime with session cache.
63  * @param string $filename File to get modification time for.
64  * @return int Last modification time. False if file does not exists.
65  */
66  public function filemtime($filename){
67  $check_time = time();
68  if(
69  ($file_time = \Rsi\Record::get($file_times = $this->session->fileTimes ?: [],$filename)) &&
70  ($file_time['check'] >= $check_time - $this->filemtimeTtl)
71  ) return $file_time['time'];
72  $this->session->fileTimes = $file_times + [$filename => ['time' => $time = \Rsi\File::mtime($filename),'check' => $check_time]];
73  return $time;
74  }
75  /**
76  * Get a component (local or default).
77  * @param string $name Name of the component.
78  * @return Rsi\\Fred\\Component
79  */
80  public function component($name){
81  if(array_key_exists($name,$this->_components)) return $this->_components[$name];
82  return $this->_fred->may($name) ?: false;
83  }
84  /**
85  * Get multiple components in an array.
86  * @param string $names,... Names of the components.
87  * @return array Key = component name, value = component.
88  */
89  public function components(...$names){
90  $components = [];
91  foreach($names as $name) $components[$name] = $this->component($name);
92  return $components;
93  }
94 
95  protected function getSession(){
96  if(!$this->_session) $this->_session = new Component\Session(get_called_class());
97  return $this->_session;
98  }
99 
100 }
__construct($fred, $config=null)
Definition: Component.php:19
Basic object.
Definition: Thing.php:13
ping()
Ping function.
Definition: Component.php:59
config($key, $default=null)
Retrieve a config value.
Definition: Component.php:53
configure($config)
Configure the object.
Definition: Thing.php:45
filemtime($filename)
Filemtime with session cache.
Definition: Component.php:66
publish($property, $visibility=self::READABLE)
Publish a property (or hide it again).
Definition: Thing.php:27
$_components
Local components (key = component name, value = component).
Definition: Component.php:16
Basic component class.
Definition: Component.php:8
clientConfig()
Public configuration.
Definition: Component.php:40
components(... $names)
Get multiple components in an array.
Definition: Component.php:89
component($name)
Get a component (local or default).
Definition: Component.php:80