1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of PHPUnit Generator. |
5
|
|
|
* |
6
|
|
|
* (c) Paul Thébaud <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PHPUnitGenerator\FileSystem; |
13
|
|
|
|
14
|
|
|
use PHPUnitGenerator\Exception\DirNotFoundException; |
15
|
|
|
use PHPUnitGenerator\Exception\FileNotFoundException; |
16
|
|
|
use PHPUnitGenerator\FileSystem\FileSystemInterface\FileSystemInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class LocalFileSystem |
20
|
|
|
* |
21
|
|
|
* Allow to use the local storage to manage files and directories |
22
|
|
|
* |
23
|
|
|
* @package PHPUnitGenerator\FileSystem |
24
|
|
|
*/ |
25
|
|
|
class LocalFileSystem implements FileSystemInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function getFiles(string $dir): array |
31
|
|
|
{ |
32
|
|
|
$fileList = []; |
33
|
|
|
|
34
|
|
|
foreach ($this->getFilesIterator($dir) as $file) { |
35
|
|
|
/** |
36
|
|
|
* @var \SplFileInfo $file |
37
|
|
|
*/ |
38
|
|
|
if ($this->fileExists($file->__toString())) { |
|
|
|
|
39
|
|
|
$fileList[] = $file->__toString(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $fileList; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function filterFiles(array $files, string $includeRegex = null, string $excludeRegex = null): array |
50
|
|
|
{ |
51
|
|
|
foreach ($files as $key => $file) { |
52
|
|
|
if (($includeRegex !== null && preg_match($includeRegex, $file) <= 0) |
53
|
|
|
|| ($excludeRegex !== null && preg_match($excludeRegex, $file) > 0) |
54
|
|
|
) { |
55
|
|
|
unset($files[$key]); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $files; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function mkDir(string $dir) |
66
|
|
|
{ |
67
|
|
|
if (! $this->dirExists($dir)) { |
68
|
|
|
mkdir($dir, 0755, true); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function pathExists(string $path): bool |
76
|
|
|
{ |
77
|
|
|
return file_exists($path); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function fileExists(string $file): bool |
84
|
|
|
{ |
85
|
|
|
return $this->pathExists($file) && is_file($file); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function dirExists(string $file): bool |
92
|
|
|
{ |
93
|
|
|
return $this->pathExists($file) && is_dir($file); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function write(string $file, string $content) |
100
|
|
|
{ |
101
|
|
|
file_put_contents($file, $content); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function read(string $file): string |
108
|
|
|
{ |
109
|
|
|
if (! $this->fileExists($file)) { |
110
|
|
|
throw new FileNotFoundException(sprintf(FileNotFoundException::TEXT, $file)); |
111
|
|
|
} |
112
|
|
|
return file_get_contents($file); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the file iterator for a directory |
117
|
|
|
* |
118
|
|
|
* @param string $dir |
119
|
|
|
* |
120
|
|
|
* @return \IteratorIterator |
121
|
|
|
* |
122
|
|
|
* @throws DirNotFoundException If the directory does not exists |
123
|
|
|
*/ |
124
|
|
|
protected function getFilesIterator(string $dir) |
125
|
|
|
{ |
126
|
|
|
if (! $this->dirExists($dir)) { |
127
|
|
|
throw new DirNotFoundException(sprintf(DirNotFoundException::TEXT, $dir)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$directory = new \RecursiveDirectoryIterator($dir); |
131
|
|
|
$iterator = new \RecursiveIteratorIterator($directory); |
132
|
|
|
return new \IteratorIterator($iterator); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|