1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Backup package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2015 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* This project is fork of "kbond/php-backup", for full credits info, please |
11
|
|
|
* view CREDITS file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
namespace RunOpenCode\Backup\Backup; |
14
|
|
|
|
15
|
|
|
use RunOpenCode\Backup\Contract\FileInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class File |
19
|
|
|
* |
20
|
|
|
* Backup file abstraction. |
21
|
|
|
* |
22
|
|
|
* @package RunOpenCode\Backup\Backup |
23
|
|
|
*/ |
24
|
|
|
final class File implements FileInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $path; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string; |
33
|
|
|
*/ |
34
|
|
|
private $rootPath; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string; |
38
|
|
|
*/ |
39
|
|
|
private $relativePath; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var integer |
43
|
|
|
*/ |
44
|
|
|
private $size; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \DateTimeInterface |
48
|
|
|
*/ |
49
|
|
|
private $createdAt; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \DateTimeInterface |
53
|
|
|
*/ |
54
|
|
|
private $modifiedAt; |
55
|
|
|
|
56
|
38 |
|
public function __construct($path, $rootPath, $size, $createdAt, $modifiedAt) |
57
|
|
|
{ |
58
|
38 |
|
$this->path = $path; |
59
|
38 |
|
$this->rootPath = rtrim(is_null($rootPath) ? '' : $rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
60
|
38 |
|
$this->size = $size; |
61
|
38 |
|
$this->createdAt = (is_numeric($createdAt)) ? date_timestamp_set(new \DateTime(), $createdAt) : clone $createdAt; |
62
|
38 |
|
$this->modifiedAt = (is_numeric($modifiedAt)) ? date_timestamp_set(new \DateTime(), $modifiedAt) : clone $modifiedAt; |
63
|
38 |
|
$this->relativePath = null; |
64
|
38 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
26 |
|
public function getPath() |
70
|
|
|
{ |
71
|
26 |
|
return $this->path; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function getRootPath() |
78
|
|
|
{ |
79
|
|
|
return $this->rootPath; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
28 |
|
public function getRelativePath() |
86
|
|
|
{ |
87
|
28 |
|
if (is_null($this->relativePath)) { |
88
|
|
|
|
89
|
28 |
|
$pos = strpos($this->path, $this->rootPath); |
90
|
|
|
|
91
|
28 |
|
if ($pos === 0) { |
92
|
28 |
|
$this->relativePath = substr_replace($this->path, '', $pos, strlen($this->rootPath)); |
93
|
14 |
|
} else { |
94
|
|
|
$this->relativePath = $this->path; |
95
|
|
|
} |
96
|
14 |
|
} |
97
|
|
|
|
98
|
28 |
|
return $this->relativePath; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
24 |
|
public function getSize() |
105
|
|
|
{ |
106
|
24 |
|
return $this->size; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getCreatedAt() |
113
|
|
|
{ |
114
|
|
|
return (!is_null($this->createdAt)) ? clone $this->createdAt : null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
2 |
|
public function getModifiedAt() |
121
|
|
|
{ |
122
|
2 |
|
return (!is_null($this->modifiedAt)) ? clone $this->modifiedAt : null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Create File instance from local, mounted filesystem. |
127
|
|
|
* |
128
|
|
|
* @param string $path Path to file. |
129
|
|
|
* @param null|string $rootPath Root path of file. |
130
|
|
|
* @return File Created backup file instance. |
131
|
|
|
*/ |
132
|
38 |
|
public static function fromLocal($path, $rootPath = null) |
133
|
|
|
{ |
134
|
38 |
|
return new static( |
135
|
19 |
|
$path, |
136
|
19 |
|
$rootPath, |
137
|
19 |
|
filesize($path), |
138
|
19 |
|
filectime($path), |
139
|
19 |
|
filemtime($path) |
140
|
19 |
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Create file instance from \SplFileInfo instance. |
145
|
|
|
* |
146
|
|
|
* @param \SplFileInfo $file |
147
|
|
|
* @param null|string $rootPath Root path of file. |
148
|
|
|
* @return File |
149
|
|
|
*/ |
150
|
10 |
|
public static function fromSplFileInfo(\SplFileInfo $file, $rootPath = null) |
151
|
|
|
{ |
152
|
10 |
|
return new static( |
153
|
10 |
|
$file->getPathname(), |
154
|
5 |
|
$rootPath, |
155
|
10 |
|
$file->getSize(), |
156
|
10 |
|
$file->getCTime(), |
157
|
10 |
|
$file->getMTime() |
158
|
5 |
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Create file instance from Flysystem file metadata. |
163
|
|
|
* |
164
|
|
|
* @param array $metadata Flysystem file metadata. |
165
|
|
|
* @param null|string $rootPath Root path of file. |
166
|
|
|
* @return File |
167
|
|
|
*/ |
168
|
4 |
|
public static function fromFlysystemMetadata(array $metadata, $rootPath = null) |
169
|
|
|
{ |
170
|
4 |
|
return new static( |
171
|
4 |
|
$metadata['path'], |
172
|
2 |
|
$rootPath, |
173
|
4 |
|
$metadata['size'], |
174
|
4 |
|
$metadata['timestamp'], |
175
|
4 |
|
$metadata['timestamp'] |
176
|
2 |
|
); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|