-
Notifications
You must be signed in to change notification settings - Fork 430
/
Filesystem.php
173 lines (140 loc) · 4.51 KB
/
Filesystem.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
<?php
declare(strict_types=1);
namespace GrumPHP\Util;
use SplFileInfo;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
class Filesystem extends SymfonyFilesystem
{
public function readFromFileInfo(SplFileInfo $file): string
{
$handle = $file->openFile('r');
$content = '';
while (!$handle->eof()) {
$content .= $handle->fgets();
}
return $content;
}
public function readPath(string $path): string
{
return $this->readFromFileInfo(new SplFileInfo($path));
}
public function isFile(string $path): bool
{
return \is_file($path);
}
public function realpath(string $path): string
{
$realPath = realpath($path);
if ($realPath === false) {
throw new FileNotFoundException(sprintf('Path "%s" does not exist.', $path));
}
return $realPath;
}
public function makePathAbsolute(string $path, string $basePath): string
{
if ($this->isAbsolutePath($path)) {
return $path;
}
return $this->buildPath($basePath, $path);
}
public function isPathInFolder(string $path, string $folder): bool
{
$realPath = $this->realpath($path);
$realDirectory = $this->isFile($realPath) ? dirname($realPath) : $realPath;
return strpos($realDirectory, $this->realpath($folder)) === 0;
}
public function buildPath(string $baseDir, string $path): string
{
return $baseDir.DIRECTORY_SEPARATOR.$path;
}
/**
* @param non-empty-list<?string> $paths
*/
public function guessPath(array $paths): string
{
$paths = array_filter($paths);
foreach ($paths as $path) {
if ($this->exists($path) && is_dir($path)) {
return $path;
}
}
return (string) current($paths);
}
/**
* @param non-empty-list<?string> $paths
* @param non-empty-list<?string> $fileNames
*/
public function guessFile(array $paths, array $fileNames): string
{
$paths = array_filter($paths);
$fileNames = array_filter($fileNames);
foreach ($paths as $path) {
if (!$this->exists($path)) {
continue;
}
if (is_file($path)) {
return $path;
}
foreach ($fileNames as $fileName) {
$filePath = $this->buildPath($path, $fileName);
if ($this->exists($filePath)) {
return $filePath;
}
}
}
$firstPath = (string) current($paths);
$firstName = (string) current($fileNames);
if (preg_match('#'.preg_quote($firstName, '#').'$#', $firstPath)) {
return $firstPath;
}
return $this->buildPath($firstPath, $firstName);
}
public function ensureUnixPath(string $path): string
{
// Unix systems know best ...
if (DIRECTORY_SEPARATOR === '/') {
return $path;
}
// Convert backslashes, remove duplicate slashes and transform drive letter to uppercase:
$path = str_replace('\\', '/', $path);
$path = preg_replace('|(?<=.)/+|', '/', $path);
if (':' === ($path[1] ?? '')) {
$path = ucfirst($path);
}
return $path;
}
public function ensureValidSlashes(string $path): string
{
// Unix systems know best ...
if (DIRECTORY_SEPARATOR === '/') {
return $path;
}
// Convert / slash to \ on windows:
$path = str_replace('/', '\\', $path);
$path = preg_replace('|(?<=.)\\\\+|', '\\', $path);
if (':' === ($path[1] ?? '')) {
$path = ucfirst($path);
}
return $path;
}
/**
* This method can be used to create a backup of current file.
* It used md5 to hash the content.
* So multiple backups of a file can exist (depending on the content)
*/
public function backupFile(string $fileName, ?string $hashOfNewContent = null): void
{
if (!$this->isFile($fileName)) {
return;
}
if (!$hash = md5_file($fileName)) {
return;
}
// Skip backup if the new file has the same content as the old one.
if ($hashOfNewContent && $hashOfNewContent === $hash) {
return;
}
$this->copy($fileName, sprintf('%s.%s.backup', $fileName, $hash));
}
}