23 protected function format($format,$id,$index =
null,$time =
null){
24 return \Rsi\Str::replaceDate(strtr(preg_replace_callback(
'/\\((-?\\d+),(\\d+)\\)/',
function($match) use ($id){
25 return \Rsi\Str::pad(substr($id,$match[1],$match[2]),$match[2]);
26 },$format),[
'*' => $id,
'?' => $index ===
null ?
null : \
Rsi\Str::pad($index,$this->_indexSize)]),$time);
34 public function name($type,$id){
35 return $this->
format($this->_types[$type][
'format'],$id);
44 return is_file($this->
name($type,$id));
53 protected function archive($type,$id,&$filename =
null){
54 $filename = $this->
name($type,$id);
55 if($format = $this->_types[$type][
'archive'] ??
null){
56 if(!is_file($filename))
return false;
58 do $archive = $this->
format($format,$id,$index ===
null ?
null : $index++);
59 while(($index !==
null) && is_file($archive));
60 \Rsi\File::mkdir(dirname($archive));
61 if($result = \
Rsi\File::rename($filename,$archive))
62 $this->
component(
'event')->trigger(self::EVENT_ARCHIVE,$this,$type,$id,$filename,$archive);
63 return $result ? $archive :
false;
74 public function save($type,$id,$data){
75 $this->
archive($type,$id,$filename);
76 \Rsi\File::mkdir(dirname($filename));
77 if(($result = \
Rsi\File::write($filename,$data,$this->mode)) !==
false)
78 $this->
component(
'event')->trigger(self::EVENT_SAVE,$this,$type,$id,$filename);
87 public function delete($type,$id){
88 $this->
archive($type,$id,$filename);
89 if($result = \
Rsi\File::unlink($filename)) $this->
component(
'event')->trigger(self::EVENT_DELETE,$this,$type,$id);
99 protected function find($dir,$dirs,$time =
null){
101 if($filter = array_shift($dirs)){
102 $filter = [\Rsi\File::FIND_FILTER_NAME .
'//' => $filter];
103 if(!$dirs) $archives += \Rsi\File::find($dir,
104 [\
Rsi\File::FIND_FILTER_TYPE => \
Rsi\File::FIND_TYPE_FILE] +
106 ($time ? [\
Rsi\File::FIND_FILTER_TIME .
'<' => $time] : [])
108 elseif($subs = \
Rsi\
File::find($dir,[\
Rsi\File::FIND_FILTER_TYPE => \
Rsi\File::FIND_TYPE_DIR] + $filter))
109 foreach($subs as $sub => $info) $archives += $this->
find($sub,$dirs,$time);
121 if($format = $this->_types[$type][
'archive'] ??
null){
123 foreach(preg_split(
'/[\\/\\\\]/',str_replace(
'*',$id ?:
'*',$format)) as $dir){
124 if(!$dirs && !preg_match(
'/[\\(\\[\\?\\*]/',$dir)) $root[] = $dir;
126 $dir = preg_quote($dir,
'/');
127 $dir = preg_replace(
'/\\\\\\(.*?,(\\d+)\\\\\\)/',
'.{$1}',$dir);
128 $dir = preg_replace(
'/\\\\\\[.*?\\\\\\]/',
'\\w+?',$dir);
129 $dir = strtr($dir,[
'\\?' =>
'\\d+',
'\\*' =>
'.*?']);
130 $dirs[] =
'/^' . $dir .
'$/';
133 return array_keys($this->
find(implode(
'/',$root),$dirs,$time));
144 if(($archives = $this->
archives($type,
null,$time)) ===
null)
return null;
146 foreach($archives as $archive)
if(\
Rsi\File::unlink($archive)) $count++;
155 foreach($this->_types as $type => $config)
if($time = $config[
'purge'] ??
null) $count += $this->
purge($type,$time);