/
FilesystemAdapter.php
119 lines (90 loc) · 3.09 KB
/
FilesystemAdapter.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
<?php
namespace Statamic\Filesystem;
use Illuminate\Filesystem\Filesystem;
use Statamic\Facades\Path;
use Statamic\Support\Str;
use Symfony\Component\Finder\Finder;
class FilesystemAdapter extends AbstractAdapter
{
protected $root;
protected $filesystem;
protected $withAbsolutePaths = false;
public function __construct(Filesystem $filesystem, $root)
{
$this->filesystem = $filesystem;
$this->setRootDirectory($root);
}
public function setRootDirectory($directory)
{
$this->root = rtrim($directory, '/\\');
return $this;
}
public function normalizePath($path)
{
if (is_null($path)) {
$path = '/';
}
// If given an absolute path, just tidy it (to adjust the slashes) and return it.
// Except for a single slash, because that means "the root of the configured
// filesystem", and not "the root of this entire computer".
if ($path !== '/' && Path::isAbsolute($path)) {
return Path::tidy($path);
}
$path = Path::tidy($this->root.'/'.$path);
return $path;
}
public function isWithinRoot($path)
{
$path = $this->normalizePath($path);
return Str::startsWith($path, Path::tidy($this->root));
}
protected function relativePath($path)
{
$root = Path::tidy($this->root);
$path = Path::tidy(Str::removeLeft($path, $root));
return ltrim($path, '/');
}
public function isDirectory($path)
{
return $this->filesystem->isDirectory($this->normalizePath($path));
}
public function getFiles($path, $recursive = false)
{
$method = $recursive ? 'allFiles' : 'files';
if (! $this->exists($path)) {
return $this->collection();
}
$files = $this->filesystem->$method($this->normalizePath($path), true);
$inRoot = $this->isWithinRoot($path);
return $this->collection($files)->map(function ($file) use ($inRoot) {
$path = $file->getPathname();
return $inRoot && ! $this->withAbsolutePaths
? $this->relativePath($path)
: $this->normalizePath($path);
});
}
public function getFolders($path, $recursive = false)
{
$finder = Finder::create()
->in($this->normalizePath($path))
->depth($recursive ? '>=0' : 0)
->directories();
return collect($finder)->map(function ($file) {
return $this->relativePath($file->getPathname());
})->values();
}
public function copyDirectory($src, $dest, $overwrite = false)
{
// todo: implement the overwrite argument
return $this->filesystem->copyDirectory($this->normalizePath($src), $this->normalizePath($dest));
}
public function moveDirectory($src, $dest, $overwrite = false)
{
return $this->filesystem->moveDirectory($this->normalizePath($src), $this->normalizePath($dest), $overwrite);
}
public function withAbsolutePaths()
{
$this->withAbsolutePaths = true;
return $this;
}
}