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