-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystemDistribution.php
More file actions
115 lines (99 loc) · 3.65 KB
/
FilesystemDistribution.php
File metadata and controls
115 lines (99 loc) · 3.65 KB
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
<?php
declare(strict_types=1);
namespace Stasis\Generator\Distribution;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Stasis\Exception\LogicException;
use Stasis\Exception\RuntimeException;
class FilesystemDistribution implements LocalDistributionInterface, SymlinkDistributionInterface
{
private readonly string $basePath;
private Filesystem $filesystem;
public function __construct(string $distPath, Filesystem $filesystem = new Filesystem())
{
if (!Path::isAbsolute($distPath)) {
throw new LogicException(sprintf('Provided dist path "%s" is not absolute.', $distPath));
}
$this->basePath = rtrim(Path::canonicalize($distPath), '/');
$this->filesystem = $filesystem;
}
#[\Override]
public function path(): string
{
return $this->basePath;
}
#[\Override]
public function clear(): void
{
try {
if (!$this->filesystem->exists($this->basePath) || !is_dir($this->basePath)) {
return;
}
/** @var \FilesystemIterator<string> $iterator */
$iterator = new \FilesystemIterator($this->basePath, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS);
foreach ($iterator as $path) {
$this->filesystem->remove($path);
}
} catch (\Throwable $exception) {
throw new RuntimeException(
message: sprintf('Error clearing distribution "%s".', $this->basePath),
previous: $exception,
);
}
}
#[\Override]
public function write(string $path, $content): void
{
$fullPath = $this->getFullPath($path);
try {
$this->filesystem->dumpFile($fullPath, $content);
} catch (\Throwable $exception) {
throw new RuntimeException(
message: sprintf('Error writing to distribution path "%s".', $fullPath),
previous: $exception,
);
}
}
#[\Override]
public function copy(string $sourcePath, string $destinationPath): void
{
if (!file_exists($sourcePath)) {
throw new LogicException(sprintf('Source path "%s" does not exist', $sourcePath));
}
$isDir = is_dir($sourcePath);
$fullPath = $this->getFullPath($destinationPath);
try {
if ($isDir) {
$this->filesystem->mirror($sourcePath, $fullPath);
} else {
$this->filesystem->copy($sourcePath, $fullPath, true);
}
} catch (\Throwable $exception) {
$type = $isDir ? 'directory' : 'file';
throw new RuntimeException(
message: sprintf('Error copying %s "%s" to distribution "%s".', $type, $sourcePath, $fullPath),
previous: $exception,
);
}
}
#[\Override]
public function link(string $sourcePath, string $destinationPath): void
{
if (!file_exists($sourcePath)) {
throw new LogicException(sprintf('Source path "%s" does not exist', $sourcePath));
}
$fullPath = $this->getFullPath($destinationPath);
try {
$this->filesystem->symlink($sourcePath, $fullPath, false);
} catch (\Throwable $exception) {
throw new RuntimeException(
message: sprintf('Error creating symlink "%s" to distribution "%s".', $sourcePath, $fullPath),
previous: $exception,
);
}
}
private function getFullPath(string $path): string
{
return sprintf('%s/%s', $this->basePath, ltrim($path, '/'));
}
}