1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Util; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Path class. |
8
|
|
|
* |
9
|
|
|
* @package phpbu |
10
|
|
|
* @subpackage Backup |
11
|
|
|
* @author Sebastian Feldmann <[email protected]> |
12
|
|
|
* @author Vitaly Baev <[email protected]> |
13
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
14
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
15
|
|
|
* @link https://phpbu.de/ |
16
|
|
|
* @since Class available since Release 5.1.0 |
17
|
|
|
*/ |
18
|
|
|
class Path |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Path |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $path; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Raw path, that could contain placeholders |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $pathRaw; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Part of path, that is permanent |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $pathNotChanging; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Indicates if the path changes over time. |
43
|
|
|
* |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
private $pathIsChanging = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* List of all path elements. |
50
|
|
|
* |
51
|
|
|
* @var string[] |
52
|
|
|
*/ |
53
|
|
|
private $pathElements = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Time for replacing placeholders |
57
|
|
|
* |
58
|
|
|
* @var int |
59
|
|
|
*/ |
60
|
|
|
private $time; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Whether leading slash is needed or not |
64
|
|
|
* |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
private $leadingSlash; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Whether trailing slash is needed or not |
71
|
|
|
* |
72
|
|
|
* @var bool |
73
|
|
|
*/ |
74
|
|
|
private $trailingSlash; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Path constructor. |
78
|
|
|
* |
79
|
|
|
* @param string $path |
80
|
|
|
* @param int|null $time |
81
|
|
|
* @param bool|null $leadingSlash |
82
|
|
|
* @param bool|null $trailingSlash |
83
|
|
|
*/ |
84
|
53 |
|
public function __construct(string $path, $time = null, $leadingSlash = null, $trailingSlash = null) |
85
|
|
|
{ |
86
|
53 |
|
$this->leadingSlash = $leadingSlash; |
87
|
53 |
|
$this->trailingSlash = $trailingSlash; |
88
|
53 |
|
$this->pathRaw = $path; |
89
|
53 |
|
$this->time = $time; |
90
|
|
|
|
91
|
53 |
|
$this->setUp(); |
92
|
53 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Updates path according to provided parameters. |
96
|
|
|
*/ |
97
|
53 |
|
private function setUp() |
98
|
|
|
{ |
99
|
53 |
|
if (!is_null($this->leadingSlash)) { |
100
|
50 |
|
if ($this->leadingSlash) { |
101
|
49 |
|
$this->pathRaw = Util\Path::withLeadingSlash($this->pathRaw); |
102
|
|
|
} else { |
103
|
3 |
|
$this->pathRaw = Util\Path::withoutLeadingSlash($this->pathRaw); |
104
|
|
|
} |
105
|
|
|
} |
106
|
53 |
|
if (!is_null($this->trailingSlash) && !empty($this->pathRaw)) { |
107
|
46 |
|
if ($this->trailingSlash) { |
108
|
1 |
|
$this->pathRaw = Util\Path::withTrailingSlash($this->pathRaw); |
109
|
|
|
} else { |
110
|
46 |
|
$this->pathRaw = Util\Path::withoutTrailingSlash($this->pathRaw); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
53 |
|
$path = $this->pathRaw; |
115
|
53 |
|
if (Util\Path::isContainingPlaceholder($this->pathRaw)) { |
116
|
11 |
|
$this->pathIsChanging = true; |
117
|
11 |
|
$this->detectPathNotChanging($this->pathRaw); |
118
|
|
|
// replace potential date placeholder |
119
|
11 |
|
$path = Util\Path::replaceDatePlaceholders($this->pathRaw, $this->time); |
120
|
|
|
} else { |
121
|
42 |
|
$this->pathNotChanging = $path; |
122
|
|
|
} |
123
|
|
|
|
124
|
53 |
|
$this->path = $path; |
125
|
53 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Find path elements that can't change because of placeholder usage. |
129
|
|
|
* |
130
|
|
|
* @param string $path |
131
|
|
|
*/ |
132
|
11 |
|
private function detectPathNotChanging(string $path) |
133
|
|
|
{ |
134
|
11 |
|
$partsNotChanging = []; |
135
|
11 |
|
$foundChangingElement = false; |
136
|
|
|
|
137
|
11 |
|
foreach (Util\Path::getDirectoryListFromAbsolutePath($path) as $depth => $dir) { |
138
|
11 |
|
$this->pathElements[] = $dir; |
139
|
|
|
|
140
|
|
|
// already found placeholder or found one right now |
141
|
|
|
// path isn't static anymore so don't add directory to path not changing |
142
|
11 |
|
if ($foundChangingElement || Util\Path::isContainingPlaceholder($dir)) { |
143
|
11 |
|
$foundChangingElement = true; |
144
|
11 |
|
continue; |
145
|
|
|
} |
146
|
|
|
// do not add the / element leading slash will be re-added later |
147
|
11 |
|
if ($dir !== '/') { |
148
|
11 |
|
$partsNotChanging[] = $dir; |
149
|
|
|
} |
150
|
|
|
} |
151
|
11 |
|
$this->pathNotChanging = implode(DIRECTORY_SEPARATOR, $partsNotChanging); |
152
|
11 |
|
if (!is_null($this->leadingSlash) && $this->leadingSlash) { |
153
|
11 |
|
$this->pathNotChanging = DIRECTORY_SEPARATOR . $this->pathNotChanging; |
154
|
|
|
} |
155
|
11 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Return path element at given index. |
159
|
|
|
* |
160
|
|
|
* @param int $index |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
6 |
|
public function getPathElementAtIndex(int $index): string |
164
|
|
|
{ |
165
|
6 |
|
return $this->pathElements[$index]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Return the full target path depth. |
170
|
|
|
* |
171
|
|
|
* @return int |
172
|
|
|
*/ |
173
|
11 |
|
public function getPathDepth(): int |
174
|
|
|
{ |
175
|
11 |
|
return count($this->pathElements); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Return the path to the backup file. |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
26 |
|
public function getPath(): string |
184
|
|
|
{ |
185
|
26 |
|
return $this->path; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Return the path to the backup file. |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
4 |
|
public function getPathRaw(): string |
194
|
|
|
{ |
195
|
4 |
|
return $this->pathRaw; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Is dirname configured with any date placeholders. |
200
|
|
|
* |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
3 |
|
public function hasChangingPath(): bool |
204
|
|
|
{ |
205
|
3 |
|
return $this->pathIsChanging; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Return the part of the path that is not changing. |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
13 |
|
public function getPathThatIsNotChanging(): string |
214
|
|
|
{ |
215
|
13 |
|
return $this->pathNotChanging; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Return path when casted to string. |
220
|
|
|
* |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
2 |
|
public function __toString() |
224
|
|
|
{ |
225
|
2 |
|
return $this->path; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|