1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c)2014-2014 heiglandreas |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
13
|
|
|
* all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIBILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* @category |
24
|
|
|
* @author Andreas Heigl<[email protected]> |
25
|
|
|
* @copyright ©2014-2014 Andreas Heigl |
26
|
|
|
* @license http://www.opesource.org/licenses/mit-license.php MIT-License |
27
|
|
|
* @version 0.0 |
28
|
|
|
* @since 05.11.14 |
29
|
|
|
* @link https://github.com/heiglandreas/ |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace Org_Heigl\FileFinder; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
class FileFinder |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array $filterlist |
40
|
|
|
*/ |
41
|
|
|
protected $filterlist = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array $searchLocation |
45
|
|
|
*/ |
46
|
|
|
protected $searchLocations = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var FileListInterface $fileList |
50
|
|
|
*/ |
51
|
|
|
protected $fileList = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add a further filter |
55
|
|
|
* |
56
|
|
|
* @param FilterInterface $filter |
57
|
|
|
* |
58
|
|
|
* @return self |
59
|
|
|
*/ |
60
|
3 |
|
public function addFilter(FilterInterface $filter) |
61
|
|
|
{ |
62
|
3 |
|
$this->filterlist[] = $filter; |
63
|
|
|
|
64
|
3 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Add a further directory to search for files in |
69
|
|
|
* |
70
|
|
|
* @param string $dir |
71
|
|
|
* |
72
|
|
|
* @return self |
73
|
|
|
*/ |
74
|
3 |
|
public function addDirectory($dir) |
75
|
|
|
{ |
76
|
3 |
|
$this->searchLocations[] = $dir; |
77
|
|
|
|
78
|
3 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set the file list |
83
|
|
|
* |
84
|
|
|
* @param FileListInterface $fileList |
85
|
|
|
* |
86
|
|
|
* @return self |
87
|
|
|
*/ |
88
|
2 |
|
public function setFileList(FileListInterface $fileList) |
89
|
|
|
{ |
90
|
2 |
|
$this->fileList = $fileList; |
91
|
|
|
|
92
|
2 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the file list |
97
|
|
|
* |
98
|
|
|
* @return FileListInterface |
99
|
|
|
*/ |
100
|
3 |
|
public function getFileList() |
101
|
|
|
{ |
102
|
3 |
|
if (! $this->fileList instanceof FileListInterface) { |
103
|
2 |
|
$this->fileList = new FileList(); |
104
|
|
|
} |
105
|
3 |
|
return $this->fileList; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Do the actual searching |
110
|
|
|
* |
111
|
|
|
* @return FileListInterface[] |
112
|
|
|
*/ |
113
|
2 |
|
public function find() |
114
|
|
|
{ |
115
|
2 |
|
$this->getFileList()->clear(); |
116
|
2 |
|
foreach ($this->searchLocations as $location) { |
117
|
2 |
|
if (! is_dir($location)) { |
118
|
1 |
|
continue; |
119
|
|
|
} |
120
|
2 |
|
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($location)); |
121
|
2 |
|
foreach ($iterator as $file) { |
122
|
2 |
|
if (! $this->filter($file)) { |
123
|
2 |
|
continue; |
124
|
|
|
} |
125
|
2 |
|
$this->getFileList()->add($file); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
return $this->getFileList(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Filter the file |
134
|
|
|
* |
135
|
|
|
* @param SPLFileInfo $file |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
2 |
|
public function filter(\SPLFileInfo $file) |
140
|
|
|
{ |
141
|
2 |
|
foreach ($this->filterlist as $filter) { |
142
|
2 |
|
if (! $filter->filter($file)) { |
143
|
2 |
|
return false; |
144
|
|
|
} |
145
|
|
|
} |
146
|
2 |
|
return true; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|