|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see FileHelper_FileFinder} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Application Utils |
|
6
|
|
|
* @subpackage FileHelper |
|
7
|
|
|
* @see FileHelper_FileFinder |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types = 1); |
|
11
|
|
|
|
|
12
|
|
|
namespace AppUtils; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* File finder class used to fetch file lists from folders, |
|
16
|
|
|
* with criteria matching. Offers many customization options |
|
17
|
|
|
* on how to return the files, from absolute paths to file names |
|
18
|
|
|
* without extensions or even class name maps. |
|
19
|
|
|
* |
|
20
|
|
|
* @package Application Utils |
|
21
|
|
|
* @subpackage FileHelper |
|
22
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class FileHelper_FileFinder implements Interface_Optionable |
|
25
|
|
|
{ |
|
26
|
|
|
use Traits_Optionable; |
|
27
|
|
|
|
|
28
|
|
|
const PATH_MODE_ABSOLUTE = 'absolute'; |
|
29
|
|
|
|
|
30
|
|
|
const PATH_MODE_RELATIVE = 'relative'; |
|
31
|
|
|
|
|
32
|
|
|
const PATH_MODE_STRIP = 'strip'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $path; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(string $path) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->path = $this->normalizeSlashes($path); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getDefaultOptions() : array |
|
45
|
|
|
{ |
|
46
|
|
|
return array( |
|
47
|
|
|
'recursive' => false, |
|
48
|
|
|
'strip-extensions' => false, |
|
49
|
|
|
'include-extensions' => array(), |
|
50
|
|
|
'exclude-extensions' => array(), |
|
51
|
|
|
'pathmode' => self::PATH_MODE_ABSOLUTE, |
|
52
|
|
|
'slash-replacement' => null |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function normalizeSlashes($string) |
|
57
|
|
|
{ |
|
58
|
|
|
return str_replace('\\', '/', $string); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function stripExtensions() : FileHelper_FileFinder |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->setOption('strip-extensions', true); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function makeRecursive() : FileHelper_FileFinder |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->setOption('recursive', true); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getIncludeExtensions() : array |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->getArrayOption('include-extensions'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function includeExtensions(array $extensions) : FileHelper_FileFinder |
|
77
|
|
|
{ |
|
78
|
|
|
$items = $this->getIncludeExtensions(); |
|
79
|
|
|
$items = array_merge($items, $extensions); |
|
80
|
|
|
$items = array_unique($items); |
|
81
|
|
|
|
|
82
|
|
|
$this->setOption('include-extensions', $items); |
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getExcludeExtensions() : array |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->getArrayOption('exclude-extensions'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function excludeExtensions(array $extensions) : FileHelper_FileFinder |
|
92
|
|
|
{ |
|
93
|
|
|
$items = $this->getExcludeExtensions(); |
|
94
|
|
|
$items = array_merge($items, $extensions); |
|
95
|
|
|
$items = array_unique($items); |
|
96
|
|
|
|
|
97
|
|
|
$this->setOption('exclude-extensions', $items); |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function setPathmodeStrip() : FileHelper_FileFinder |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->setPathmode(self::PATH_MODE_STRIP); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function setPathmodeRelative() : FileHelper_FileFinder |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->setPathmode(self::PATH_MODE_RELATIVE); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function setPathmodeAbsolute() : FileHelper_FileFinder |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->setPathmode(self::PATH_MODE_ABSOLUTE); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function setSlashReplacement($character) |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->setOption('slash-replacement', $character); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
protected function setPathmode($mode) : FileHelper_FileFinder |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->setOption('pathmode', $mode); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getAll() : array |
|
127
|
|
|
{ |
|
128
|
|
|
if(!isset($this->found)) { |
|
129
|
|
|
$this->find($this->path, true); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $this->found; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function getPHPFiles() : array |
|
136
|
|
|
{ |
|
137
|
|
|
$this->includeExtensions(array('php')); |
|
138
|
|
|
return $this->getAll(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function getPHPClassNames() : array |
|
142
|
|
|
{ |
|
143
|
|
|
$this->includeExtensions(array('php')); |
|
144
|
|
|
$this->stripExtensions(); |
|
145
|
|
|
$this->setSlashReplacement('_'); |
|
146
|
|
|
$this->setPathmodeRelative(); |
|
147
|
|
|
|
|
148
|
|
|
return $this->getAll(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
protected $found; |
|
152
|
|
|
|
|
153
|
|
|
protected function find($path, $isRoot=false) |
|
154
|
|
|
{ |
|
155
|
|
|
if($isRoot) { |
|
156
|
|
|
$this->found = array(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$d = new \DirectoryIterator($path); |
|
160
|
|
|
foreach($d as $item) |
|
161
|
|
|
{ |
|
162
|
|
|
if($item->isDir()) |
|
163
|
|
|
{ |
|
164
|
|
|
if($this->getOption('recursive') === true && !$item->isDot()) { |
|
165
|
|
|
$this->find($item->getPathname()); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
else |
|
169
|
|
|
{ |
|
170
|
|
|
$file = $this->filterFile($item->getPathname()); |
|
171
|
|
|
if($file) { |
|
172
|
|
|
$this->found[] = $file; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
protected function filterFile($path) |
|
179
|
|
|
{ |
|
180
|
|
|
$path = $this->normalizeSlashes($path); |
|
181
|
|
|
|
|
182
|
|
|
$info = pathinfo($path); |
|
183
|
|
|
|
|
184
|
|
|
$include = $this->getOption('include-extensions'); |
|
185
|
|
|
$exclude = $this->getOption('exclude-extensions'); |
|
186
|
|
|
|
|
187
|
|
|
if(!empty($include)) |
|
188
|
|
|
{ |
|
189
|
|
|
if(!in_array($info['extension'], $include)) { |
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
else if(!empty($exclude)) |
|
194
|
|
|
{ |
|
195
|
|
|
if(in_array($info['extension'], $exclude)) { |
|
196
|
|
|
return false; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
switch($this->getOption('pathmode')) |
|
201
|
|
|
{ |
|
202
|
|
|
case self::PATH_MODE_STRIP: |
|
203
|
|
|
$path = basename($path); |
|
204
|
|
|
break; |
|
205
|
|
|
|
|
206
|
|
|
case self::PATH_MODE_RELATIVE: |
|
207
|
|
|
$path = str_replace($this->path, '', $path); |
|
208
|
|
|
$path = ltrim($path, '/'); |
|
209
|
|
|
break; |
|
210
|
|
|
|
|
211
|
|
|
case self::PATH_MODE_ABSOLUTE: |
|
212
|
|
|
default: |
|
213
|
|
|
break; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
if($this->getOption('strip-extensions') === true) |
|
217
|
|
|
{ |
|
218
|
|
|
$path = str_replace('.'.$info['extension'], '', $path); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$replace = $this->getOption('slash-replacement'); |
|
222
|
|
|
if(!empty($replace)) { |
|
223
|
|
|
$path = str_replace('/', $replace, $path); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $path; |
|
227
|
|
|
} |
|
228
|
|
|
} |