|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Snapshots; |
|
4
|
|
|
|
|
5
|
|
|
class Filesystem |
|
6
|
|
|
{ |
|
7
|
|
|
/** @var string */ |
|
8
|
|
|
private $basePath; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct(string $basePath) |
|
11
|
|
|
{ |
|
12
|
|
|
$this->basePath = $basePath; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public static function inDirectory(string $path): self |
|
16
|
|
|
{ |
|
17
|
|
|
return new self($path); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function path(string $filename): string |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->basePath.DIRECTORY_SEPARATOR.$filename; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function has(string $filename): bool |
|
26
|
|
|
{ |
|
27
|
|
|
return file_exists($this->path($filename)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get all file names in this directory that have the same name |
|
32
|
|
|
* as $fileName, but have a different file extension. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $fileName |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getNamesWithDifferentExtension(string $fileName) |
|
39
|
|
|
{ |
|
40
|
|
|
if (! file_exists($this->basePath)) { |
|
41
|
|
|
return []; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$extension = pathinfo($fileName, PATHINFO_EXTENSION); |
|
45
|
|
|
|
|
46
|
|
|
$baseName = substr($fileName, 0, strlen($fileName) - strlen($extension) - 1); |
|
47
|
|
|
|
|
48
|
|
|
$allNames = scandir($this->basePath); |
|
49
|
|
|
|
|
50
|
|
|
$namesWithDifferentExtension = array_filter($allNames, function ($existingName) use ($baseName, $extension) { |
|
51
|
|
|
$existingExtension = pathinfo($existingName, PATHINFO_EXTENSION); |
|
52
|
|
|
|
|
53
|
|
|
$existingBaseName = substr($existingName, 0, strlen($existingName) - strlen($existingExtension) - 1); |
|
54
|
|
|
|
|
55
|
|
|
return $existingBaseName === $baseName && $existingExtension !== $extension; |
|
56
|
|
|
}); |
|
57
|
|
|
|
|
58
|
|
|
return array_values($namesWithDifferentExtension); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function read(string $filename): string |
|
62
|
|
|
{ |
|
63
|
|
|
return file_get_contents($this->path($filename)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function put(string $filename, string $contents) |
|
67
|
|
|
{ |
|
68
|
|
|
if (! file_exists($this->basePath)) { |
|
69
|
|
|
mkdir($this->basePath); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
file_put_contents($this->path($filename), $contents); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function delete(string $fileName) |
|
76
|
|
|
{ |
|
77
|
|
|
return unlink($this->path($fileName)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function copy(string $filePath, string $fileName) |
|
81
|
|
|
{ |
|
82
|
|
|
if (! file_exists($this->basePath)) { |
|
83
|
|
|
mkdir($this->basePath); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
copy($filePath, $this->path($fileName)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function fileEquals(string $filePath, string $fileName) |
|
90
|
|
|
{ |
|
91
|
|
|
return sha1_file($filePath) === sha1_file($this->path($fileName)); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|