1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jellyfish\FilesystemSymfony; |
4
|
|
|
|
5
|
|
|
use Jellyfish\Filesystem\FilesystemInterface; |
6
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
7
|
|
|
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem; |
8
|
|
|
|
9
|
|
|
class Filesystem implements FilesystemInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Symfony\Component\Filesystem\Filesystem |
13
|
|
|
*/ |
14
|
|
|
protected $symfonyFilesystem; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param \Symfony\Component\Filesystem\Filesystem $symfonyFilesystem |
18
|
|
|
*/ |
19
|
|
|
public function __construct(SymfonyFilesystem $symfonyFilesystem) |
20
|
|
|
{ |
21
|
|
|
$this->symfonyFilesystem = $symfonyFilesystem; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $path |
26
|
|
|
* @param int $mode |
27
|
|
|
* |
28
|
|
|
* @return \Jellyfish\Filesystem\FilesystemInterface |
29
|
|
|
*/ |
30
|
|
|
public function mkdir(string $path, int $mode = 0777): FilesystemInterface |
31
|
|
|
{ |
32
|
|
|
$this->symfonyFilesystem->mkdir($path, $mode); |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $path |
39
|
|
|
* |
40
|
|
|
* @return \Jellyfish\Filesystem\FilesystemInterface |
41
|
|
|
*/ |
42
|
|
|
public function remove(string $path): FilesystemInterface |
43
|
|
|
{ |
44
|
|
|
$this->symfonyFilesystem->remove($path); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $path |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function exists(string $path): bool |
55
|
|
|
{ |
56
|
|
|
return $this->symfonyFilesystem->exists($path); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $pathToFile |
61
|
|
|
* @param string $content |
62
|
|
|
* |
63
|
|
|
* @return \Jellyfish\Filesystem\FilesystemInterface |
64
|
|
|
*/ |
65
|
|
|
public function appendToFile(string $pathToFile, string $content): FilesystemInterface |
66
|
|
|
{ |
67
|
|
|
if (false === @\file_put_contents($pathToFile, $content, FILE_APPEND)) { |
68
|
|
|
throw new IOException(sprintf('Failed to write file "%s".', $pathToFile), 0, null, $pathToFile); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $pathToFile |
76
|
|
|
* @param string $content |
77
|
|
|
* |
78
|
|
|
* @return \Jellyfish\Filesystem\FilesystemInterface |
79
|
|
|
*/ |
80
|
|
|
public function writeToFile(string $pathToFile, string $content): FilesystemInterface |
81
|
|
|
{ |
82
|
|
|
if (false === @\file_put_contents($pathToFile, $content)) { |
83
|
|
|
throw new IOException(sprintf('Failed to write file "%s".', $pathToFile), 0, null, $pathToFile); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $pathToFile |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function readFromFile(string $pathToFile): string |
95
|
|
|
{ |
96
|
|
|
$fileContent = @\file_get_contents($pathToFile); |
97
|
|
|
|
98
|
|
|
if (false === $fileContent) { |
99
|
|
|
throw new IOException(sprintf('Failed to read file "%s".', $pathToFile), 0, null, $pathToFile); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $fileContent; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|