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\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Storage\Message\Message; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* LocalDirTrait |
21
|
|
|
* |
22
|
|
|
* Dealing with directories in LocalDriver |
23
|
|
|
* |
24
|
|
|
* @package Phossa2\Storage |
25
|
|
|
* @author Hong Zhang <[email protected]> |
26
|
|
|
* @version 2.0.0 |
27
|
|
|
* @since 2.0.0 added |
28
|
|
|
*/ |
29
|
|
|
trait LocalDirTrait |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritDoc} |
33
|
|
|
*/ |
34
|
|
|
protected function isDir(/*# string */ $realPath)/*# : bool */ |
35
|
|
|
{ |
36
|
|
|
return is_dir($realPath); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
|
|
protected function readDir( |
43
|
|
|
/*# string */ $realPath, |
44
|
|
|
/*# string */ $prefix = '' |
45
|
|
|
)/*# : array */ { |
46
|
|
|
try { |
47
|
|
|
$res = []; |
48
|
|
|
foreach (new \DirectoryIterator($realPath) as $fileInfo) { |
49
|
|
|
if($fileInfo->isDot()) continue; |
50
|
|
|
$res[] = $prefix . $fileInfo->getFilename(); |
51
|
|
|
} |
52
|
|
|
return $res; |
53
|
|
|
|
54
|
|
|
} catch (\Exception $e) { |
55
|
|
|
$this->setError( |
56
|
|
|
Message::get(Message::STR_READDIR_FAIL, $realPath), |
57
|
|
|
Message::STR_READDIR_FAIL |
58
|
|
|
); |
59
|
|
|
return []; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
protected function renameDir( |
67
|
|
|
/*# string */ $from, |
68
|
|
|
/*# string */ $to |
69
|
|
|
)/*# : bool */ { |
70
|
|
|
return rename($from, $to); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
|
|
protected function copyDir( |
78
|
|
|
/*# string */ $from, |
79
|
|
|
/*# string */ $to |
80
|
|
|
)/*# : bool */ { |
81
|
|
|
$files = $this->readDir($from); |
82
|
|
|
|
83
|
|
|
foreach ($files as $file) { |
84
|
|
|
$f = $from . \DIRECTORY_SEPARATOR . $file; |
85
|
|
|
$t = $to . \DIRECTORY_SEPARATOR . $file; |
86
|
|
|
if (is_dir($f)) { |
87
|
|
|
$this->makeDirectory($t); |
88
|
|
|
$res = $this->copyDir($f, $t); |
89
|
|
|
} else { |
90
|
|
|
$res = copy($f, $t); |
91
|
|
|
} |
92
|
|
|
if (false === $res) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritDoc} |
101
|
|
|
*/ |
102
|
|
|
protected function deleteDir(/*# string */ $realPath)/*# : bool */ |
103
|
|
|
{ |
104
|
|
|
$pref = rtrim($realPath, '/\\') . \DIRECTORY_SEPARATOR; |
105
|
|
|
$files = $this->readDir($realPath, $pref); |
106
|
|
|
|
107
|
|
|
foreach ($files as $file) { |
108
|
|
|
if (is_dir($file)) { |
109
|
|
|
$res = $this->deleteDir($file); |
110
|
|
|
} else { |
111
|
|
|
$res = unlink($file); |
112
|
|
|
} |
113
|
|
|
if (false === $res) { |
114
|
|
|
return $this->setError( |
115
|
|
|
Message::get(Message::STR_DELETE_FAIL, $file), |
116
|
|
|
Message::STR_DELETE_FAIL |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
return rmdir($realPath); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* create directory |
125
|
|
|
* |
126
|
|
|
* @param string $dir |
127
|
|
|
* @access protected |
128
|
|
|
*/ |
129
|
|
|
protected function makeDirectory(/*# string */ $dir)/*# : bool */ |
130
|
|
|
{ |
131
|
|
|
if (!is_dir($dir)) { |
132
|
|
|
$umask = umask(0); |
133
|
|
|
mkdir($dir, 0755, true); |
134
|
|
|
umask($umask); |
135
|
|
|
|
136
|
|
|
if (!is_dir($dir)) { |
137
|
|
|
$this->setError( |
138
|
|
|
Message::get(Message::STR_MKDIR_FAIL, $dir), |
139
|
|
|
Message::STR_MKDIR_FAIL |
140
|
|
|
); |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
abstract public function setError( |
148
|
|
|
/*# string */ $message = '', |
149
|
|
|
/*# string */ $code = '' |
150
|
|
|
)/*# : bool */; |
151
|
|
|
} |