1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2015, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Go\Instrument\FileSystem; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Enumerates files in the concrete directory, applying filtration logic |
15
|
|
|
*/ |
16
|
|
|
class Enumerator |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Path to the root directory, where enumeration should start |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $rootDirectory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* List of additional include paths, should be below rootDirectory |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $includePaths; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* List of additional exclude paths, should be below rootDirectory |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $excludePaths; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Initializes an enumerator |
42
|
|
|
* |
43
|
|
|
* @param string $rootDirectory Path to the root directory |
44
|
|
|
* @param array $includePaths List of additional include paths |
45
|
|
|
* @param array $excludePaths List of additional exclude paths |
46
|
|
|
*/ |
47
|
5 |
|
public function __construct($rootDirectory, array $includePaths = [], array $excludePaths = []) |
48
|
|
|
{ |
49
|
5 |
|
$this->rootDirectory = $rootDirectory; |
50
|
5 |
|
$this->includePaths = $includePaths; |
51
|
5 |
|
$this->excludePaths = $excludePaths; |
52
|
5 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns an enumerator for files |
56
|
|
|
* |
57
|
|
|
* @return \CallbackFilterIterator|\RecursiveIteratorIterator|\SplFileInfo[] |
58
|
|
|
*/ |
59
|
5 |
|
public function enumerate() |
60
|
|
|
{ |
61
|
5 |
|
$iterator = new \RecursiveIteratorIterator( |
62
|
5 |
|
new \RecursiveDirectoryIterator( |
63
|
5 |
|
$this->rootDirectory, |
64
|
5 |
|
\FilesystemIterator::SKIP_DOTS |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
|
68
|
5 |
|
$callback = $this->getFilter(); |
69
|
5 |
|
$iterator = new \CallbackFilterIterator($iterator, $callback); |
70
|
|
|
|
71
|
5 |
|
return $iterator; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns a filter callback for enumerating files |
76
|
|
|
* |
77
|
|
|
* @return \Closure |
78
|
|
|
*/ |
79
|
5 |
|
public function getFilter() |
80
|
|
|
{ |
81
|
5 |
|
$rootDirectory = $this->rootDirectory; |
82
|
5 |
|
$includePaths = $this->includePaths; |
83
|
5 |
|
$excludePaths = $this->excludePaths; |
84
|
|
|
|
85
|
5 |
|
return function (\SplFileInfo $file) use ($rootDirectory, $includePaths, $excludePaths) { |
86
|
|
|
|
87
|
5 |
|
if ($file->getExtension() !== 'php') { |
88
|
|
|
return false; |
89
|
|
|
}; |
90
|
|
|
|
91
|
5 |
|
$fullPath = $this->getFileFullPath($file); |
92
|
5 |
|
$dir = dirname($fullPath); |
93
|
|
|
// Do not touch files that not under rootDirectory |
94
|
5 |
|
if (strpos($fullPath, $rootDirectory) !== 0) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
if (!empty($includePaths)) { |
99
|
1 |
|
$found = false; |
100
|
1 |
|
foreach ($includePaths as $includePattern) { |
101
|
1 |
|
if (fnmatch($includePattern, $dir)) { |
102
|
1 |
|
$found = true; |
103
|
1 |
|
break; |
104
|
|
|
} |
105
|
|
|
} |
106
|
1 |
|
if (!$found) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
foreach ($excludePaths as $excludePattern) { |
112
|
3 |
|
if (fnmatch($excludePattern, $dir)) { |
113
|
3 |
|
return false; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
return true; |
118
|
5 |
|
}; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Return the real path of the given file |
123
|
|
|
* |
124
|
|
|
* This is used for testing purpose with virtual file system. |
125
|
|
|
* In a vfs the 'realPath' methode will always return false. |
126
|
|
|
* So we have a chance to mock this single function to return different path. |
127
|
|
|
* |
128
|
|
|
* @param \SplFileInfo $file |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
protected function getFileFullPath(\SplFileInfo $file) |
133
|
|
|
{ |
134
|
|
|
return $file->getRealPath(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|