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
|
|
|
* Path constructor. |
64
|
|
|
* |
65
|
|
|
* @param string $path |
66
|
|
|
* @param int|null $time |
67
|
|
|
*/ |
68
|
46 |
|
public function __construct(string $path, $time = null) |
69
|
|
|
{ |
70
|
|
|
// remove trailing slashes |
71
|
46 |
|
$path = rtrim($path, DIRECTORY_SEPARATOR); |
72
|
46 |
|
$this->pathRaw = $path; |
73
|
46 |
|
$this->pathNotChanging = $path; |
74
|
46 |
|
$this->time = $time; |
75
|
|
|
|
76
|
46 |
|
if (Util\Path::isContainingPlaceholder($path)) { |
77
|
11 |
|
$this->pathIsChanging = true; |
78
|
11 |
|
$this->detectPathNotChanging($path); |
79
|
|
|
// replace potential date placeholder |
80
|
11 |
|
$path = Util\Path::replaceDatePlaceholders($path, $this->time); |
81
|
|
|
} |
82
|
|
|
|
83
|
46 |
|
$this->path = $path; |
84
|
46 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Find path elements that can't change because of placeholder usage. |
88
|
|
|
* |
89
|
|
|
* @param string $path |
90
|
|
|
*/ |
91
|
11 |
|
private function detectPathNotChanging(string $path) |
92
|
|
|
{ |
93
|
11 |
|
$partsNotChanging = []; |
94
|
11 |
|
$foundChangingElement = false; |
95
|
|
|
|
96
|
11 |
|
foreach (Util\Path::getDirectoryListFromAbsolutePath($path) as $depth => $dir) { |
97
|
11 |
|
$this->pathElements[] = $dir; |
98
|
|
|
|
99
|
|
|
// already found placeholder or found one right now |
100
|
|
|
// path isn't static anymore so don't add directory to path not changing |
101
|
11 |
|
if ($foundChangingElement || Util\Path::isContainingPlaceholder($dir)) { |
102
|
11 |
|
$foundChangingElement = true; |
103
|
11 |
|
continue; |
104
|
|
|
} |
105
|
|
|
// do not add the / element leading slash will be re-added later |
106
|
11 |
|
if ($dir !== '/') { |
107
|
11 |
|
$partsNotChanging[] = $dir; |
108
|
|
|
} |
109
|
|
|
} |
110
|
11 |
|
$this->pathNotChanging = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $partsNotChanging); |
111
|
11 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return path element at given index. |
115
|
|
|
* |
116
|
|
|
* @param int $index |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
6 |
|
public function getPathElementAtIndex(int $index) : string |
120
|
|
|
{ |
121
|
6 |
|
return $this->pathElements[$index]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return the full target path depth. |
126
|
|
|
* |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
10 |
|
public function getPathDepth() : int |
130
|
|
|
{ |
131
|
10 |
|
return count($this->pathElements); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Return the path to the backup file. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
20 |
|
public function getPath() : string |
140
|
|
|
{ |
141
|
20 |
|
return $this->path; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return the path to the backup file. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
2 |
|
public function getPathRaw() : string |
150
|
|
|
{ |
151
|
2 |
|
return $this->pathRaw; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Is dirname configured with any date placeholders. |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
3 |
|
public function hasChangingPath() : bool |
160
|
|
|
{ |
161
|
3 |
|
return $this->pathIsChanging; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Return the part of the path that is not changing. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
11 |
|
public function getPathThatIsNotChanging() : string |
170
|
|
|
{ |
171
|
11 |
|
return $this->pathNotChanging; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Return path when casted to string. |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function __toString() |
180
|
|
|
{ |
181
|
|
|
return $this->path; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|