-
Notifications
You must be signed in to change notification settings - Fork 0
/
FolderWalker.class.php
89 lines (80 loc) · 3.41 KB
/
FolderWalker.class.php
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
<?php
/**
* an abstract class that walk recursively through a given path.
* Activating the walking can be done by calling the StartWalking($pathtofolder) function
* This class contains 4 abstract function that the user needs to be implemented:
* 1. shouldProcessFolder: a function that will be called when walking process finds a folder. Expected to return true of false
* 2. processFolder: a function that will be called if shouldProcessFolder function returns true
* 3. shouldProcessFile: behave the same as FolderFilter but instead of folder, this function subject is file
* 4. processFile: a function that will be called if shouldProcessFile returns true
* 5. shouldBrowseFolder: a function that will be called to decide whether or not browse a folder
*/
abstract class FolderWalker {
public $mFolderPath;
public $mDirIter;
const DS = DIRECTORY_SEPARATOR;
public function __construct() {
}
/**
* the function to activate walking process. Default setting is walk recursively, but can be change easily
*/
public function startWalking($pFolderPath, $pRecursiveWalk = true) {
$this->mFolderPath = $pFolderPath;
$this->mDirIter = new DirectoryIterator($this->mFolderPath);
return $this->walkDirectory($this->mDirIter, $pRecursiveWalk);
}
/**
* the recursive function that walks through a directory of given path
*/
protected function walkDirectory(DirectoryIterator $Directory, $pRecursiveWalk, $depth = 0) {
$mStringFolderTree = str_repeat(' ', ($depth * 5)) . basename($Directory->getPath()) . self::DS . "\n";
while ($Directory->valid()) {
$node = $Directory->current();
if ($node->isDir() && $node->isReadable() && !$node->isDot()) {
if ($this->shouldProcessFolder($Directory, $node)) {
$this->processFolder($Directory, $node);
}
if ($pRecursiveWalk && $node->isReadable() && $this->shouldBrowseFolder($Directory, $node)) {
try {
$mStringFolderTree .= $this->walkDirectory(new DirectoryIterator($node->getPathname()), $pRecursiveWalk, $depth + 1);
} catch (Exception $e) {
echo "Failed to open " . $node->getPathname() . "\n";
}
}
}
elseif ($node->isFile()) {
if($this->shouldProcessFile($Directory, $node)) {
$this->processFile($Directory, $node);
}
$mStringFolderTree .= str_repeat(' ', ((1 + $depth) * 5)) . $node->getFilename() . "\n";
}
$Directory->next();
}
return $mStringFolderTree;
}
/**
* Should we process this directory?
* User need to implement this. Expected to return true or false
*/
abstract protected function shouldProcessFolder(DirectoryIterator $pParent, DirectoryIterator $pNode);
/**
* What should we do with this directory?
* User need to implement this. This is a procedure
*/
abstract protected function processFolder(DirectoryIterator $pParent, DirectoryIterator $pNode);
/**
* Should we process this file?
* User need to implement this. Expected to return true or false
*/
abstract protected function shouldProcessFile(DirectoryIterator $pParent, DirectoryIterator $pNode);
/**
* What should we do with this file?
* User need to implement this. This is a procedure
*/
abstract protected function processFile(DirectoryIterator $pParent, DirectoryIterator $pNode);
/**
* Should we browse this directory?
* User need to implement this. Expected to return true or false
*/
abstract protected function shouldBrowseFolder(DirectoryIterator $pParent, DirectoryIterator $pNode);
}