1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2017 |
6
|
|
|
* @package MW |
7
|
|
|
* @subpackage Filesystem |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\MW\Filesystem; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Implementation of Laravel file system adapter |
16
|
|
|
* |
17
|
|
|
* @package MW |
18
|
|
|
* @subpackage Filesystem |
19
|
|
|
*/ |
20
|
|
|
class Laravel implements Iface, DirIface, MetaIface |
21
|
|
|
{ |
22
|
|
|
private $fs; |
23
|
|
|
private $tempdir; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Initializes the object |
28
|
|
|
* |
29
|
|
|
* @param \Illuminate\Contracts\Filesystem\Filesystem $fs Laravel file system object |
30
|
|
|
* @param string $tempdir Directory for storing temporary files |
31
|
|
|
*/ |
32
|
|
|
public function __construct( \Illuminate\Contracts\Filesystem\Filesystem $fs, $tempdir ) |
33
|
|
|
{ |
34
|
|
|
$this->fs = $fs; |
35
|
|
|
$this->tempdir = $tempdir; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Tests if the given path is a directory |
41
|
|
|
* |
42
|
|
|
* @param string $path Path to the file or directory |
43
|
|
|
* @return boolean True if directory, false if not |
44
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
45
|
|
|
*/ |
46
|
|
|
public function isdir( $path ) |
47
|
|
|
{ |
48
|
|
|
return in_array( basename( $path ), $this->fs->directories( dirname( $path ) ) ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Creates a new directory for the given path |
54
|
|
|
* |
55
|
|
|
* @param string $path Path to the directory |
56
|
|
|
* @return void |
57
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
58
|
|
|
*/ |
59
|
|
|
public function mkdir( $path ) |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
$this->fs->makeDirectory( $path ); |
63
|
|
|
} catch( \Exception $e ) { |
64
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Deletes the directory for the given path |
71
|
|
|
* |
72
|
|
|
* @param string $path Path to the directory |
73
|
|
|
* @return void |
74
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
75
|
|
|
*/ |
76
|
|
|
public function rmdir( $path ) |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
|
|
$this->fs->deleteDirectory( $path ); |
80
|
|
|
} catch( \Exception $e ) { |
81
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns an iterator over the entries in the given path |
88
|
|
|
* |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
* |
91
|
|
|
* @param string $path Path to the filesystem or directory |
92
|
|
|
* @return \Iterator|array Iterator over the entries or array with entries |
93
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
94
|
|
|
*/ |
95
|
|
|
public function scan( $path = null ) |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
|
|
return array_merge( $this->fs->directories( $path ), $this->fs->files( $path ) ); |
99
|
|
|
} catch( \Exception $e ) { |
100
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns the file size |
107
|
|
|
* |
108
|
|
|
* @param string $path Path to the file |
109
|
|
|
* @return integer Size in bytes |
110
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
111
|
|
|
*/ |
112
|
|
|
public function size( $path ) |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
|
|
return $this->fs->size( $path ); |
116
|
|
|
} catch( \Exception $e ) { |
117
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the Unix time stamp for the file |
124
|
|
|
* |
125
|
|
|
* @param string $path Path to the file |
126
|
|
|
* @return integer Unix time stamp in seconds |
127
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
128
|
|
|
*/ |
129
|
|
|
public function time( $path ) |
130
|
|
|
{ |
131
|
|
|
try { |
132
|
|
|
return $this->fs->lastModified( $path ); |
133
|
|
|
} catch( \Exception $e ) { |
134
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Deletes the file for the given path |
141
|
|
|
* |
142
|
|
|
* @param string $path Path to the file |
143
|
|
|
* @return void |
144
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
145
|
|
|
*/ |
146
|
|
|
public function rm( $path ) |
147
|
|
|
{ |
148
|
|
|
try { |
149
|
|
|
$this->fs->delete( $path ); |
150
|
|
|
} catch( \Exception $e ) { |
151
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Tests if a file exists at the given path |
158
|
|
|
* |
159
|
|
|
* @param string $path Path to the file |
160
|
|
|
* @return boolean True if it exists, false if not |
161
|
|
|
*/ |
162
|
|
|
public function has( $path ) |
163
|
|
|
{ |
164
|
|
|
return $this->fs->exists( $path ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the content of the file |
170
|
|
|
* |
171
|
|
|
* {@inheritDoc} |
172
|
|
|
* |
173
|
|
|
* @param string $path Path to the file |
174
|
|
|
* @return string File content |
175
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
176
|
|
|
*/ |
177
|
|
|
public function read( $path ) |
178
|
|
|
{ |
179
|
|
|
try { |
180
|
|
|
return $this->fs->get( $path ); |
181
|
|
|
} catch( \Exception $e ) { |
182
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Reads the content of the remote file and writes it to a local one |
189
|
|
|
* |
190
|
|
|
* @param string $path Path to the remote file |
191
|
|
|
* @return string Path of the local file |
192
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
193
|
|
|
*/ |
194
|
|
|
public function readf( $path ) |
195
|
|
|
{ |
196
|
|
|
if( ( $filename = tempnam( $this->tempdir, 'ai-' ) ) === false ) { |
197
|
|
|
throw new Exception( sprintf( 'Unable to create file in "%1$s"', $this->tempdir ) ); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if( @file_put_contents( $filename, $this->fs->get( $path ) ) === false ) { |
201
|
|
|
throw new Exception( sprintf( 'Couldn\'t write file "%1$s"', $filename ) ); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $filename; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns the stream descriptor for the file |
210
|
|
|
* |
211
|
|
|
* {@inheritDoc} |
212
|
|
|
* |
213
|
|
|
* @param string $path Path to the file |
214
|
|
|
* @return resource File stream descriptor |
215
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
216
|
|
|
*/ |
217
|
|
|
public function reads( $path ) |
218
|
|
|
{ |
219
|
|
|
try { |
220
|
|
|
$content = $this->fs->get( $path ); |
221
|
|
|
} catch( \Exception $e ) { |
222
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if( ( $stream = tmpfile() ) === false ) { |
226
|
|
|
throw new Exception( 'Couldn\'t create temporary file' ); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if( fwrite( $stream, $content ) === false ) { |
230
|
|
|
throw new Exception( 'Couldn\'t write to temporary file' ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if( rewind( $stream ) === false ) { |
234
|
|
|
throw new Exception( 'Couldn\'t rewind temporary file' ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $stream; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Writes the given content to the file |
243
|
|
|
* |
244
|
|
|
* {@inheritDoc} |
245
|
|
|
* |
246
|
|
|
* @param string $path Path to the file |
247
|
|
|
* @param string $content New file content |
248
|
|
|
* @return void |
249
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
250
|
|
|
*/ |
251
|
|
|
public function write( $path, $content ) |
252
|
|
|
{ |
253
|
|
|
try { |
254
|
|
|
$this->fs->put( $path, $content ); |
255
|
|
|
} catch( \Exception $e ) { |
256
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Writes the content of the local file to the remote path |
263
|
|
|
* |
264
|
|
|
* {@inheritDoc} |
265
|
|
|
* |
266
|
|
|
* @param string $path Path to the remote file |
267
|
|
|
* @param string $local Path to the local file |
268
|
|
|
* @return void |
269
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
270
|
|
|
*/ |
271
|
|
|
public function writef( $path, $local ) |
272
|
|
|
{ |
273
|
|
|
if( ( $content = @file_get_contents( $local ) ) === false ) { |
274
|
|
|
throw new Exception( sprintf( 'Couldn\'t read file "%1$s"', $local ) ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$this->write( $path, $content ); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Write the content of the stream descriptor into the remote file |
283
|
|
|
* |
284
|
|
|
* {@inheritDoc} |
285
|
|
|
* |
286
|
|
|
* @param string $path Path to the file |
287
|
|
|
* @param resource $stream File stream descriptor |
288
|
|
|
* @return void |
289
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
290
|
|
|
*/ |
291
|
|
|
public function writes( $path, $stream ) |
292
|
|
|
{ |
293
|
|
|
if( ( $content = @fread( $stream, 0x7ffffffd ) ) === false ) { |
294
|
|
|
$error = error_get_last(); |
295
|
|
|
throw new Exception( $error['message'] ); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
try { |
299
|
|
|
$this->fs->put( $path, $content ); |
300
|
|
|
} catch( \Exception $e ) { |
301
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Renames a file, moves it to a new location or both at once |
308
|
|
|
* |
309
|
|
|
* @param string $from Path to the original file |
310
|
|
|
* @param string $to Path to the new file |
311
|
|
|
* @return void |
312
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
313
|
|
|
*/ |
314
|
|
|
public function move( $from, $to ) |
315
|
|
|
{ |
316
|
|
|
try { |
317
|
|
|
$this->fs->move( $from, $to ); |
318
|
|
|
} catch( \Exception $e ) { |
319
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Copies a file to a new location |
326
|
|
|
* |
327
|
|
|
* @param string $from Path to the original file |
328
|
|
|
* @param string $to Path to the new file |
329
|
|
|
* @return void |
330
|
|
|
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs |
331
|
|
|
*/ |
332
|
|
|
public function copy( $from, $to ) |
333
|
|
|
{ |
334
|
|
|
try { |
335
|
|
|
$this->fs->copy( $from, $to ); |
336
|
|
|
} catch( \Exception $e ) { |
337
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|