1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\File; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use const BASE_PATH; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Diretório de Arquivos |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class Directory { |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
private $path; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Instância um diretório |
19
|
|
|
* @param string $path |
20
|
|
|
*/ |
21
|
|
|
public function __construct($path) { |
22
|
|
|
$this->path = static::toAbsolutePath($path); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** @return string */ |
26
|
|
|
public function getPath() { |
27
|
|
|
return $this->path; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** @return string */ |
31
|
|
|
public function __toString() { |
32
|
|
|
return $this->getPath(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** @return string */ |
36
|
|
|
public function getRelativePath() { |
37
|
|
|
return str_replace(BASE_PATH . DIRECTORY_SEPARATOR, '', $this->path); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Converte o caminho relativo para absoluto |
42
|
|
|
* @param $relativePath |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public static function toAbsolutePath($relativePath) { |
46
|
|
|
if (!preg_match('@^(([a-z0-9._\-][\/]?))+$@', $relativePath . DIRECTORY_SEPARATOR)) { |
47
|
|
|
throw new Exception($relativePath . ' is a invalid path.'); |
48
|
|
|
} |
49
|
|
|
return BASE_PATH . DIRECTORY_SEPARATOR . $relativePath; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @return boolean */ |
53
|
|
|
public function exists() { |
54
|
|
|
return is_dir($this->path); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Renomeia o diretório |
59
|
|
|
* @param string $newPath Caminho para o novo diretório |
60
|
|
|
* @return boolean |
61
|
|
|
*/ |
62
|
|
|
public function rename($newPath) { |
63
|
|
|
$newFullPath = static::toAbsolutePath($newPath); |
64
|
|
|
$oldFullPath = $this->path; |
65
|
|
|
$this->path = $newFullPath; |
66
|
|
|
return rename($oldFullPath, $newFullPath); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Exclui o diretório e o seu conteúdo |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
|
|
public function delete() { |
74
|
|
|
$return = false; |
75
|
|
|
if ($this->exists()) { |
76
|
|
|
$this->deleteContent(); |
77
|
|
|
$return = rmdir($this->path); |
78
|
|
|
} |
79
|
|
|
return $return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Exclui o conteúdo do diretório |
84
|
|
|
*/ |
85
|
|
|
protected function deleteContent() { |
86
|
|
|
foreach ($this->scan() as $content) { |
87
|
|
|
if (is_dir($this->path . DIRECTORY_SEPARATOR . $content)) { |
88
|
|
|
$subDirectory = new Directory($this->getRelativePath() . DIRECTORY_SEPARATOR . $content); |
89
|
|
|
$subDirectory->delete(); |
90
|
|
|
} else { |
91
|
|
|
unlink($this->path . DIRECTORY_SEPARATOR . $content); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Cria o diretório |
98
|
|
|
* @param int $chmod |
99
|
|
|
* @return boolean |
100
|
|
|
*/ |
101
|
|
|
public function create($chmod = 0755) { |
102
|
|
|
if (!$this->exists()) { |
103
|
|
|
if (@mkdir($this->path, $chmod, (boolean) STREAM_MKDIR_RECURSIVE) === false) { |
104
|
|
|
throw new Exception('The directory ' . $this->path . ' could not be created.'); |
105
|
|
|
} |
106
|
|
|
$this->chmod($chmod); |
107
|
|
|
} |
108
|
|
|
return $this->exists(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Retorna o conteúdo do diretório |
113
|
|
|
* @return string[] |
114
|
|
|
*/ |
115
|
|
|
public function scan() { |
116
|
|
|
return array_diff(scandir($this->path), ['.', '..']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Define a permissão ao diretório |
121
|
|
|
* @param int $chmod |
122
|
|
|
* @return boolean |
123
|
|
|
*/ |
124
|
|
|
public function chmod($chmod = 0755) { |
125
|
|
|
return @chmod($this->path, $chmod); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** @return string */ |
129
|
|
|
public function getPermission() { |
130
|
|
|
clearstatcache(); |
131
|
|
|
return substr(decoct(fileperms($this->path)), 2); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|