1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acacha\AdminLTETemplateLaravel\Filesystem; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Filesystem. |
7
|
|
|
* |
8
|
|
|
* @package Acacha\Llum\Filesystem |
9
|
|
|
*/ |
10
|
|
|
class Filesystem |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Root directory |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $root; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param null|string $root |
22
|
|
|
*/ |
23
|
|
|
public function __construct($root = '/') |
24
|
|
|
{ |
25
|
|
|
$this->root = $root; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* Overwrite file with provided content. |
31
|
|
|
* |
32
|
|
|
* @param $file |
33
|
|
|
* @param $content |
34
|
|
|
*/ |
35
|
|
|
public function overwrite($file, $content) |
36
|
|
|
{ |
37
|
|
|
$this->put($this->getPath($file), $content); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create file with provided content if not exists. |
42
|
|
|
* |
43
|
|
|
* @param $file |
44
|
|
|
* @param $content |
45
|
|
|
* @throws FileAlreadyExists |
46
|
|
|
*/ |
47
|
|
|
public function make($file, $content) |
48
|
|
|
{ |
49
|
|
|
if ($this->exists($this->getPath($file))) { |
50
|
|
|
throw new FileAlreadyExists; |
51
|
|
|
} |
52
|
|
|
$this->put($file, $content); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get file contents. |
57
|
|
|
* @param $file |
58
|
|
|
* @return string |
59
|
|
|
* @throws FileDoesNotExists |
60
|
|
|
*/ |
61
|
|
|
public function get($file) |
62
|
|
|
{ |
63
|
|
|
$path = $this->getPath($file); |
64
|
|
|
|
65
|
|
|
if (! file_exists($path)) { |
66
|
|
|
throw new FileDoesNotExists; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return file_get_contents($path); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Put content in file. |
74
|
|
|
* |
75
|
|
|
* @param $file |
76
|
|
|
* @param $content |
77
|
|
|
* @param $flag |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
|
|
protected function put($file, $content, $flag = null) |
81
|
|
|
{ |
82
|
|
|
return file_put_contents($this->getPath($file), $content, $flag); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Does the given file exist? |
87
|
|
|
* |
88
|
|
|
* @param $file |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function exists($file) |
92
|
|
|
{ |
93
|
|
|
return file_exists($this->getPath($file)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Build the path to the file. |
98
|
|
|
* |
99
|
|
|
* @param $file |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected function getPath($file) |
103
|
|
|
{ |
104
|
|
|
return $this->root . '/' . $file; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Append to a file |
109
|
|
|
* |
110
|
|
|
* @param $file |
111
|
|
|
* @param $body |
112
|
|
|
* @return int |
113
|
|
|
*/ |
114
|
|
|
public function append($file, $body) |
115
|
|
|
{ |
116
|
|
|
return $this->put($file, $body, FILE_APPEND); |
117
|
|
|
} |
118
|
|
|
/** |
119
|
|
|
* Delete a file |
120
|
|
|
* |
121
|
|
|
* @param $file |
122
|
|
|
*/ |
123
|
|
|
public function delete($file) |
124
|
|
|
{ |
125
|
|
|
unlink($this->getPath($file)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|