1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Support; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Contains; |
6
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Logger; |
7
|
|
|
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; |
8
|
|
|
use Helldar\LaravelLangPublisher\Facades\Config as ConfigFacade; |
9
|
|
|
|
10
|
|
|
final class Path |
11
|
|
|
{ |
12
|
|
|
use Contains; |
13
|
|
|
use Logger; |
|
|
|
|
14
|
|
|
|
15
|
24 |
|
public function source(string $package, string $locale): string |
16
|
|
|
{ |
17
|
24 |
|
$this->log('Getting the path to the source files of the localization:', $package, $locale); |
18
|
|
|
|
19
|
24 |
|
if ($this->isEnglish($locale)) { |
20
|
24 |
|
return $this->cleanable($this->getBasePath(), $package, $this->getSourcePath()); |
21
|
|
|
} |
22
|
|
|
|
23
|
7 |
|
return $this->locales($package, $locale); |
24
|
|
|
} |
25
|
|
|
|
26
|
24 |
|
public function target(string $locale, bool $is_json = false): string |
27
|
|
|
{ |
28
|
24 |
|
$this->log('Getting the path to the target files of the localization:', $locale, $is_json); |
29
|
|
|
|
30
|
24 |
|
$path = $this->getTargetPath(); |
31
|
|
|
|
32
|
24 |
|
$suffix = $is_json ? '' : '/' . $locale; |
33
|
|
|
|
34
|
24 |
|
return $this->clean($path) . $suffix; |
35
|
|
|
} |
36
|
|
|
|
37
|
15 |
|
public function targetFull(string $locale, ?string $filename): string |
38
|
|
|
{ |
39
|
15 |
|
$this->log('Getting the full path to the target files of the localization:', $locale, $filename); |
40
|
|
|
|
41
|
15 |
|
$is_json = ! empty($filename) && $this->isJson($filename); |
42
|
|
|
|
43
|
15 |
|
$path = $this->target($locale, $is_json); |
44
|
|
|
|
45
|
15 |
|
return $path . '/' . $filename; |
46
|
|
|
} |
47
|
|
|
|
48
|
19 |
|
public function locales(string $package, string $locale = null): string |
49
|
|
|
{ |
50
|
19 |
|
$this->log('Getting the path to the source translation files for', $package, $locale); |
51
|
|
|
|
52
|
19 |
|
return $this->cleanable($this->getBasePath(), $package, $this->getLocalesPath(), $locale); |
53
|
|
|
} |
54
|
|
|
|
55
|
14 |
|
public function directory(string $path): string |
56
|
|
|
{ |
57
|
14 |
|
$this->log('Getting the directory name from file path:', $path); |
58
|
|
|
|
59
|
14 |
|
$path = pathinfo($path, PATHINFO_DIRNAME); |
60
|
|
|
|
61
|
14 |
|
return $this->clean($path); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
14 |
|
public function filename(string $path): string |
65
|
|
|
{ |
66
|
14 |
|
$this->log('Getting file name without extension and path:', $path); |
67
|
|
|
|
68
|
14 |
|
$path = pathinfo($path, PATHINFO_FILENAME); |
69
|
|
|
|
70
|
14 |
|
return $this->clean($path); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function basename(string $path): string |
74
|
|
|
{ |
75
|
|
|
$this->log('Getting file basename without extension and path:', $path); |
76
|
|
|
|
77
|
|
|
$path = pathinfo($path, PATHINFO_BASENAME); |
78
|
|
|
|
79
|
|
|
return $this->clean($path); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
14 |
|
public function extension(string $path): string |
83
|
|
|
{ |
84
|
14 |
|
$this->log('Getting file extension from path:', $path); |
85
|
|
|
|
86
|
14 |
|
$path = pathinfo($path, PATHINFO_EXTENSION); |
87
|
|
|
|
88
|
14 |
|
return $this->clean($path); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
24 |
|
public function clean(string $path = null, bool $both = false): ?string |
92
|
|
|
{ |
93
|
24 |
|
$this->log('Clearing the path from the trailing character:', $path); |
94
|
|
|
|
95
|
24 |
|
if (! empty($path)) { |
96
|
24 |
|
$chars = '\\/'; |
97
|
|
|
|
98
|
24 |
|
return $both ? trim($path, $chars) : rtrim($path, $chars); |
99
|
|
|
} |
100
|
|
|
|
101
|
19 |
|
return $path; |
102
|
|
|
} |
103
|
|
|
|
104
|
24 |
|
protected function cleanable(...$values): string |
105
|
|
|
{ |
106
|
24 |
|
$this->log('Cleaning values to compile a path:', $values); |
107
|
|
|
|
108
|
24 |
|
foreach ($values as &$value) { |
109
|
24 |
|
$value = $this->clean($value); |
110
|
|
|
} |
111
|
|
|
|
112
|
24 |
|
return implode('/', $values); |
113
|
|
|
} |
114
|
|
|
|
115
|
24 |
|
protected function isEnglish(string $locale): bool |
116
|
|
|
{ |
117
|
24 |
|
$this->log('Check if localization is English: ' . $locale); |
118
|
|
|
|
119
|
24 |
|
return $locale === LocalesList::ENGLISH; |
120
|
|
|
} |
121
|
|
|
|
122
|
24 |
|
protected function getBasePath(): string |
123
|
|
|
{ |
124
|
24 |
|
return ConfigFacade::basePath(); |
125
|
|
|
} |
126
|
|
|
|
127
|
24 |
|
protected function getSourcePath(): string |
128
|
|
|
{ |
129
|
24 |
|
return ConfigFacade::sourcePath(); |
130
|
|
|
} |
131
|
|
|
|
132
|
19 |
|
protected function getLocalesPath(): string |
133
|
|
|
{ |
134
|
19 |
|
return ConfigFacade::localesPath(); |
135
|
|
|
} |
136
|
|
|
|
137
|
24 |
|
protected function getTargetPath(): string |
138
|
|
|
{ |
139
|
24 |
|
return ConfigFacade::resourcesPath(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|