-
Notifications
You must be signed in to change notification settings - Fork 672
/
Copy pathFileProvider.php
198 lines (162 loc) · 5.44 KB
/
FileProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Psalm\Internal\Provider;
use FilesystemIterator;
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
use RecursiveIterator;
use RecursiveIteratorIterator;
use UnexpectedValueException;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function filemtime;
use function in_array;
use function is_dir;
use const DIRECTORY_SEPARATOR;
/**
* @internal
*/
class FileProvider
{
/**
* @var array<string, array{version: ?int, content: string}>
*/
protected array $temp_files = [];
/**
* @var array<string, string>
*/
protected static array $open_files = [];
/**
* @var array<string, string>
*/
protected array $open_files_paths = [];
public function getContents(string $file_path, bool $go_to_source = false): string
{
if (!$go_to_source && isset($this->temp_files[$file_path])) {
return $this->temp_files[$file_path]['content'];
}
if (isset(self::$open_files[$file_path])) {
return self::$open_files[$file_path];
}
if (!file_exists($file_path)) {
throw new UnexpectedValueException('File ' . $file_path . ' should exist to get contents');
}
if (is_dir($file_path)) {
throw new UnexpectedValueException('File ' . $file_path . ' is a directory');
}
$file_contents = (string) file_get_contents($file_path);
self::$open_files[$file_path] = $file_contents;
return $file_contents;
}
public function setContents(string $file_path, string $file_contents): void
{
if (isset(self::$open_files[$file_path])) {
self::$open_files[$file_path] = $file_contents;
}
if (isset($this->temp_files[$file_path])) {
$this->temp_files[$file_path] = [
'version'=> null,
'content' => $file_contents,
];
}
file_put_contents($file_path, $file_contents);
}
public function setOpenContents(string $file_path, ?string $file_contents = null): void
{
if (isset(self::$open_files[$file_path])) {
self::$open_files[$file_path] = $file_contents ?? $this->getContents($file_path, true);
}
}
public function getModifiedTime(string $file_path): int
{
if (!file_exists($file_path)) {
throw new UnexpectedValueException('File should exist to get modified time');
}
return (int) filemtime($file_path);
}
public function addTemporaryFileChanges(string $file_path, string $new_content, ?int $version = null): void
{
if (isset($this->temp_files[$file_path]) &&
$version !== null &&
$this->temp_files[$file_path]['version'] !== null &&
$version < $this->temp_files[$file_path]['version']
) {
return;
}
$this->temp_files[$file_path] = [
'version' => $version,
'content' => $new_content,
];
}
public function removeTemporaryFileChanges(string $file_path): void
{
unset($this->temp_files[$file_path]);
}
public function getOpenFilesPath(): array
{
return $this->open_files_paths;
}
public function openFile(string $file_path): void
{
self::$open_files[$file_path] = $this->getContents($file_path, true);
$this->open_files_paths[$file_path] = $file_path;
}
public function isOpen(string $file_path): bool
{
return isset($this->temp_files[$file_path]) || isset(self::$open_files[$file_path]);
}
public function closeFile(string $file_path): void
{
unset(
$this->temp_files[$file_path],
self::$open_files[$file_path],
$this->open_files_paths[$file_path],
);
}
public function fileExists(string $file_path): bool
{
return file_exists($file_path);
}
public function isDirectory(string $file_path): bool
{
return is_dir($file_path);
}
/**
* @param array<string> $file_extensions
* @param null|callable(string):bool $filter
* @return list<string>
*/
public function getFilesInDir(string $dir_path, array $file_extensions, ?callable $filter = null): array
{
$file_paths = [];
$iterator = new RecursiveDirectoryIterator(
$dir_path,
FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS,
);
if ($filter !== null) {
$iterator = new RecursiveCallbackFilterIterator(
$iterator,
/** @param mixed $_ */
static function (string $current, $_, RecursiveIterator $iterator) use ($filter): bool {
if ($iterator->hasChildren()) {
$path = $current . DIRECTORY_SEPARATOR;
} else {
$path = $current;
}
return $filter($path);
},
);
}
/** @var RecursiveDirectoryIterator */
$iterator = new RecursiveIteratorIterator($iterator);
$iterator->rewind();
while ($iterator->valid()) {
$extension = $iterator->getExtension();
if (in_array($extension, $file_extensions, true)) {
$file_paths[] = (string)$iterator->getRealPath();
}
$iterator->next();
}
return $file_paths;
}
}