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  $key = str_replace($this->_fred->rootPath,'~',$filename);
68  $check_time = time();
69  if(
70  ($file_time = \Rsi\Record::get($file_times = $this->session->fileTimes ?: [],$key)) &&
71  ($file_time['check'] >= $check_time - $this->filemtimeTtl)
72  ) return $file_time['time'];
73  $this->session->fileTimes = [$key => ['time' => $time = \Rsi\File::mtime($filename),'check' => $check_time]] + $file_times;
74  return $time;
75  }
76  /**
77  * Get a component (local or default).
78  * @param string $name Name of the component.
79  * @return Rsi\\Fred\\Component
80  */
81  public function component($name){
82  if(array_key_exists($name,$this->_components)) return $this->_components[$name];
83  return $this->_fred->may($name) ?: false;
84  }
85  /**
86  * Get multiple components in an array.
87  * @param string $names,... Names of the components.
88  * @return array Key = component name, value = component.
89  */
90  public function components(...$names){
91  $components = [];
92  foreach($names as $name) $components[$name] = $this->component($name);
93  return $components;
94  }
95 
96  protected function getSession(){
97  if(!$this->_session) $this->_session = new Component\Session(get_called_class());
98  return $this->_session;
99  }
100 
101 }
Rsi\Fred\Component\ping
ping()
Ping function.
Definition: Component.php:59
Rsi\Thing
Basic object.
Definition: Thing.php:13
Rsi\Fred\Component\clientConfig
clientConfig()
Public configuration.
Definition: Component.php:40
Rsi\Fred\Component\$_components
$_components
Local components (key = component name, value = component).
Definition: Component.php:16
Rsi
Rsi\Fred\Component\__destruct
__destruct()
Definition: Component.php:26
Rsi\Fred\Component\filemtime
filemtime($filename)
Filemtime with session cache.
Definition: Component.php:66
Rsi\Fred\Component\components
components(... $names)
Get multiple components in an array.
Definition: Component.php:90
Rsi\Fred\Component
Basic component class.
Definition: Component.php:8
Rsi\Fred\Component\component
component($name)
Get a component (local or default).
Definition: Component.php:81
Rsi\Fred\Component\$_config
$_config
Definition: Component.php:13
Rsi\Thing\configure
configure($config)
Configure the object.
Definition: Thing.php:55
Rsi\Fred\Component\done
done()
Definition: Component.php:34
Rsi\Fred\Component\$_session
$_session
Definition: Component.php:17
Rsi\Fred\Component\config
config($key, $default=null)
Retrieve a config value.
Definition: Component.php:53
Rsi\Fred\Component\init
init()
Definition: Component.php:30
Rsi\Thing\publish
publish($property, $visibility=self::READABLE)
Publish a property (or hide it again).
Definition: Thing.php:28
Rsi\Fred\Component\getSession
getSession()
Definition: Component.php:96
Rsi\Fred\Component\$_fred
$_fred
Definition: Component.php:12
Rsi\Fred\Component\__construct
__construct($fred, $config=null)
Definition: Component.php:19
Rsi\Fred\Component\$filemtimeTtl
$filemtimeTtl
Definition: Component.php:10
Rsi\Fred\Component\$_name
$_name
Definition: Component.php:14
Rsi\Fred
Definition: Alive.php:3