FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Stream.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class Stream extends Component{
6 
7  public $protocol = 'fred';
8  public $className = __CLASS__ . '\\Wrapper';
9 
10  public $handlers = [];
11 
12  protected function init(){
13  parent::init();
14  stream_wrapper_register($this->protocol,$this->className);
15  }
16  /**
17  * Check if the handlers exist.
18  * @param array $handlers
19  * @return bool True if all handlers exist.
20  */
21  public function handlersExist($handlers){
22  foreach($handlers as $handler) if(!array_key_exists($handler,$this->handlers)) return false;
23  return true;
24  }
25  /**
26  * Get a stream handler.
27  * @param string $name Stream handler name.
28  * @return Stream\\Handler
29  */
30  public function handler($name){
31  if(!array_key_exists($name,$this->handlers)) throw new \OutOfRangeException('Unknown stream handler');
32  if(!array_key_exists(null,$config =& $this->handlers[$name])){
33  $class_name = \Rsi\Record::get($config,'className',__CLASS__ . '\\Handler\\' . ucfirst($name));
34  $config[null] = new $class_name($this,$config);
35  }
36  return $config[null];
37  }
38  /**
39  * Search for files.
40  * @param array $handlers Downstream handlers.
41  * @param string $mask Filter mask.
42  * @return array Filenames found.
43  */
44  public function search($handlers,$mask = '*'){
45  if(!$handlers) return [];
46  $handler = array_shift($handlers);
47  return $this->handler($handler)->search($handlers,$mask);
48  }
49  /**
50  * Check if a file exists.
51  * @param array $handlers Downstream handlers.
52  * @param string $filename Filename.
53  * @param array $params Parameters.
54  * @return bool
55  */
56  public function exists($handlers,$filename,$params = null){
57  if(!$handlers) return false;
58  $handler = array_shift($handlers);
59  return $this->handler($handler)->exists($handlers,$filename,$params);
60  }
61  /**
62  * Modification time of a file.
63  * @param array $handlers Downstream handlers.
64  * @param string $filename Filename.
65  * @param array $params Parameters.
66  * @return int Unix timestamp.
67  */
68  public function time($handlers,$filename,$params = null){
69  if(!$handlers) return false;
70  $handler = array_shift($handlers);
71  return $this->handler($handler)->time($handlers,$filename,$params);
72  }
73  /**
74  * Read a file.
75  * @param array $handlers Downstream handlers.
76  * @param string $filename Filename.
77  * @param array $params Parameters.
78  * @return string Binary data.
79  */
80  public function read($handlers,$filename,$params = null){
81  if(!$handlers) return false;
82  $handler = array_shift($handlers);
83  return $this->handler($handler)->read($handlers,$filename,$params);
84  }
85  /**
86  * Save a file.
87  * @param array $handlers Downstream handlers.
88  * @param string $filename Filename.
89  * @param array $params Parameters.
90  * @param string $data Binary data.
91  * @return bool True when succesful.
92  */
93  public function save($handlers,$filename,$params,$data){
94  if(!$handlers) return false;
95  $handler = array_shift($handlers);
96  return $this->handler($handler)->save($handlers,$filename,$params,$data);
97  }
98  /**
99  * Save a file.
100  * @param array $handlers Downstream handlers.
101  * @param string $filename Filename.
102  * @param array $params Parameters.
103  * @return bool True when succesful.
104  */
105  public function delete($handlers,$filename,$params = null){
106  if(!$handlers) return false;
107  $handler = array_shift($handlers);
108  return $this->handler($handler)->delete($handlers,$filename,$params);
109  }
110 
111 }
save($handlers, $filename, $params, $data)
Save a file.
Definition: Stream.php:93
read($handlers, $filename, $params=null)
Read a file.
Definition: Stream.php:80
exists($handlers, $filename, $params=null)
Check if a file exists.
Definition: Stream.php:56
handler($name)
Get a stream handler.
Definition: Stream.php:30
handlersExist($handlers)
Check if the handlers exist.
Definition: Stream.php:21
Basic component class.
Definition: Component.php:8
search($handlers, $mask=' *')
Search for files.
Definition: Stream.php:44
time($handlers, $filename, $params=null)
Modification time of a file.
Definition: Stream.php:68