1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2025 |
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 \Illuminate\Contracts\Filesystem\Filesystem $fs; |
23
|
|
|
private string $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, string $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
|
|
|
* @param string|null $local Path to the local file (optional) |
198
|
|
|
* @return string Path of the local file |
199
|
|
|
* @throws \Aimeos\Base\Filesystem\Exception If an error occurs |
200
|
|
|
*/ |
201
|
|
|
public function readf( string $path, ?string $local = null ) : string |
202
|
|
|
{ |
203
|
|
|
if( $local === null && ( $local = @tempnam( $this->tempdir, 'ai-' ) ) === false ) { |
204
|
|
|
throw new Exception( sprintf( 'Unable to create file in "%1$s"', $this->tempdir ) ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if( @file_put_contents( $local, $this->fs->get( $path ) ) === false ) { |
208
|
|
|
throw new Exception( sprintf( 'Couldn\'t write file "%1$s"', $local ) ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $local; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns the stream descriptor for the file |
217
|
|
|
* |
218
|
|
|
* {@inheritDoc} |
219
|
|
|
* |
220
|
|
|
* @param string $path Path to the file |
221
|
|
|
* @return resource File stream descriptor |
222
|
|
|
* @throws \Aimeos\Base\Filesystem\Exception If an error occurs |
223
|
|
|
*/ |
224
|
|
|
public function reads( string $path ) |
225
|
|
|
{ |
226
|
|
|
try { |
227
|
|
|
$content = $this->fs->get( $path ); |
228
|
|
|
} catch( \Exception $e ) { |
229
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if( ( $stream = tmpfile() ) === false ) { |
233
|
|
|
throw new Exception( 'Couldn\'t create temporary file' ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if( fwrite( $stream, $content ) === false ) { |
|
|
|
|
237
|
|
|
throw new Exception( 'Couldn\'t write to temporary file' ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if( rewind( $stream ) === false ) { |
241
|
|
|
throw new Exception( 'Couldn\'t rewind temporary file' ); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $stream; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Writes the given content to the file |
250
|
|
|
* |
251
|
|
|
* {@inheritDoc} |
252
|
|
|
* |
253
|
|
|
* @param string $path Path to the file |
254
|
|
|
* @param string $content New file content |
255
|
|
|
* @return \Aimeos\Base\Filesystem\Iface Filesystem object for fluent interface |
256
|
|
|
* @throws \Aimeos\Base\Filesystem\Exception If an error occurs |
257
|
|
|
*/ |
258
|
|
|
public function write( string $path, string $content ) : Iface |
259
|
|
|
{ |
260
|
|
|
try { |
261
|
|
|
$this->fs->put( $path, $content ); |
262
|
|
|
} catch( \Exception $e ) { |
263
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Writes the content of the local file to the remote path |
272
|
|
|
* |
273
|
|
|
* {@inheritDoc} |
274
|
|
|
* |
275
|
|
|
* @param string $path Path to the remote file |
276
|
|
|
* @param string $local Path to the local file |
277
|
|
|
* @return \Aimeos\Base\Filesystem\Iface Filesystem object for fluent interface |
278
|
|
|
* @throws \Aimeos\Base\Filesystem\Exception If an error occurs |
279
|
|
|
*/ |
280
|
|
|
public function writef( string $path, string $local ) : Iface |
281
|
|
|
{ |
282
|
|
|
if( ( $content = @file_get_contents( $local ) ) === false ) { |
283
|
|
|
throw new Exception( sprintf( 'Couldn\'t read file "%1$s"', $local ) ); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $this->write( $path, $content ); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Write the content of the stream descriptor into the remote file |
292
|
|
|
* |
293
|
|
|
* {@inheritDoc} |
294
|
|
|
* |
295
|
|
|
* @param string $path Path to the file |
296
|
|
|
* @param resource $stream File stream descriptor |
297
|
|
|
* @return \Aimeos\Base\Filesystem\Iface Filesystem object for fluent interface |
298
|
|
|
* @throws \Aimeos\Base\Filesystem\Exception If an error occurs |
299
|
|
|
*/ |
300
|
|
|
public function writes( string $path, $stream ) : Iface |
301
|
|
|
{ |
302
|
|
|
try |
303
|
|
|
{ |
304
|
|
|
if( ( $content = @fread( $stream, 0x7ffffffd ) ) === false ) { |
305
|
|
|
throw new \Exception( error_get_last()['message'] ); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$this->fs->put( $path, $content ); |
309
|
|
|
} |
310
|
|
|
catch( \Throwable $e ) |
311
|
|
|
{ |
312
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return $this; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Renames a file, moves it to a new location or both at once |
321
|
|
|
* |
322
|
|
|
* @param string $from Path to the original file |
323
|
|
|
* @param string $to Path to the new file |
324
|
|
|
* @return \Aimeos\Base\Filesystem\Iface Filesystem object for fluent interface |
325
|
|
|
* @throws \Aimeos\Base\Filesystem\Exception If an error occurs |
326
|
|
|
*/ |
327
|
|
|
public function move( string $from, string $to ) : Iface |
328
|
|
|
{ |
329
|
|
|
try { |
330
|
|
|
$this->fs->move( $from, $to ); |
331
|
|
|
} catch( \Exception $e ) { |
332
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Copies a file to a new location |
341
|
|
|
* |
342
|
|
|
* @param string $from Path to the original file |
343
|
|
|
* @param string $to Path to the new file |
344
|
|
|
* @return \Aimeos\Base\Filesystem\Iface Filesystem object for fluent interface |
345
|
|
|
* @throws \Aimeos\Base\Filesystem\Exception If an error occurs |
346
|
|
|
*/ |
347
|
|
|
public function copy( string $from, string $to ) : Iface |
348
|
|
|
{ |
349
|
|
|
try { |
350
|
|
|
$this->fs->copy( $from, $to ); |
351
|
|
|
} catch( \Exception $e ) { |
352
|
|
|
throw new Exception( $e->getMessage(), 0, $e ); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
return $this; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|