|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* FileSystemNative |
|
5
|
|
|
* |
|
6
|
|
|
* Native Filesystem |
|
7
|
|
|
* |
|
8
|
|
|
* @package core |
|
9
|
|
|
* @author [email protected] |
|
10
|
|
|
* @copyright Caffeina srl - 2015 - http://caffeina.it |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace FileSystem; |
|
14
|
|
|
|
|
15
|
|
|
class Native implements Adapter { |
|
16
|
|
|
|
|
17
|
|
|
protected $root; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(array $options = []) { |
|
20
|
|
|
$this->root = empty($options['root'])?'/':(rtrim($options['root'],'/').'/'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function exists($path){ |
|
24
|
|
|
return file_exists($this->realPath($path)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function read($path){ |
|
28
|
|
|
return $this->exists($path) ? file_get_contents($this->realPath($path)) : false; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function write($path, $data){ |
|
32
|
|
|
$r_path = $this->realPath($path); |
|
33
|
|
|
if ( ! is_dir($r_dir = dirname($r_path)) ) @mkdir($r_dir,0775,true); |
|
34
|
|
|
return file_put_contents($r_path, $data); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function append($path, $data){ |
|
38
|
|
|
return file_put_contents($this->realPath($path), $data, FILE_APPEND); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function move($old, $new){ |
|
42
|
|
|
// Atomic |
|
43
|
|
|
if($this->exists($old)){ |
|
44
|
|
|
return $this->write($new,$this->read($old)) && $this->delete($old); |
|
45
|
|
|
} else return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function delete($path){ |
|
49
|
|
|
return $this->exists($path) ? unlink($this->realPath($path)) : false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function search($pattern, $recursive=true){ |
|
53
|
|
|
$results = []; |
|
54
|
|
|
$root_len = strlen($this->root); |
|
55
|
|
|
$rx_pattern = '('.strtr($pattern,['.'=>'\.','*'=>'.*','?'=>'.']).')Ai'; |
|
56
|
|
|
|
|
57
|
|
|
/* |
|
58
|
|
|
$files = new \RegexIterator(new \RecursiveDirectoryIterator($this->root, |
|
59
|
|
|
\RecursiveDirectoryIterator::SKIP_DOTS),$rx_pattern); |
|
60
|
|
|
foreach ($files as $path) { |
|
61
|
|
|
$results[] = trim(substr($path, $root_len),'/'); |
|
62
|
|
|
} |
|
63
|
|
|
return $results; |
|
64
|
|
|
*/ |
|
65
|
|
|
|
|
66
|
|
|
$tree = new \RegexIterator( |
|
67
|
|
|
new \RecursiveIteratorIterator( |
|
68
|
|
|
new \RecursiveDirectoryIterator( |
|
69
|
|
|
$this->root, |
|
70
|
|
|
\RecursiveDirectoryIterator::SKIP_DOTS)), |
|
71
|
|
|
$rx_pattern,\RegexIterator::GET_MATCH); |
|
72
|
|
|
|
|
73
|
|
|
$fileList = []; |
|
74
|
|
|
foreach($tree as $group) { |
|
75
|
|
|
foreach($group as $path) { |
|
76
|
|
|
$results[] = trim(substr($path, $root_len),'/'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $results; |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function realPath($path){ |
|
85
|
|
|
return $this->root . $path; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|