1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Storage |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Storage\Driver; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
18
|
|
|
use Phossa2\Shared\Error\ErrorAwareTrait; |
19
|
|
|
use Phossa2\Shared\Error\ErrorAwareInterface; |
20
|
|
|
use Phossa2\Storage\Interfaces\DriverInterface; |
21
|
|
|
use Phossa2\Shared\Extension\ExtensionAwareTrait; |
22
|
|
|
use Phossa2\Shared\Extension\ExtensionAwareInterface; |
23
|
|
|
use Phossa2\Storage\Message\Message; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* DriverAbstract |
27
|
|
|
* |
28
|
|
|
* @package Phossa2\Storage |
29
|
|
|
* @author Hong Zhang <[email protected]> |
30
|
|
|
* @see ObjectAbstract |
31
|
|
|
* @see DriverInterface |
32
|
|
|
* @see ErrorAwareInterface |
33
|
|
|
* @see ExtensionAwareInterface |
34
|
|
|
* @version 2.0.0 |
35
|
|
|
* @since 2.0.0 added |
36
|
|
|
*/ |
37
|
|
|
abstract class DriverAbstract extends ObjectAbstract implements DriverInterface, ErrorAwareInterface, ExtensionAwareInterface |
38
|
|
|
{ |
39
|
|
|
use ErrorAwareTrait, ExtensionAwareTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Store meta data in a seperate file |
43
|
|
|
* |
44
|
|
|
* @var bool |
45
|
|
|
* @access protected |
46
|
|
|
*/ |
47
|
|
|
protected $use_metafile = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
*/ |
52
|
|
|
public function exists(/*# string */ $path)/*# : bool */ |
53
|
|
|
{ |
54
|
|
|
$real = $this->realPath($path); |
55
|
|
|
return $this->realExists($real); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
*/ |
61
|
|
|
public function getContent(/*# string */ $path, /*# bool */ $stream = false) |
62
|
|
|
{ |
63
|
|
|
$real = $this->realPath($path); |
64
|
|
|
if ($this->isRealDir($real)) { |
65
|
|
|
return $this->readDir($real, rtrim($path, '/\\') . '/'); |
66
|
|
|
|
67
|
|
|
} elseif ($stream) { |
68
|
|
|
return $this->openReadStream($real); |
69
|
|
|
|
70
|
|
|
} else { |
71
|
|
|
return $this->readFile($real); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritDoc} |
77
|
|
|
*/ |
78
|
|
|
public function getMeta(/*# string */ $path)/*# : array */ |
79
|
|
|
{ |
80
|
|
|
$real = $this->realPath($path); |
81
|
|
|
|
82
|
|
|
if ($this->isRealDir($real)) { |
83
|
|
|
return []; |
84
|
|
|
|
85
|
|
|
} elseif ($this->use_metafile) { |
86
|
|
|
$meta = $this->readFile($real . '.meta'); |
87
|
|
|
return is_string($meta) ? unserialize($meta) : []; |
88
|
|
|
|
89
|
|
|
} else { |
90
|
|
|
return $this->getRealMeta($real); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function setContent(/*# string */ $path, $content)/*# : bool */ |
98
|
|
|
{ |
99
|
|
|
$real = $this->realPath($path); |
100
|
|
|
|
101
|
|
|
if ($this->ensurePath($real)) { |
102
|
|
|
if (is_resource($content)) { |
103
|
|
|
$res = $this->writeStream($real, $content); |
104
|
|
|
} else { |
105
|
|
|
$res = $this->writeFile($real, $content); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $res ?: $this->setError( |
109
|
|
|
Message::get(Message::STR_WRITEFILE_FAIL, $path), |
110
|
|
|
Message::STR_WRITEFILE_FAIL |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
|
|
public function setMeta(/*# string */ $path, array $meta)/*# : bool */ |
120
|
|
|
{ |
121
|
|
|
$real = $this->realPath($path); |
122
|
|
|
|
123
|
|
|
if ($this->use_metafile) { |
124
|
|
|
$new = array_replace($this->getMeta($path), $meta); |
125
|
|
|
$res = $this->writeFile($real . '.meta', serialize($new)); |
126
|
|
|
} else { |
127
|
|
|
$res = $this->setRealMeta($real, $meta); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $res ?: $this->setError( |
131
|
|
|
Message::get(Message::STR_SETMETA_FAIL, $real), |
132
|
|
|
Message::STR_SETMETA_FAIL |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
*/ |
139
|
|
|
public function isDir(/*# string */ $path)/*# : bool */ |
140
|
|
|
{ |
141
|
|
|
$real = $this->realPath($path); |
142
|
|
|
return $this->isRealDir($real); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
*/ |
148
|
|
|
public function rename(/*# string */ $from, /*# string */ $to)/*# : bool */ |
149
|
|
|
{ |
150
|
|
|
$real_to = $this->realPath($to); |
151
|
|
|
if ($this->ensurePath($real_to)) { |
152
|
|
|
$real_from = $this->realPath($from); |
153
|
|
|
|
154
|
|
|
// rename a file |
155
|
|
|
if (!$this->isRealDir($real_from)) { |
156
|
|
|
$res = $this->renameFile($real_from, $real_to); |
157
|
|
|
|
158
|
|
|
// rename a dir into file is NOT allowed |
159
|
|
|
} elseif ($this->realExists($real_to)){ |
160
|
|
|
$res = false; |
161
|
|
|
|
162
|
|
|
// rename a dir |
163
|
|
|
} else { |
164
|
|
|
$res = $this->renameDir($real_from, $real_to); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $res ?: $this->setError( |
168
|
|
|
Message::get(Message::STR_RENAME_FAIL, $real_from, $real_to), |
169
|
|
|
Message::STR_RENAME_FAIL |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
return false; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritDoc} |
177
|
|
|
*/ |
178
|
|
|
public function copy(/*# string */ $from, /*# string */ $to)/*# : bool */ |
179
|
|
|
{ |
180
|
|
|
$real_from = $this->realPath($from); |
181
|
|
|
$real_to = $this->realPath($to); |
182
|
|
|
|
183
|
|
|
if ($this->ensurePath($real_to)) { |
184
|
|
|
if ($this->isRealDir($real_from)) { |
185
|
|
|
$res = $this->copyDir($real_from, $real_to); |
186
|
|
|
} else { |
187
|
|
|
$res = $this->copyFile($real_from, $real_to); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $res ?: $this->setError( |
191
|
|
|
Message::get(Message::STR_COPY_FAIL, $real_from, $real_to), |
192
|
|
|
Message::STR_COPY_FAIL |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritDoc} |
200
|
|
|
*/ |
201
|
|
|
public function delete(/*# string */ $path)/*# : bool */ |
202
|
|
|
{ |
203
|
|
|
$real = $this->realPath($path); |
204
|
|
|
|
205
|
|
|
if ($this->isRealDir($real)) { |
206
|
|
|
return $this->deleteDir($real); |
207
|
|
|
} else { |
208
|
|
|
if ($this->use_metafile) { |
209
|
|
|
$this->deleteFile($real . '.meta'); |
210
|
|
|
} |
211
|
|
|
return $this->deleteFile($real); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns driver specific real path |
217
|
|
|
* @param string $path |
218
|
|
|
* @return string |
219
|
|
|
* @access protected |
220
|
|
|
*/ |
221
|
|
|
abstract protected function realPath(/*# string */ $path)/*# : string */; |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Exists of real path |
225
|
|
|
* |
226
|
|
|
* @param string $realPath |
227
|
|
|
* @return bool |
228
|
|
|
* @access protected |
229
|
|
|
*/ |
230
|
|
|
abstract protected function realExists(/*# string */ $realPath)/*# : bool */; |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Is path a directory |
234
|
|
|
* |
235
|
|
|
* @param string $realPath |
236
|
|
|
* @return bool |
237
|
|
|
* @access protected |
238
|
|
|
*/ |
239
|
|
|
abstract protected function isRealDir(/*# string */ $realPath)/*# : bool */; |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Read directory, returns an array of paths in this directory |
243
|
|
|
* |
244
|
|
|
* @param string $realPath |
245
|
|
|
* @param string $prefix prefix to prepend to the results |
246
|
|
|
* @return array |
247
|
|
|
* @access protected |
248
|
|
|
*/ |
249
|
|
|
abstract protected function readDir( |
250
|
|
|
/*# string */ $realPath, |
251
|
|
|
/*# string */ $prefix = '' |
252
|
|
|
)/*# : array */; |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* create directory |
256
|
|
|
* |
257
|
|
|
* @param string $realPath |
258
|
|
|
* @access protected |
259
|
|
|
*/ |
260
|
|
|
abstract protected function makeDirectory( |
261
|
|
|
/*# string */ $realPath |
262
|
|
|
)/*# : bool */; |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Open read stream |
266
|
|
|
* |
267
|
|
|
* @param string $realPath |
268
|
|
|
* @return resource|null |
269
|
|
|
* @access protected |
270
|
|
|
*/ |
271
|
|
|
abstract protected function openReadStream(/*# string */ $realPath); |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Read file and returns all the content |
275
|
|
|
* |
276
|
|
|
* @param string $realPath |
277
|
|
|
* @return string|null |
278
|
|
|
* @access protected |
279
|
|
|
*/ |
280
|
|
|
abstract protected function readFile(/*# string */ $realPath); |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Get the meta data |
284
|
|
|
* |
285
|
|
|
* @param string $realPath |
286
|
|
|
* @return array |
287
|
|
|
* @access protected |
288
|
|
|
*/ |
289
|
|
|
abstract protected function getRealMeta(/*# string */ $realPath)/*# : array */; |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Make sure path directory exits. |
293
|
|
|
* |
294
|
|
|
* @param string $realPath |
295
|
|
|
* @return bool |
296
|
|
|
* @access protected |
297
|
|
|
*/ |
298
|
|
|
abstract protected function ensurePath(/*# string */ $realPath)/*# : bool */; |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Write to file from stream |
302
|
|
|
* |
303
|
|
|
* @param string $realPath |
304
|
|
|
* @param resource $resource |
305
|
|
|
* @return bool |
306
|
|
|
* @access protected |
307
|
|
|
*/ |
308
|
|
|
abstract protected function writeStream( |
309
|
|
|
/*# string */ $realPath, |
310
|
|
|
$resource |
311
|
|
|
)/*# : bool */; |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Write to file |
315
|
|
|
* |
316
|
|
|
* @param string $realPath |
317
|
|
|
* @param string $content |
318
|
|
|
* @return bool |
319
|
|
|
* @access protected |
320
|
|
|
*/ |
321
|
|
|
abstract protected function writeFile( |
322
|
|
|
/*# string */ $realPath, |
323
|
|
|
/*# string */ $content |
324
|
|
|
)/*# : bool */; |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Write meta data |
328
|
|
|
* |
329
|
|
|
* @param string $realPath |
330
|
|
|
* @param array $meta |
331
|
|
|
* @return bool |
332
|
|
|
* @access protected |
333
|
|
|
*/ |
334
|
|
|
abstract protected function setRealMeta( |
335
|
|
|
/*# string */ $realPath, |
336
|
|
|
array $meta |
337
|
|
|
)/*# : bool */; |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Rename directory |
341
|
|
|
* |
342
|
|
|
* @param string $from |
343
|
|
|
* @param string $to |
344
|
|
|
* @return bool |
345
|
|
|
* @access protected |
346
|
|
|
*/ |
347
|
|
|
abstract protected function renameDir( |
348
|
|
|
/*# string */ $from, |
349
|
|
|
/*# string */ $to |
350
|
|
|
)/*# : bool */; |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Rename file |
354
|
|
|
* |
355
|
|
|
* @param string $from |
356
|
|
|
* @param string $to |
357
|
|
|
* @return bool |
358
|
|
|
* @access protected |
359
|
|
|
*/ |
360
|
|
|
abstract protected function renameFile( |
361
|
|
|
/*# string */ $from, |
362
|
|
|
/*# string */ $to |
363
|
|
|
)/*# : bool */; |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Copy directory |
367
|
|
|
* |
368
|
|
|
* @param string $from |
369
|
|
|
* @param string $to |
370
|
|
|
* @return bool |
371
|
|
|
* @access protected |
372
|
|
|
*/ |
373
|
|
|
abstract protected function copyDir( |
374
|
|
|
/*# string */ $from, |
375
|
|
|
/*# string */ $to |
376
|
|
|
)/*# : bool */; |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Copy file |
380
|
|
|
* |
381
|
|
|
* @param string $from |
382
|
|
|
* @param string $to |
383
|
|
|
* @return bool |
384
|
|
|
* @access protected |
385
|
|
|
*/ |
386
|
|
|
abstract protected function copyFile( |
387
|
|
|
/*# string */ $from, |
388
|
|
|
/*# string */ $to |
389
|
|
|
)/*# : bool */; |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Delete directory |
393
|
|
|
* |
394
|
|
|
* @param string $realPath |
395
|
|
|
* @return bool |
396
|
|
|
* @access protected |
397
|
|
|
*/ |
398
|
|
|
abstract protected function deleteDir(/*# string */ $realPath)/*# : bool */; |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Delete the file |
402
|
|
|
* |
403
|
|
|
* @param string $realPath |
404
|
|
|
* @return bool |
405
|
|
|
* @access protected |
406
|
|
|
*/ |
407
|
|
|
abstract protected function deleteFile(/*# string */ $realPath)/*# : bool */; |
408
|
|
|
} |
409
|
|
|
|