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