27 protected function key($item,$id,$user =
null){
28 return implode($this->keySeperator,array_merge([$user ?: $this->
component(
'user')->
id,$item],is_array($id) ? $id : [$id]));
37 protected function filename($item,$id,$user =
null){
38 return $this->path . $this->
key($item,$id ===
true ? $this->multiIndicator : $id,$user) .
$this->ext;
47 protected function find($item =
null,$id =
null,$user =
null){
48 return glob($this->
filename($item ?:
'*',$id ?:
'*',$user),GLOB_NOSORT);
63 public function exists($item,$id =
null,$user =
null){
65 $time = time() - $this->timeout;
67 foreach($this->
find($item,$id,$user ?:
'*') as $lock)
68 if(
filemtime($lock) < $time) \Rsi\File::unlink($lock);
69 elseif(($user !==
false) || (\
Rsi\Str::part(basename($lock),$this->keySeperator) != $this->
component(
'user')->
id)) $count++;
78 public function request($item,$id =
null){
79 $this->
component(
'alive')->register($this->_name);
80 clearstatcache(
true,$lock = $this->
filename($item,$id ?:
true));
83 if(!is_file($lock) && ($this->
exists($item,$id ?:
true,
false) || ($id && $this->
exists($item,
true)))) $lock =
false;
84 else $error = \Rsi\File::write($lock,
null,0666) ===
false;
87 if($this->_fred->debug)
throw $e;
90 if($lock && ($error || ($this->
exists($item,$id) > 1) || ($id && $this->
exists($item,
true)))){
91 \Rsi\File::unlink($lock);
94 return $lock && is_file($lock);
102 public function refresh($item =
null,$id =
null){
103 $result = count($locks = $this->
find($item,$id)) > 0;
104 foreach($locks as $lock)
105 if(!touch($lock) || ($this->
exists($item,$id) > 1) || ($id && $this->
exists($item,
true))){
106 \Rsi\File::unlink($lock);
117 public function release($item =
null,$id =
null){
119 foreach($this->
find($item,$id) as $lock) $result &= \Rsi\File::unlink($lock);
128 public function owner($item,$id =
null){
129 return ($filenames = $this->
find($item,$id,
'*') ?: ($id ? $this->
find($item,
true,
'*') :
null))
130 ? \Rsi\Str::part(basename(array_pop($filenames)),$this->keySeperator)
135 if(!$this->_path) $this->_path = $this->
config(
'path') ?: \Rsi\File::tempDir();
140 if($this->_timeout ===
null) $this->_timeout = $this->
config(
'timeout') ?: $this->
component(
'alive')->interval * 120;
145 $this->_timeout = $value;