1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_user_paths; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_paths\ArrayPath; |
7
|
|
|
use kalanis\kw_paths\Interfaces\IPaths; |
8
|
|
|
use kalanis\kw_paths\PathsException; |
9
|
|
|
use kalanis\kw_routed_paths\RoutedPath; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class InnerLinks |
14
|
|
|
* Extend known path to user file with user's props |
15
|
|
|
*/ |
16
|
|
|
class InnerLinks |
17
|
|
|
{ |
18
|
|
|
protected ArrayPath $arrPath; |
19
|
|
|
protected RoutedPath $routedPath; |
20
|
|
|
protected bool $moreUsers = false; |
21
|
|
|
protected bool $moreLangs = false; |
22
|
|
|
/** @var string[] */ |
23
|
|
|
protected array $prefixPath = []; |
24
|
|
|
protected bool $useSystemPrefix = true; |
25
|
|
|
protected bool $useDataSeparator = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param RoutedPath $routedPath |
29
|
|
|
* @param bool $moreUsers |
30
|
|
|
* @param bool $moreLangs |
31
|
|
|
* @param string[] $prefixPath path to all user data, depends on storage |
32
|
|
|
* @param bool $useSystemPrefix add system dir as prefix |
33
|
|
|
* @param bool $useDataSeparator add system dir as separator of part to user's data itself |
34
|
|
|
*/ |
35
|
172 |
|
public function __construct( |
36
|
|
|
RoutedPath $routedPath, |
37
|
|
|
bool $moreUsers = false, |
38
|
|
|
bool $moreLangs = false, |
39
|
|
|
array $prefixPath = [], |
40
|
|
|
bool $useSystemPrefix = true, |
41
|
|
|
bool $useDataSeparator = true |
42
|
|
|
) { |
43
|
172 |
|
$this->arrPath = new ArrayPath(); |
44
|
172 |
|
$this->routedPath = $routedPath; |
45
|
172 |
|
$this->moreUsers = $moreUsers; |
46
|
172 |
|
$this->moreLangs = $moreLangs; |
47
|
172 |
|
$this->prefixPath = $prefixPath; |
48
|
172 |
|
$this->useSystemPrefix = $useSystemPrefix; |
49
|
172 |
|
$this->useDataSeparator = $useDataSeparator; |
50
|
172 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Path to system things |
54
|
|
|
* @param string $module |
55
|
|
|
* @param string[] $current |
56
|
|
|
* @return string[] |
57
|
|
|
*/ |
58
|
43 |
|
public function toModulePath(string $module, array $current): array |
59
|
|
|
{ |
60
|
43 |
|
return array_merge( |
61
|
43 |
|
$this->prefixPath, |
62
|
43 |
|
$this->useSystemPrefix ? $this->addModuleSeparator() : [], |
63
|
43 |
|
[$module], |
64
|
|
|
$current |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Path to user system things |
70
|
|
|
* @param string[] $current |
71
|
|
|
* @throws PathsException |
72
|
|
|
* @return string[] |
73
|
|
|
*/ |
74
|
43 |
|
public function toUserPath(array $current): array |
75
|
|
|
{ |
76
|
43 |
|
return array_merge( |
77
|
43 |
|
$this->prefixPath, |
78
|
43 |
|
$this->useSystemPrefix ? $this->addPrefixSeparator() : [], |
79
|
43 |
|
$this->moreUsers ? $this->addUser($this->routedPath->getUser()) : [], |
80
|
|
|
$current |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Path to user system things |
86
|
|
|
* @param string $module |
87
|
|
|
* @param string[] $current |
88
|
|
|
* @throws PathsException |
89
|
|
|
* @return string[] |
90
|
|
|
*/ |
91
|
43 |
|
public function toUserModulePath(string $module, array $current): array |
92
|
|
|
{ |
93
|
43 |
|
return array_merge( |
94
|
43 |
|
$this->prefixPath, |
95
|
43 |
|
$this->useSystemPrefix ? $this->addPrefixSeparator() : [], |
96
|
43 |
|
$this->moreUsers ? $this->addUser($this->routedPath->getUser()) : [], |
97
|
43 |
|
$this->useDataSeparator ? $this->addModuleSeparator() : [], |
98
|
43 |
|
[$module], |
99
|
|
|
$current |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Path to user data |
105
|
|
|
* @param string[] $current |
106
|
|
|
* @throws PathsException |
107
|
|
|
* @return string[] |
108
|
|
|
*/ |
109
|
43 |
|
public function toFullPath(array $current): array |
110
|
|
|
{ |
111
|
43 |
|
return array_merge( |
112
|
43 |
|
$this->prefixPath, |
113
|
43 |
|
$this->useSystemPrefix ? $this->addPrefixSeparator() : [], |
114
|
43 |
|
$this->moreUsers ? $this->addUser($this->routedPath->getUser()) : [], |
115
|
43 |
|
$this->useDataSeparator ? $this->addDataSeparator() : [], |
116
|
43 |
|
$this->moreLangs ? $this->addLang($this->routedPath->getLang()) : [], |
117
|
|
|
$current |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string[] |
123
|
|
|
*/ |
124
|
44 |
|
protected function addModuleSeparator(): array |
125
|
|
|
{ |
126
|
44 |
|
return [IPaths::DIR_MODULE]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string[] |
131
|
|
|
*/ |
132
|
66 |
|
protected function addPrefixSeparator(): array |
133
|
|
|
{ |
134
|
66 |
|
return [IPaths::DIR_USER]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $user |
139
|
|
|
* @throws PathsException |
140
|
|
|
* @return string[] |
141
|
|
|
*/ |
142
|
60 |
|
protected function addUser(string $user): array |
143
|
|
|
{ |
144
|
60 |
|
return $this->arrPath->setString($user)->getArray(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string[] |
149
|
|
|
*/ |
150
|
22 |
|
protected function addDataSeparator(): array |
151
|
|
|
{ |
152
|
22 |
|
return [IPaths::DIR_DATA]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $lang |
157
|
|
|
* @throws PathsException |
158
|
|
|
* @return string[] |
159
|
|
|
*/ |
160
|
20 |
|
protected function addLang(string $lang): array |
161
|
|
|
{ |
162
|
20 |
|
return $this->arrPath->setString($lang)->getArray(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|