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