|
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 isRealDir(/*# 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
|
|
|
* {@inheritDoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function copyDir( |
|
77
|
|
|
/*# string */ $from, |
|
78
|
|
|
/*# string */ $to |
|
79
|
|
|
)/*# : bool */ { |
|
80
|
|
|
if (!$this->makeDirectory($to)) { |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$files = $this->readDir($from); |
|
85
|
|
|
foreach ($files as $file) { |
|
86
|
|
|
$f = $from . \DIRECTORY_SEPARATOR . $file; |
|
87
|
|
|
$t = $to . \DIRECTORY_SEPARATOR . $file; |
|
88
|
|
|
if (is_dir($f)) { |
|
89
|
|
|
|
|
90
|
|
|
$res = $this->copyDir($f, $t); |
|
91
|
|
|
} else { |
|
92
|
|
|
$res = @copy($f, $t); |
|
93
|
|
|
} |
|
94
|
|
|
if (false === $res) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritDoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function deleteDir(/*# string */ $realPath)/*# : bool */ |
|
105
|
|
|
{ |
|
106
|
|
|
$pref = rtrim($realPath, '/\\') . \DIRECTORY_SEPARATOR; |
|
107
|
|
|
$files = $this->readDir($realPath, $pref); |
|
108
|
|
|
|
|
109
|
|
|
foreach ($files as $file) { |
|
110
|
|
|
if (is_dir($file)) { |
|
111
|
|
|
$res = $this->deleteDir($file); |
|
112
|
|
|
} else { |
|
113
|
|
|
$res = unlink($file); |
|
114
|
|
|
} |
|
115
|
|
|
if (false === $res) { |
|
116
|
|
|
return $this->setError( |
|
117
|
|
|
Message::get(Message::STR_DELETE_FAIL, $file), |
|
118
|
|
|
Message::STR_DELETE_FAIL |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
return rmdir($realPath); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* create directory |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $dir |
|
129
|
|
|
* @access protected |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function makeDirectory(/*# string */ $dir)/*# : bool */ |
|
132
|
|
|
{ |
|
133
|
|
|
if (!is_dir($dir)) { |
|
134
|
|
|
$umask = umask(0); |
|
135
|
|
|
@mkdir($dir, 0755, true); |
|
|
|
|
|
|
136
|
|
|
umask($umask); |
|
137
|
|
|
|
|
138
|
|
|
if (!is_dir($dir)) { |
|
139
|
|
|
$this->setError( |
|
140
|
|
|
Message::get(Message::STR_MKDIR_FAIL, $dir), |
|
141
|
|
|
Message::STR_MKDIR_FAIL |
|
142
|
|
|
); |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
abstract public function setError( |
|
150
|
|
|
/*# string */ $message = '', |
|
151
|
|
|
/*# string */ $code = '' |
|
152
|
|
|
)/*# : bool */; |
|
153
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: