1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magister\Services\Filesystem; |
4
|
|
|
|
5
|
|
|
use Magister\Services\Contracts\Filesystem\FileNotFoundException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Filesystem. |
9
|
|
|
*/ |
10
|
|
|
class Filesystem |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Determine if a file exists. |
14
|
|
|
* |
15
|
|
|
* @param string $path |
16
|
|
|
* |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
|
|
public function exists($path) |
20
|
|
|
{ |
21
|
|
|
return file_exists($path); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the contents of a file. |
26
|
|
|
* |
27
|
|
|
* @param string $path |
28
|
|
|
* |
29
|
|
|
* @throws \Magister\Services\Contracts\Filesystem\FileNotFoundException |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function get($path) |
34
|
|
|
{ |
35
|
|
|
if ($this->isFile($path)) { |
36
|
|
|
return file_get_contents($path); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
throw new FileNotFoundException(sprintf('File does not exist at path "%s".', $path)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the returned value of a file. |
44
|
|
|
* |
45
|
|
|
* @param string $path |
46
|
|
|
* |
47
|
|
|
* @throws \Magister\Services\Contracts\Filesystem\FileNotFoundException |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function getRequire($path) |
52
|
|
|
{ |
53
|
|
|
if ($this->isFile($path)) { |
54
|
|
|
return require $path; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw new FileNotFoundException(sprintf('File does not exist at path "%s".', $path)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Require the given file once. |
62
|
|
|
* |
63
|
|
|
* @param string $file |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function requireOnce($file) |
68
|
|
|
{ |
69
|
|
|
require_once $file; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Write the contents of a file. |
74
|
|
|
* |
75
|
|
|
* @param string $path |
76
|
|
|
* @param string $contents |
77
|
|
|
* @param bool $lock |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function put($path, $contents, $lock = false) |
82
|
|
|
{ |
83
|
|
|
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Prepend to a file. |
88
|
|
|
* |
89
|
|
|
* @param string $path |
90
|
|
|
* @param string $data |
91
|
|
|
* |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
|
|
public function prepend($path, $data) |
95
|
|
|
{ |
96
|
|
|
if ($this->exists($path)) { |
97
|
|
|
return $this->put($path, $data.$this->get($path)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->put($path, $data); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Append to a file. |
105
|
|
|
* |
106
|
|
|
* @param string $path |
107
|
|
|
* @param string $data |
108
|
|
|
* |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
|
|
public function append($path, $data) |
112
|
|
|
{ |
113
|
|
|
return file_put_contents($path, $data, FILE_APPEND); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Delete the file at a given path. |
118
|
|
|
* |
119
|
|
|
* @param string|array $paths |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function delete($paths) |
124
|
|
|
{ |
125
|
|
|
$paths = is_array($paths) ? $paths : func_get_args(); |
126
|
|
|
|
127
|
|
|
$success = true; |
128
|
|
|
|
129
|
|
|
foreach ($paths as $path) { |
130
|
|
|
if (!@unlink($path)) { |
131
|
|
|
$success = false; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $success; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Move a file to a new location. |
140
|
|
|
* |
141
|
|
|
* @param string $path |
142
|
|
|
* @param string $target |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function move($path, $target) |
147
|
|
|
{ |
148
|
|
|
return rename($path, $target); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Copy a file to a new location. |
153
|
|
|
* |
154
|
|
|
* @param string $path |
155
|
|
|
* @param string $target |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function copy($path, $target) |
160
|
|
|
{ |
161
|
|
|
return copy($path, $target); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Determine if the given path is a directory. |
166
|
|
|
* |
167
|
|
|
* @param string $directory |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function isDirectory($directory) |
172
|
|
|
{ |
173
|
|
|
return is_dir($directory); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Determine if the given path is writable. |
178
|
|
|
* |
179
|
|
|
* @param string $path |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
public function isWritable($path) |
184
|
|
|
{ |
185
|
|
|
return is_writable($path); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Determine if the given path is a file. |
190
|
|
|
* |
191
|
|
|
* @param string $file |
192
|
|
|
* |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
public function isFile($file) |
196
|
|
|
{ |
197
|
|
|
return is_file($file); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|