-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSTraverser.php
More file actions
149 lines (131 loc) · 4.23 KB
/
FSTraverser.php
File metadata and controls
149 lines (131 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Brainexploded\FSTools;
class FSTraverser
{
protected $rootDir;
protected $callback;
protected $excludeNodes;
protected $excludeExtensions = [];
protected $allowedExtensions = [];
protected $maxDepth;
public function __construct(
$rootDir = null,
callable $callback = null,
$excludeNodes = [],
$allowedExtensions = [],
$excludeExtensions = [],
$maxDepth = null)
{
$this->rootDir = $rootDir;
$this->callback = $callback;
$this->excludeNodes = $excludeNodes;
$this->excludeExtensions = $excludeExtensions;
$this->allowedExtensions = $allowedExtensions;
$this->maxDepth = $maxDepth;
}
public function setRootDir($rootDir)
{
$this->rootDir = $rootDir;
return $this;
}
public function setCallback($callback)
{
$this->callback = $callback;
return $this;
}
public function setExcludeNodes($excludeNodes)
{
$this->excludeNodes = $excludeNodes;
return $this;
}
public function setExcludeExtensions($excludeExtensions)
{
$this->excludeExtensions = $excludeExtensions;
return $this;
}
public function setAllowedExtensions($allowedExtensions)
{
$this->allowedExtensions = $allowedExtensions;
return $this;
}
public function setMaxDepth($maxDepth)
{
$this->maxDepth = $maxDepth;
return $this;
}
public function go($returnContent = false)
{
if (!$this->rootDir) {
throw new FSTraverserException('Root directory is not specified');
}
if (!$this->callback) {
throw new FSTraverserException('Callback function is not specified');
}
$this->traverse($this->rootDir, $this->callback, $returnContent);
}
protected function traverse($path, callable $callback, $returnContent = false, $depth = 0)
{
if ($handle = opendir($path)) {
while (false !== ($entry = readdir($handle))) {
$fullpath = $path.'/'.$entry;
if (in_array($entry, ['.', '..'])) {
continue;
}
if (is_dir($fullpath) && $this->validateDir($fullpath, $depth)) {
$this->traverse($fullpath, $callback, $returnContent, $depth+1);
} elseif ($this->validateFile($fullpath)) {
if ($returnContent) {
$contentHandle = fopen($fullpath, 'rb');
if ($contentHandle === false) {
echo "[WARNING] cannot open file $fullpath", PHP_EOL;
continue;
}
$size = filesize($fullpath);
if ($size > 0) {
$content = fread($contentHandle, filesize($fullpath));
$callback($path, $entry, $content);
unset($content);
} else {
echo "[NOTICE] file $fullpath is empty!", PHP_EOL;
}
fclose($contentHandle);
} else {
$callback($path, $entry);
}
}
}
}
}
protected function validateDir($dir, $depth)
{
if (in_array($dir, $this->excludeNodes) || is_link($dir)) {
return false;
}
if ($this->maxDepth && ($depth >= $this->maxDepth)) {
return false;
}
return true;
}
protected function validateFile($file)
{
if (!file_exists($file)) {
return false;
}
if (in_array($file, $this->excludeNodes)) {
return false;
}
$ext = FSUtil::getFileExtension($file);
if ($this->allowedExtensions) {
if (!$ext || !in_array($ext, $this->allowedExtensions)) {
return false;
}
}
if ($this->excludeExtensions) {
if (in_array($ext, $this->excludeExtensions)) {
return false;
}
}
return true;
}
}
class FSTraverserException extends \Exception {}