1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace uuf6429\ElderBrother\Change; |
4
|
|
|
|
5
|
|
|
class FileList |
6
|
|
|
{ |
7
|
|
|
/** @var string */ |
8
|
|
|
protected $cacheKey; |
9
|
|
|
|
10
|
|
|
/** @var callable */ |
11
|
|
|
protected $source; |
12
|
|
|
|
13
|
|
|
/** @var array */ |
14
|
|
|
protected static $cache; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $cacheKey |
18
|
|
|
* @param callable $source |
19
|
|
|
*/ |
20
|
11 |
|
public function __construct($cacheKey, callable $source) |
21
|
|
|
{ |
22
|
11 |
|
$this->cacheKey = $cacheKey; |
23
|
11 |
|
$this->source = $source; |
24
|
11 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Filter by file path matching a regular expression. |
28
|
|
|
* |
29
|
|
|
* @param string $regex |
30
|
|
|
* |
31
|
|
|
* @return static |
32
|
|
|
*/ |
33
|
1 |
|
public function name($regex) |
34
|
|
|
{ |
35
|
1 |
|
$source = $this->source; |
36
|
|
|
|
37
|
1 |
|
return new self( |
38
|
1 |
|
$this->cacheKey . '->' . __FUNCTION__ . '(' . $regex . ')', |
39
|
|
|
function () use ($source, $regex) { |
40
|
1 |
|
return array_filter( |
41
|
1 |
|
$source(), |
42
|
|
|
function ($file) use ($regex) { |
43
|
1 |
|
return preg_match($regex, $file); |
44
|
1 |
|
} |
45
|
|
|
); |
46
|
1 |
|
} |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Filter by file path starting with a string. |
52
|
|
|
* |
53
|
|
|
* @param string $string |
54
|
|
|
* |
55
|
|
|
* @return static |
56
|
|
|
*/ |
57
|
1 |
|
public function startingWith($string) |
58
|
|
|
{ |
59
|
1 |
|
$source = $this->source; |
60
|
|
|
|
61
|
1 |
|
return new self( |
62
|
1 |
|
$this->cacheKey . '->' . __FUNCTION__ . '(' . $string . ')', |
63
|
|
|
function () use ($source, $string) { |
64
|
1 |
|
return array_filter( |
65
|
1 |
|
$source(), |
66
|
|
|
function ($file) use ($string) { |
67
|
1 |
|
$fileLen = strlen($file); |
68
|
1 |
|
$strnLen = strlen($string); |
69
|
|
|
|
70
|
1 |
|
return $strnLen < $fileLen |
71
|
1 |
|
&& substr_compare($file, $string, 0, $strnLen) === 0; |
72
|
1 |
|
} |
73
|
|
|
); |
74
|
1 |
|
} |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Filter by file path ending with a string. |
80
|
|
|
* |
81
|
|
|
* @param string $string |
82
|
|
|
* |
83
|
|
|
* @return static |
84
|
|
|
*/ |
85
|
1 |
|
public function endingWith($string) |
86
|
|
|
{ |
87
|
1 |
|
$source = $this->source; |
88
|
|
|
|
89
|
1 |
|
return new self( |
90
|
1 |
|
$this->cacheKey . '->' . __FUNCTION__ . '(' . $string . ')', |
91
|
|
|
function () use ($source, $string) { |
92
|
1 |
|
return array_filter( |
93
|
1 |
|
$source(), |
94
|
1 |
|
function ($file) use ($string) { |
95
|
1 |
|
$fileLen = strlen($file); |
96
|
1 |
|
$strnLen = strlen($string); |
97
|
|
|
|
98
|
1 |
|
return $fileLen > $strnLen |
99
|
1 |
|
&& substr_compare($file, $string, -$strnLen) === 0; |
100
|
1 |
|
} |
101
|
|
|
); |
102
|
1 |
|
} |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns array of file paths. |
108
|
|
|
* |
109
|
|
|
* @return string[] |
110
|
|
|
*/ |
111
|
11 |
|
public function toArray() |
112
|
|
|
{ |
113
|
11 |
|
if (!isset(self::$cache[$this->cacheKey])) { |
114
|
11 |
|
$source = $this->source; |
115
|
11 |
|
self::$cache[$this->cacheKey] = $source(); |
116
|
|
|
} |
117
|
|
|
|
118
|
11 |
|
return self::$cache[$this->cacheKey]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|