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 extends DirectoryItem { |
13
|
|
|
|
14
|
|
|
const REGEXP_PATH = '@^(([a-z0-9._\-][\/]?))+$@'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Instância um diretório |
18
|
|
|
* @param string $path Caminho relativo |
19
|
|
|
*/ |
20
|
|
|
public function __construct($path) { |
21
|
|
|
$this->setPath($path); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** @return string */ |
25
|
|
|
public function __toString() { |
26
|
|
|
return $this->toString(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $path Caminho relativo |
31
|
|
|
* @throws Exception |
32
|
|
|
*/ |
33
|
|
|
protected function setPath($path) { |
34
|
|
|
if (!preg_match(static::REGEXP_PATH, $path . DIRECTORY_SEPARATOR)) { |
35
|
|
|
throw new Exception($path . ' is a invalid directory path.'); |
36
|
|
|
} |
37
|
|
|
parent::setPath($path); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @return string */ |
41
|
|
|
public function getName() { |
42
|
|
|
return pathinfo($this->getPath(), PATHINFO_BASENAME); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** @return string */ |
46
|
|
|
public function toString() { |
47
|
|
|
return $this->getName(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @return boolean */ |
51
|
|
|
public function exists() { |
52
|
|
|
return is_dir($this->getAbsolutePath()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Exclui o diretório e o seu conteúdo |
57
|
|
|
* @return boolean |
58
|
|
|
*/ |
59
|
|
|
public function delete() { |
60
|
|
|
$success = false; |
61
|
|
|
if ($this->exists()) { |
62
|
|
|
$this->clear(); |
63
|
|
|
$success = rmdir($this->getAbsolutePath()); |
64
|
|
|
} |
65
|
|
|
return $success; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Renomeia o diretório |
70
|
|
|
* @param string $newPath |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
|
|
public function rename($newPath) { |
74
|
|
|
parent::rename($newPath); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Exclui apenas o conteúdo do diretório |
79
|
|
|
*/ |
80
|
|
|
public function clear() { |
81
|
|
|
foreach ($this->getItemsName() as $content) { |
82
|
|
|
if (is_dir($this->getAbsolutePath() . DIRECTORY_SEPARATOR . $content)) { |
83
|
|
|
$subDirectory = new Directory($this->getPath() . DIRECTORY_SEPARATOR . $content); |
84
|
|
|
$subDirectory->delete(); |
85
|
|
|
} else { |
86
|
|
|
unlink($this->getAbsolutePath() . DIRECTORY_SEPARATOR . $content); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Cria o diretório |
93
|
|
|
* @param int $chmod |
94
|
|
|
* @return boolean |
95
|
|
|
* @throws Exception |
96
|
|
|
*/ |
97
|
|
|
public function create($chmod = 0755) { |
98
|
|
|
if (!$this->exists()) { |
99
|
|
|
if (@mkdir($this->getAbsolutePath(), $chmod, (boolean) STREAM_MKDIR_RECURSIVE) === false) { |
100
|
|
|
throw new Exception('The directory ' . $this->getPath() . ' could not be created.'); |
101
|
|
|
} |
102
|
|
|
$this->setChmod($chmod); |
103
|
|
|
} |
104
|
|
|
return $this->exists(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** @return boolean */ |
108
|
|
|
public function isEmpty() { |
109
|
|
|
return (count($this->getItemsName()) == 0); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retorna nome dos itens dentro do diretório (em ordem alfabética) |
114
|
|
|
* @return string[] |
115
|
|
|
*/ |
116
|
|
|
public function getItemsName() { |
117
|
|
|
return array_values(array_diff(scandir($this->getAbsolutePath()), ['.', '..'])); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Retorna os itens dentro do diretório (em ordem alfabética) |
122
|
|
|
* @return Directory[]|File[] |
123
|
|
|
*/ |
124
|
|
|
public function getItems() { |
125
|
|
|
$items = []; |
126
|
|
|
foreach ($this->getItemsName() as $itemName) { |
127
|
|
|
$itemPath = $this->getPath() . DIRECTORY_SEPARATOR . $itemName; |
128
|
|
|
if (is_dir(BASE_PATH . DIRECTORY_SEPARATOR . $itemPath)) { |
129
|
|
|
$items[] = new Directory($itemPath); |
130
|
|
|
} else { |
131
|
|
|
$items[] = new File($itemPath); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return $items; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|