1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW\Helpers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class use to read a directory and sub-directories |
7
|
|
|
*/ |
8
|
|
|
class ReadDirectory |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string $calledClass : Name of the current class. |
12
|
|
|
* For recall the correct class when she's extended. |
13
|
|
|
*/ |
14
|
|
|
protected $calledClass = ''; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array $list : List all path found |
18
|
|
|
*/ |
19
|
|
|
protected $list; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array $ignore : Item to ignore during the reading of directories |
23
|
|
|
*/ |
24
|
|
|
private $ignore = ['.', '..']; |
25
|
|
|
|
26
|
|
|
/* |
27
|
|
|
* Constructeur |
28
|
|
|
* |
29
|
|
|
* @param array &$listFiles : List of file(s) found |
30
|
|
|
*/ |
31
|
|
|
public function __construct(&$listFiles) |
32
|
|
|
{ |
33
|
|
|
$this->calledClass = get_called_class(); |
34
|
|
|
$this->list = &$listFiles; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Read all the directories |
39
|
|
|
* |
40
|
|
|
* @param string $path : Path to read |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function run($path) |
45
|
|
|
{ |
46
|
|
|
$dir = opendir($path); |
47
|
|
|
if ($dir === false) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
//Tant qu'il y a des fichiers à lire dans le dossier |
52
|
|
|
while (($file = readdir($dir)) !== false) { |
53
|
|
|
$action = $this->fileAction($file, $path); |
54
|
|
|
|
55
|
|
|
if ($action === 'continue') { |
56
|
|
|
continue; |
57
|
|
|
} elseif ($action === 'break') { |
58
|
|
|
break; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
//If it's a directory |
62
|
|
|
if (is_dir($path.'/'.$file)) { |
63
|
|
|
$this->dirAction($path.'/'.$file); |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
closedir($dir); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Action to do when a file is found. |
73
|
|
|
* |
74
|
|
|
* @param string $fileName The file's name |
75
|
|
|
* @param string $pathToFile The file's path |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function fileAction($fileName, $pathToFile) |
80
|
|
|
{ |
81
|
|
|
if (in_array($fileName, $this->ignore)) { |
82
|
|
|
return 'continue'; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Recall ReadDirectory to read this directory |
88
|
|
|
* This is to avoid having the recursion error |
89
|
|
|
* |
90
|
|
|
* @param string $directory |
91
|
|
|
*/ |
92
|
|
|
protected function dirAction($directory) |
93
|
|
|
{ |
94
|
|
|
$read = new $this->calledClass($this->list); |
95
|
|
|
$read->run($directory); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|