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
|
|
|
} elseif ($stream) { |
67
|
|
|
return $this->openReadStream($real); |
68
|
|
|
} else { |
69
|
|
|
return $this->readFile($real); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
|
|
public function getMeta(/*# string */ $path)/*# : array */ |
77
|
|
|
{ |
78
|
|
|
$real = $this->realPath($path); |
79
|
|
|
|
80
|
|
|
if ($this->isRealDir($real)) { |
81
|
|
|
return []; |
82
|
|
|
} elseif ($this->use_metafile) { |
83
|
|
|
$meta = $this->readFile($real . '.meta'); |
84
|
|
|
return is_string($meta) ? unserialize($meta) : []; |
85
|
|
|
} else { |
86
|
|
|
return $this->getRealMeta($real); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
public function setContent(/*# string */ $path, $content)/*# : bool */ |
94
|
|
|
{ |
95
|
|
|
$real = $this->realPath($path); |
96
|
|
|
|
97
|
|
|
if ($this->ensurePath($real)) { |
98
|
|
|
if (is_resource($content)) { |
99
|
|
|
$res = $this->writeStream($real, $content); |
100
|
|
|
} else { |
101
|
|
|
$res = $this->writeFile($real, $content); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $res ?: $this->setError( |
105
|
|
|
Message::get(Message::STR_WRITEFILE_FAIL, $path), |
106
|
|
|
Message::STR_WRITEFILE_FAIL |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
|
|
public function setMeta(/*# string */ $path, array $meta)/*# : bool */ |
116
|
|
|
{ |
117
|
|
|
$real = $this->realPath($path); |
118
|
|
|
|
119
|
|
|
if ($this->use_metafile) { |
120
|
|
|
$new = array_replace($this->getMeta($path), $meta); |
121
|
|
|
$res = $this->writeFile($real . '.meta', serialize($new)); |
122
|
|
|
} else { |
123
|
|
|
$res = $this->setRealMeta($real, $meta); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $res ?: $this->setError( |
127
|
|
|
Message::get(Message::STR_SETMETA_FAIL, $real), |
128
|
|
|
Message::STR_SETMETA_FAIL |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
|
|
public function isDir(/*# string */ $path)/*# : bool */ |
136
|
|
|
{ |
137
|
|
|
$real = $this->realPath($path); |
138
|
|
|
return $this->isRealDir($real); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritDoc} |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function rename(/*# string */ $from, /*# string */ $to)/*# : bool */ |
145
|
|
|
{ |
146
|
|
|
$real_to = $this->realPath($to); |
147
|
|
|
|
148
|
|
|
if ($this->ensurePath($real_to)) { |
149
|
|
|
$real_from = $this->realPath($from); |
150
|
|
|
|
151
|
|
|
if ($this->isRealDir($real_from)) { |
152
|
|
|
$res = $this->renameDir($real_from, $real_to); |
153
|
|
|
} else { |
154
|
|
|
$res = $this->renameFile($real_from, $real_to); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $res ?: $this->setError( |
158
|
|
|
Message::get(Message::STR_RENAME_FAIL, $real_from, $real_to), |
159
|
|
|
Message::STR_RENAME_FAIL |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritDoc} |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
public function copy(/*# string */ $from, /*# string */ $to)/*# : bool */ |
169
|
|
|
{ |
170
|
|
|
$real_from = $this->realPath($from); |
171
|
|
|
$real_to = $this->realPath($to); |
172
|
|
|
|
173
|
|
|
if ($this->ensurePath($real_to)) { |
174
|
|
|
if ($this->isRealDir($real_from)) { |
175
|
|
|
$res = $this->copyDir($real_from, $real_to); |
176
|
|
|
} else { |
177
|
|
|
$res = $this->copyFile($real_from, $real_to); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $res ?: $this->setError( |
181
|
|
|
Message::get(Message::STR_COPY_FAIL, $real_from, $real_to), |
182
|
|
|
Message::STR_COPY_FAIL |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritDoc} |
190
|
|
|
*/ |
191
|
|
|
public function delete(/*# string */ $path)/*# : bool */ |
192
|
|
|
{ |
193
|
|
|
$real = $this->realPath($path); |
194
|
|
|
|
195
|
|
|
if ($this->isRealDir($real)) { |
196
|
|
|
return $this->deleteDir($real, $path === ''); |
197
|
|
|
} else { |
198
|
|
|
if ($this->use_metafile) { |
199
|
|
|
$this->deleteFile($real . '.meta'); |
200
|
|
|
} |
201
|
|
|
return $this->deleteFile($real); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns driver specific real path |
207
|
|
|
* @param string $path |
208
|
|
|
* @return string |
209
|
|
|
* @access protected |
210
|
|
|
*/ |
211
|
|
|
abstract protected function realPath(/*# string */ $path)/*# : string */; |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Exists of real path |
215
|
|
|
* |
216
|
|
|
* @param string $realPath |
217
|
|
|
* @return bool |
218
|
|
|
* @access protected |
219
|
|
|
*/ |
220
|
|
|
abstract protected function realExists(/*# string */ $realPath)/*# : bool */; |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Is path a directory |
224
|
|
|
* |
225
|
|
|
* @param string $realPath |
226
|
|
|
* @return bool |
227
|
|
|
* @access protected |
228
|
|
|
*/ |
229
|
|
|
abstract protected function isRealDir(/*# string */ $realPath)/*# : bool */; |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Read directory, returns an array of paths in this directory |
233
|
|
|
* |
234
|
|
|
* @param string $realPath |
235
|
|
|
* @param string $prefix prefix to prepend to the results |
236
|
|
|
* @return array |
237
|
|
|
* @access protected |
238
|
|
|
*/ |
239
|
|
|
abstract protected function readDir( |
240
|
|
|
/*# string */ $realPath, |
241
|
|
|
/*# string */ $prefix = '' |
242
|
|
|
)/*# : array */; |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* create directory |
246
|
|
|
* |
247
|
|
|
* @param string $realPath |
248
|
|
|
* @access protected |
249
|
|
|
*/ |
250
|
|
|
abstract protected function makeDirectory( |
251
|
|
|
/*# string */ $realPath |
252
|
|
|
)/*# : bool */; |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Open read stream |
256
|
|
|
* |
257
|
|
|
* @param string $realPath |
258
|
|
|
* @return resource|null |
259
|
|
|
* @access protected |
260
|
|
|
*/ |
261
|
|
|
abstract protected function openReadStream(/*# string */ $realPath); |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Read file and returns all the content |
265
|
|
|
* |
266
|
|
|
* @param string $realPath |
267
|
|
|
* @return string|null |
268
|
|
|
* @access protected |
269
|
|
|
*/ |
270
|
|
|
abstract protected function readFile(/*# string */ $realPath); |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Get the meta data |
274
|
|
|
* |
275
|
|
|
* @param string $realPath |
276
|
|
|
* @return array |
277
|
|
|
* @access protected |
278
|
|
|
*/ |
279
|
|
|
abstract protected function getRealMeta(/*# string */ $realPath)/*# : array */; |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Make sure path directory exits. |
283
|
|
|
* |
284
|
|
|
* @param string $realPath |
285
|
|
|
* @return bool |
286
|
|
|
* @access protected |
287
|
|
|
*/ |
288
|
|
|
abstract protected function ensurePath(/*# string */ $realPath)/*# : bool */; |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Write to file from stream |
292
|
|
|
* |
293
|
|
|
* @param string $realPath |
294
|
|
|
* @param resource $resource |
295
|
|
|
* @return bool |
296
|
|
|
* @access protected |
297
|
|
|
*/ |
298
|
|
|
abstract protected function writeStream( |
299
|
|
|
/*# string */ $realPath, |
300
|
|
|
$resource |
301
|
|
|
)/*# : bool */; |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Write to file |
305
|
|
|
* |
306
|
|
|
* @param string $realPath |
307
|
|
|
* @param string $content |
308
|
|
|
* @return bool |
309
|
|
|
* @access protected |
310
|
|
|
*/ |
311
|
|
|
abstract protected function writeFile( |
312
|
|
|
/*# string */ $realPath, |
313
|
|
|
/*# string */ $content |
314
|
|
|
)/*# : bool */; |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Write meta data |
318
|
|
|
* |
319
|
|
|
* @param string $realPath |
320
|
|
|
* @param array $meta |
321
|
|
|
* @return bool |
322
|
|
|
* @access protected |
323
|
|
|
*/ |
324
|
|
|
abstract protected function setRealMeta( |
325
|
|
|
/*# string */ $realPath, |
326
|
|
|
array $meta |
327
|
|
|
)/*# : bool */; |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Rename directory |
331
|
|
|
* |
332
|
|
|
* @param string $from |
333
|
|
|
* @param string $to |
334
|
|
|
* @return bool |
335
|
|
|
* @access protected |
336
|
|
|
*/ |
337
|
|
|
abstract protected function renameDir( |
338
|
|
|
/*# string */ $from, |
339
|
|
|
/*# string */ $to |
340
|
|
|
)/*# : bool */; |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Rename file |
344
|
|
|
* |
345
|
|
|
* @param string $from |
346
|
|
|
* @param string $to |
347
|
|
|
* @return bool |
348
|
|
|
* @access protected |
349
|
|
|
*/ |
350
|
|
|
abstract protected function renameFile( |
351
|
|
|
/*# string */ $from, |
352
|
|
|
/*# string */ $to |
353
|
|
|
)/*# : bool */; |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Copy directory |
357
|
|
|
* |
358
|
|
|
* @param string $from |
359
|
|
|
* @param string $to |
360
|
|
|
* @return bool |
361
|
|
|
* @access protected |
362
|
|
|
*/ |
363
|
|
|
abstract protected function copyDir( |
364
|
|
|
/*# string */ $from, |
365
|
|
|
/*# string */ $to |
366
|
|
|
)/*# : bool */; |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Copy file |
370
|
|
|
* |
371
|
|
|
* @param string $from |
372
|
|
|
* @param string $to |
373
|
|
|
* @return bool |
374
|
|
|
* @access protected |
375
|
|
|
*/ |
376
|
|
|
abstract protected function copyFile( |
377
|
|
|
/*# string */ $from, |
378
|
|
|
/*# string */ $to |
379
|
|
|
)/*# : bool */; |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Delete directory |
383
|
|
|
* |
384
|
|
|
* @param string $realPath |
385
|
|
|
* @param bool keep the upper most directory |
386
|
|
|
* @return bool |
387
|
|
|
* @access protected |
388
|
|
|
*/ |
389
|
|
|
abstract protected function deleteDir( |
390
|
|
|
/*# string */ $realPath, |
391
|
|
|
/*# bool */ $keep = false |
392
|
|
|
)/*# : bool */; |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Delete the file |
396
|
|
|
* |
397
|
|
|
* @param string $realPath |
398
|
|
|
* @return bool |
399
|
|
|
* @access protected |
400
|
|
|
*/ |
401
|
|
|
abstract protected function deleteFile(/*# string */ $realPath)/*# : bool */; |
402
|
|
|
} |
403
|
|
|
|