Completed
Push — master ( cab8e0...64094d )
by Hong
02:15
created

DriverAbstract::rename()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 20
Ratio 100 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 20
loc 20
rs 9.2
cc 4
eloc 12
nc 5
nop 2
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 View Code Duplication
    public function rename(/*# string */ $from, /*# string */ $to)/*# : bool */
149
    {
150
        $real_to = $this->realPath($to);
151
152
        if ($this->ensurePath($real_to)) {
153
            $real_from = $this->realPath($from);
154
155
            if ($this->isRealDir($real_from)) {
156
                $res = $this->renameDir($real_from, $real_to);
157
            } else {
158
                $res = $this->renameFile($real_from, $real_to);
159
            }
160
161
            return $res ?: $this->setError(
162
                Message::get(Message::STR_RENAME_FAIL, $real_from, $real_to),
163
                Message::STR_RENAME_FAIL
164
            );
165
        }
166
        return false;
167
    }
168
169
    /**
170
     * {@inheritDoc}
171
     */
172 View Code Duplication
    public function copy(/*# string */ $from, /*# string */ $to)/*# : bool */
173
    {
174
        $real_from = $this->realPath($from);
175
        $real_to = $this->realPath($to);
176
177
        if ($this->ensurePath($real_to)) {
178
            if ($this->isRealDir($real_from)) {
179
                $res = $this->copyDir($real_from, $real_to);
180
            } else {
181
                $res = $this->copyFile($real_from, $real_to);
182
            }
183
184
            return $res ?: $this->setError(
185
                Message::get(Message::STR_COPY_FAIL, $real_from, $real_to),
186
                Message::STR_COPY_FAIL
187
            );
188
        }
189
        return false;
190
    }
191
192
    /**
193
     * {@inheritDoc}
194
     */
195
    public function delete(/*# string */ $path)/*# : bool */
196
    {
197
        $real = $this->realPath($path);
198
199
        if ($this->isRealDir($real)) {
200
            return $this->deleteDir($real, $path === '');
201
        } else {
202
            if ($this->use_metafile) {
203
                $this->deleteFile($real . '.meta');
204
            }
205
            return $this->deleteFile($real);
206
        }
207
    }
208
209
    /**
210
     * Returns driver specific real path
211
     * @param  string $path
212
     * @return string
213
     * @access protected
214
     */
215
    abstract protected function realPath(/*# string */ $path)/*# : string */;
216
217
    /**
218
     * Exists of real path
219
     *
220
     * @param  string $realPath
221
     * @return bool
222
     * @access protected
223
     */
224
    abstract protected function realExists(/*# string */ $realPath)/*# : bool */;
225
226
    /**
227
     * Is path a directory
228
     *
229
     * @param  string $realPath
230
     * @return bool
231
     * @access protected
232
     */
233
    abstract protected function isRealDir(/*# string */ $realPath)/*# : bool */;
234
235
    /**
236
     * Read directory, returns an array of paths in this directory
237
     *
238
     * @param  string $realPath
239
     * @param  string $prefix prefix to prepend to the results
240
     * @return array
241
     * @access protected
242
     */
243
    abstract protected function readDir(
244
        /*# string */ $realPath,
245
        /*# string */ $prefix = ''
246
    )/*# : array */;
247
248
    /**
249
     * create directory
250
     *
251
     * @param  string $realPath
252
     * @access protected
253
     */
254
    abstract  protected function makeDirectory(
255
        /*# string */ $realPath
256
    )/*# : bool */;
257
258
    /**
259
     * Open read stream
260
     *
261
     * @param  string $realPath
262
     * @return resource|null
263
     * @access protected
264
     */
265
    abstract protected function openReadStream(/*# string */ $realPath);
266
267
    /**
268
     * Read file and returns all the content
269
     *
270
     * @param  string $realPath
271
     * @return string|null
272
     * @access protected
273
     */
274
    abstract protected function readFile(/*# string */ $realPath);
275
276
    /**
277
     * Get the meta data
278
     *
279
     * @param  string $realPath
280
     * @return array
281
     * @access protected
282
     */
283
    abstract protected function getRealMeta(/*# string */ $realPath)/*# : array */;
284
285
    /**
286
     * Make sure path directory exits.
287
     *
288
     * @param  string $realPath
289
     * @return bool
290
     * @access protected
291
     */
292
    abstract protected function ensurePath(/*# string */ $realPath)/*# : bool */;
293
294
    /**
295
     * Write to file from stream
296
     *
297
     * @param  string $realPath
298
     * @param  resource $resource
299
     * @return bool
300
     * @access protected
301
     */
302
    abstract protected function writeStream(
303
        /*# string */ $realPath,
304
        $resource
305
    )/*# : bool */;
306
307
    /**
308
     * Write to file
309
     *
310
     * @param  string $realPath
311
     * @param  string $content
312
     * @return bool
313
     * @access protected
314
     */
315
    abstract protected function writeFile(
316
        /*# string */ $realPath,
317
        /*# string */ $content
318
    )/*# : bool */;
319
320
    /**
321
     * Write meta data
322
     *
323
     * @param  string $realPath
324
     * @param  array $meta
325
     * @return bool
326
     * @access protected
327
     */
328
    abstract protected function setRealMeta(
329
        /*# string */ $realPath,
330
        array $meta
331
    )/*# : bool */;
332
333
    /**
334
     * Rename directory
335
     *
336
     * @param  string $from
337
     * @param  string $to
338
     * @return bool
339
     * @access protected
340
     */
341
    abstract protected function renameDir(
342
        /*# string */ $from,
343
        /*# string */ $to
344
    )/*# : bool */;
345
346
    /**
347
     * Rename file
348
     *
349
     * @param  string $from
350
     * @param  string $to
351
     * @return bool
352
     * @access protected
353
     */
354
    abstract protected function renameFile(
355
        /*# string */ $from,
356
        /*# string */ $to
357
    )/*# : bool */;
358
359
    /**
360
     * Copy directory
361
     *
362
     * @param  string $from
363
     * @param  string $to
364
     * @return bool
365
     * @access protected
366
     */
367
    abstract protected function copyDir(
368
        /*# string */ $from,
369
        /*# string */ $to
370
    )/*# : bool */;
371
372
    /**
373
     * Copy file
374
     *
375
     * @param  string $from
376
     * @param  string $to
377
     * @return bool
378
     * @access protected
379
     */
380
    abstract protected function copyFile(
381
        /*# string */ $from,
382
        /*# string */ $to
383
    )/*# : bool */;
384
385
    /**
386
     * Delete directory
387
     *
388
     * @param  string $realPath
389
     * @param  bool keep the upper most directory
390
     * @return bool
391
     * @access protected
392
     */
393
    abstract protected function deleteDir(
394
        /*# string */ $realPath,
395
        /*# bool */ $keep = false
396
    )/*# : bool */;
397
398
    /**
399
     * Delete the file
400
     *
401
     * @param  string $realPath
402
     * @return bool
403
     * @access protected
404
     */
405
    abstract protected function deleteFile(/*# string */ $realPath)/*# : bool */;
406
}
407