|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\File; |
|
4
|
|
|
|
|
5
|
|
|
use Win\Calendar\DateTime; |
|
6
|
|
|
use const BASE_PATH; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Item dentro do Diretório |
|
10
|
|
|
* São outros diretório, arquivos, etc |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class DirectoryItem implements DirectoryItemInterface { |
|
13
|
|
|
|
|
14
|
|
|
const REGEXP_PATH = '@^(([a-zA-Z0-9._\-][\/]?))+$@'; |
|
15
|
|
|
const REGEXP_NAME = '@^(([a-zA-Z0-9._\-]?))+$@'; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private $path; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Directory */ |
|
21
|
|
|
private $directory; |
|
22
|
|
|
|
|
23
|
|
|
/** @return string */ |
|
24
|
|
|
public function getPath() { |
|
25
|
|
|
return $this->path; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** @return string */ |
|
29
|
|
|
public function getAbsolutePath() { |
|
30
|
|
|
return BASE_PATH . DIRECTORY_SEPARATOR . $this->path; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** @return string */ |
|
34
|
|
|
public function __toString() { |
|
35
|
|
|
return $this->getPath(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Retorna o diretório pai |
|
40
|
|
|
* @return Directory |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getDirectory() { |
|
43
|
|
|
if (is_null($this->directory)) { |
|
44
|
|
|
$this->directory = new Directory(pathinfo($this->getPath(), PATHINFO_DIRNAME)); |
|
45
|
|
|
} |
|
46
|
|
|
return $this->directory; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** @return string */ |
|
50
|
|
|
public function getName() { |
|
51
|
|
|
return pathinfo($this->getAbsolutePath(), PATHINFO_FILENAME); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** @return string */ |
|
55
|
|
|
public function getBaseName() { |
|
56
|
|
|
return pathinfo($this->getAbsolutePath(), PATHINFO_BASENAME); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @return DateTime */ |
|
60
|
|
|
public function getLastModifiedDate() { |
|
61
|
|
|
$ts = filemtime($this->getAbsolutePath()); |
|
62
|
|
|
return new DateTime("@$ts"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** @param string $path */ |
|
66
|
|
|
protected function setPath($path) { |
|
67
|
|
|
$this->path = $path; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function setDirectory(Directory $directory) { |
|
71
|
|
|
$this->directory = $directory; |
|
72
|
|
|
$path = $directory->getPath() . DIRECTORY_SEPARATOR . $this->getBaseName(); |
|
73
|
|
|
$this->setPath($path); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** @param string */ |
|
77
|
|
|
protected function setName($name) { |
|
78
|
|
|
$path = $this->getDirectory()->getPath() . DIRECTORY_SEPARATOR . $name; |
|
79
|
|
|
$this->setPath($path); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Renomeia |
|
84
|
|
|
* @param string $newName Novo nome |
|
85
|
|
|
* @return boolean |
|
86
|
|
|
*/ |
|
87
|
|
|
public function rename($newName) { |
|
88
|
|
|
$oldPath = $this->getAbsolutePath(); |
|
89
|
|
|
$this->setName($newName); |
|
90
|
|
|
return rename($oldPath, $this->getAbsolutePath()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Move para um novo diretório |
|
95
|
|
|
* @param Directory $destination |
|
96
|
|
|
* @return boolean |
|
97
|
|
|
*/ |
|
98
|
|
|
public function move(Directory $destination) { |
|
99
|
|
|
$oldPath = $this->getAbsolutePath(); |
|
100
|
|
|
$this->setDirectory($destination); |
|
101
|
|
|
if (!$this->getDirectory()->exists()) { |
|
102
|
|
|
$this->getDirectory()->create(); |
|
103
|
|
|
} |
|
104
|
|
|
return rename($oldPath, $this->getAbsolutePath()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Define a permissão ao diretório |
|
109
|
|
|
* @param int $chmod |
|
110
|
|
|
* @return boolean |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setChmod($chmod = 0755) { |
|
113
|
|
|
return @chmod($this->getAbsolutePath(), $chmod); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** @return string */ |
|
117
|
|
|
public function getChmod() { |
|
118
|
|
|
clearstatcache(); |
|
119
|
|
|
return substr(decoct(fileperms($this->getAbsolutePath())), 2); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|