1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW\Helpers; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class use to read a directory and sub-directories |
9
|
|
|
*/ |
10
|
|
|
class ReadDirectory |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @const ERR_RUN_OPENDIR Exception code if opendir fail |
14
|
|
|
*/ |
15
|
|
|
const ERR_RUN_OPENDIR = 1606001; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string $calledClass : Name of the current class. |
19
|
|
|
* For recall the correct class when she's extended. |
20
|
|
|
*/ |
21
|
|
|
protected $calledClass = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array $list : List all path found |
25
|
|
|
*/ |
26
|
|
|
protected $list; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array $ignore : Item to ignore during the reading of directories |
30
|
|
|
*/ |
31
|
|
|
protected $ignore = ['.', '..']; |
32
|
|
|
|
33
|
|
|
/* |
34
|
|
|
* Constructeur |
35
|
|
|
* |
36
|
|
|
* @param array &$listFiles : List of file(s) found |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array &$listFiles) |
39
|
|
|
{ |
40
|
|
|
$this->calledClass = get_called_class(); |
41
|
|
|
$this->list = &$listFiles; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Getter accessor to the property calledClass |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getCalledClass(): string |
50
|
|
|
{ |
51
|
|
|
return $this->calledClass; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Getter accessor to the property list |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getList(): array |
60
|
|
|
{ |
61
|
|
|
return $this->list; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Getter accessor to the property ignore |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function getIgnore(): array |
70
|
|
|
{ |
71
|
|
|
return $this->ignore; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Read all the directories |
76
|
|
|
* |
77
|
|
|
* @param string $path : Path to read |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function run(string $path) |
82
|
|
|
{ |
83
|
|
|
$dir = opendir($path); |
84
|
|
|
if ($dir === false) { |
85
|
|
|
throw new Exception( |
86
|
|
|
'The directory can not be open. ' |
87
|
|
|
.'See php error log for more informations.', |
88
|
|
|
self::ERR_RUN_OPENDIR |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
//Tant qu'il y a des fichiers à lire dans le dossier |
93
|
|
|
while (($file = readdir($dir)) !== false) { |
94
|
|
|
$action = $this->itemAction($file, $path); |
95
|
|
|
|
96
|
|
|
if ($action === 'continue') { |
97
|
|
|
continue; |
98
|
|
|
} elseif ($action === 'break') { |
99
|
|
|
break; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//If it's a directory |
103
|
|
|
if (is_dir($path.'/'.$file)) { |
104
|
|
|
$this->dirAction($path.'/'.$file); |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
closedir($dir); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Action to do when an item (file or directory) is found. |
114
|
|
|
* |
115
|
|
|
* @param string $fileName The file's name |
116
|
|
|
* @param string $pathToFile The file's path |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function itemAction(string $fileName, string $pathToFile): string |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
if (in_array($fileName, $this->ignore)) { |
123
|
|
|
return 'continue'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return ''; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Recall ReadDirectory to read this directory |
131
|
|
|
* This is to avoid having the recursion error |
132
|
|
|
* |
133
|
|
|
* @param string $dirPath |
134
|
|
|
*/ |
135
|
|
|
protected function dirAction(string $dirPath) |
136
|
|
|
{ |
137
|
|
|
$read = new $this->calledClass($this->list); |
138
|
|
|
$read->run($dirPath); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.