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