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