1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Storeman; |
4
|
|
|
|
5
|
|
|
use Storeman\Exception\Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* An index object is the representation of one of this filesystem primitives contained in the index. |
9
|
|
|
*/ |
10
|
|
|
class IndexObject |
11
|
|
|
{ |
12
|
|
|
const TYPE_DIR = 1; |
13
|
|
|
const TYPE_FILE = 2; |
14
|
|
|
const TYPE_LINK = 3; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $relativePath; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $type; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $mtime; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $ctime; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $mode; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
protected $size; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $linkTarget; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $blobId; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Prevent construction not using static factory methods. |
58
|
|
|
*/ |
59
|
|
|
protected function __construct() {} |
60
|
|
|
|
61
|
|
|
public function getRelativePath(): string |
62
|
|
|
{ |
63
|
|
|
return $this->relativePath; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getType(): int |
67
|
|
|
{ |
68
|
|
|
return $this->type; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function isDirectory(): bool |
72
|
|
|
{ |
73
|
|
|
return $this->type === static::TYPE_DIR; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function isFile(): bool |
77
|
|
|
{ |
78
|
|
|
return $this->type === static::TYPE_FILE; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function isLink(): bool |
82
|
|
|
{ |
83
|
|
|
return $this->type === static::TYPE_LINK; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getMtime(): int |
87
|
|
|
{ |
88
|
|
|
return $this->mtime; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getCtime(): int |
92
|
|
|
{ |
93
|
|
|
return $this->ctime; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getMode(): int |
97
|
|
|
{ |
98
|
|
|
return $this->mode; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getSize(): ?int |
102
|
|
|
{ |
103
|
|
|
return $this->size; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getLinkTarget(): ?string |
107
|
|
|
{ |
108
|
|
|
return $this->linkTarget; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getBlobId(): ?string |
112
|
|
|
{ |
113
|
|
|
return $this->blobId; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function setBlobId(string $blobId): IndexObject |
117
|
|
|
{ |
118
|
|
|
$this->blobId = $blobId; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function toScalarArray(): array |
124
|
|
|
{ |
125
|
|
|
return [ |
126
|
|
|
$this->relativePath, |
127
|
|
|
$this->type, |
128
|
|
|
$this->mtime, |
129
|
|
|
$this->ctime, |
130
|
|
|
$this->mode, |
131
|
|
|
$this->size, |
132
|
|
|
$this->blobId, |
133
|
|
|
$this->linkTarget, |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function fromScalarArray(array $array): IndexObject |
138
|
|
|
{ |
139
|
|
|
$object = new static; |
140
|
|
|
$object->relativePath = $array[0]; |
141
|
|
|
$object->type = (int)$array[1]; |
142
|
|
|
$object->mtime = (int)$array[2]; |
143
|
|
|
$object->ctime = (int)$array[3]; |
144
|
|
|
$object->mode = (int)$array[4]; |
145
|
|
|
$object->size = (int)$array[5]; |
146
|
|
|
$object->blobId = $array[6]; |
147
|
|
|
$object->linkTarget = $array[7]; |
148
|
|
|
|
149
|
|
|
return $object; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns an instance representing the filesystem object that can be found under the given path. |
154
|
|
|
* |
155
|
|
|
* @param string $basePath |
156
|
|
|
* @param string $relativePath |
157
|
|
|
* @return IndexObject |
158
|
|
|
*/ |
159
|
|
|
public static function fromPath(string $basePath, string $relativePath): IndexObject |
160
|
|
|
{ |
161
|
|
|
$absolutePath = rtrim($basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $relativePath; |
162
|
|
|
|
163
|
|
|
clearstatcache(null, $absolutePath); |
164
|
|
|
|
165
|
|
|
if (!($stat = @lstat($absolutePath))) |
166
|
|
|
{ |
167
|
|
|
throw new Exception(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$object = new static; |
171
|
|
|
$object->relativePath = $relativePath; |
172
|
|
|
$object->mtime = $stat['mtime']; |
173
|
|
|
$object->ctime = $stat['ctime']; |
174
|
|
|
$object->mode = $stat['mode']; |
175
|
|
|
|
176
|
|
|
if (is_link($absolutePath)) |
177
|
|
|
{ |
178
|
|
|
$object->type = static::TYPE_LINK; |
179
|
|
|
$object->linkTarget = str_replace($basePath, '', readlink($absolutePath)); |
180
|
|
|
} |
181
|
|
|
elseif (is_file($absolutePath)) |
182
|
|
|
{ |
183
|
|
|
$object->type = static::TYPE_FILE; |
184
|
|
|
$object->size = (int)$stat['size']; |
185
|
|
|
} |
186
|
|
|
elseif (is_dir($absolutePath)) |
187
|
|
|
{ |
188
|
|
|
$object->type = static::TYPE_DIR; |
189
|
|
|
} |
190
|
|
|
else |
191
|
|
|
{ |
192
|
|
|
throw new \LogicException(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $object; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|