1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Oliverde8\Component\PhpEtl\Model\File; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
8
|
|
|
|
9
|
|
|
class LocalFileSystem implements FileSystemInterface |
10
|
|
|
{ |
11
|
|
|
protected string $rootPath; |
12
|
|
|
|
13
|
|
|
protected Filesystem $filesystem; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param $rootPath |
17
|
|
|
*/ |
18
|
|
|
public function __construct($rootPath = null) |
19
|
|
|
{ |
20
|
|
|
if (!$rootPath) { |
21
|
|
|
$rootPath = getcwd(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$this->rootPath = $rootPath; |
25
|
|
|
$this->filesystem = new Filesystem(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getRootPath(): string |
32
|
|
|
{ |
33
|
|
|
return $this->rootPath; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function fileExists(string $path): bool |
37
|
|
|
{ |
38
|
|
|
return $this->filesystem->exists($this->rootPath . "/" . $path); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function write(string $path, string $contents, array $config = []): void |
42
|
|
|
{ |
43
|
|
|
$this->filesystem->dumpFile($this->rootPath . "/" . $path, $contents); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function writeStream(string $path, $contents, array $config = []): void |
47
|
|
|
{ |
48
|
|
|
$this->filesystem->dumpFile($this->rootPath . "/" . $path, $contents); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function read(string $path): string |
52
|
|
|
{ |
53
|
|
|
return file_get_contents($this->rootPath . "/" . $path); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function readStream(string $path) |
57
|
|
|
{ |
58
|
|
|
return fopen($this->rootPath . "/" . $path, 'r'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function delete(string $path): void |
62
|
|
|
{ |
63
|
|
|
$this->filesystem->remove($this->rootPath . "/" . $path); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function deleteDirectory(string $path): void |
67
|
|
|
{ |
68
|
|
|
$this->filesystem->remove($this->rootPath . "/" . $path); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function createDirectory(string $path, array $config = []): void |
72
|
|
|
{ |
73
|
|
|
$this->filesystem->mkdir($this->rootPath . "/" . $path); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function listContents(string $path): array |
77
|
|
|
{ |
78
|
|
|
$files = scandir($this->rootPath . "/" .$path); |
79
|
|
|
return $files ?: []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function move(string $source, string $destination, array $config = []) |
83
|
|
|
{ |
84
|
|
|
$this->filesystem->rename($this->rootPath . "/" . $source, $this->rootPath . "/" . $destination); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function copy(string $source, string $destination, array $config = []) |
88
|
|
|
{ |
89
|
|
|
$this->filesystem->copy($this->rootPath . "/" . $source, $this->rootPath . "/" . $destination); |
90
|
|
|
} |
91
|
|
|
} |