|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Backup; |
|
3
|
|
|
|
|
4
|
|
|
use phpbu\App\Util; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Trait Path |
|
8
|
|
|
* |
|
9
|
|
|
* @package phpbu |
|
10
|
|
|
* @subpackage Backup |
|
11
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
12
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
13
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
14
|
|
|
* @link https://phpbu.de/ |
|
15
|
|
|
* @since Class available since Release 5.1.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
trait Path |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Absolute path to the directory where to store the backup. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $path; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Path to the backup with potential date placeholders like %d. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $pathRaw; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Indicates if the path changes over time. |
|
35
|
|
|
* |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
private $pathIsChanging = false; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Part of the path without placeholders |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $pathNotChanging; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* List of all path elements. |
|
49
|
|
|
* |
|
50
|
|
|
* @var string[] |
|
51
|
|
|
*/ |
|
52
|
|
|
private $pathElements = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Directory setter. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $path |
|
58
|
|
|
* @param int $time |
|
59
|
|
|
*/ |
|
60
|
46 |
|
public function setPath($path, $time = null) |
|
61
|
|
|
{ |
|
62
|
|
|
// remove trailing slashes |
|
63
|
46 |
|
$path = rtrim($path, DIRECTORY_SEPARATOR); |
|
64
|
46 |
|
$this->pathRaw = $path; |
|
65
|
46 |
|
$this->pathNotChanging = $path; |
|
66
|
|
|
|
|
67
|
46 |
|
if (Util\Path::isContainingPlaceholder($path)) { |
|
68
|
11 |
|
$this->pathIsChanging = true; |
|
69
|
11 |
|
$this->detectPathNotChanging($path); |
|
70
|
|
|
// replace potential date placeholder |
|
71
|
11 |
|
$path = Util\Path::replaceDatePlaceholders($path, $time); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
46 |
|
$this->path = $path; |
|
75
|
46 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Return path element at given index. |
|
79
|
|
|
* |
|
80
|
|
|
* @param int $index |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
6 |
|
public function getPathElementAtIndex(int $index) : string |
|
84
|
|
|
{ |
|
85
|
6 |
|
return $this->pathElements[$index]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Return the full target path depth. |
|
90
|
|
|
* |
|
91
|
|
|
* @return int |
|
92
|
|
|
*/ |
|
93
|
10 |
|
public function getPathDepth() : int |
|
94
|
|
|
{ |
|
95
|
10 |
|
return count($this->pathElements); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Find path elements that can't change because of placeholder usage. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $path |
|
102
|
|
|
*/ |
|
103
|
11 |
|
private function detectPathNotChanging(string $path) |
|
104
|
|
|
{ |
|
105
|
11 |
|
$partsNotChanging = []; |
|
106
|
11 |
|
$foundChangingElement = false; |
|
107
|
|
|
|
|
108
|
11 |
|
foreach (Util\Path::getDirectoryListFromAbsolutePath($path) as $depth => $dir) { |
|
109
|
11 |
|
$this->pathElements[] = $dir; |
|
110
|
|
|
|
|
111
|
|
|
// already found placeholder or found one right now |
|
112
|
|
|
// path isn't static anymore so don't add directory to path not changing |
|
113
|
11 |
|
if ($foundChangingElement || Util\Path::isContainingPlaceholder($dir)) { |
|
114
|
11 |
|
$foundChangingElement = true; |
|
115
|
11 |
|
continue; |
|
116
|
|
|
} |
|
117
|
|
|
// do not add the / element leading slash will be re-added later |
|
118
|
11 |
|
if ($dir !== '/') { |
|
119
|
11 |
|
$partsNotChanging[] = $dir; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
11 |
|
$this->pathNotChanging = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $partsNotChanging); |
|
123
|
11 |
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|