1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Support; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Constants\Locales; |
6
|
|
|
use Helldar\LaravelLangPublisher\Facades\Arr as ArrFacade; |
7
|
|
|
use Helldar\LaravelLangPublisher\Facades\Config; |
|
|
|
|
8
|
|
|
use Helldar\LaravelLangPublisher\Facades\Reflection; |
|
|
|
|
9
|
|
|
use Illuminate\Support\Facades\File; |
|
|
|
|
10
|
|
|
|
11
|
|
|
final class Locale |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* List of available locations. |
15
|
|
|
* |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
36 |
|
public function available(): array |
19
|
|
|
{ |
20
|
36 |
|
return $this->filterLocales($this->availableAll()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Returns a list of all localizations available for installation without filtering. |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
36 |
|
public function availableAll(): array |
29
|
|
|
{ |
30
|
36 |
|
$available = array_values( |
31
|
36 |
|
Reflection::getConstants(Locales::class) |
32
|
|
|
); |
33
|
|
|
|
34
|
36 |
|
$this->addDefaultLocale($available); |
35
|
|
|
|
36
|
36 |
|
return $available; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* List of installed locations. |
41
|
|
|
* |
42
|
|
|
* @param bool $is_json |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
36 |
|
public function installed(bool $is_json = false): array |
47
|
|
|
{ |
48
|
36 |
|
$locales = $this->get($this->getInstalledDirectories($is_json)); |
49
|
36 |
|
$available = $this->available(); |
50
|
|
|
|
51
|
36 |
|
return array_values(array_filter($locales, function ($locale) use ($available) { |
52
|
36 |
|
return in_array($locale, $available); |
53
|
36 |
|
})); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Retrieving a list of protected locales. |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function protects(): array |
62
|
|
|
{ |
63
|
|
|
return ArrFacade::unique([ |
64
|
|
|
$this->getDefault(), |
65
|
|
|
$this->getFallback(), |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The checked locale protecting. |
71
|
|
|
* |
72
|
|
|
* @param string $locale |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
36 |
|
public function isProtected(string $locale): bool |
77
|
|
|
{ |
78
|
36 |
|
return $locale === $this->getDefault() || $locale === $this->getFallback(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Checks if a language pack is installed. |
83
|
|
|
* |
84
|
|
|
* @param string $locale |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function isAvailable(string $locale): bool |
89
|
|
|
{ |
90
|
|
|
return in_array($locale, $this->available(), true); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Checks whether it is possible to install the language pack. |
95
|
|
|
* |
96
|
|
|
* @param string $locale |
97
|
|
|
* @param bool $is_json |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function isInstalled(string $locale, bool $is_json = false): bool |
102
|
|
|
{ |
103
|
|
|
return in_array($locale, $this->installed($is_json), true); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Getting the default localization name. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
36 |
|
public function getDefault(): string |
112
|
|
|
{ |
113
|
36 |
|
return Config::getLocale(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Getting the fallback localization name. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
11 |
|
public function getFallback(): string |
122
|
|
|
{ |
123
|
11 |
|
return Config::getFallbackLocale(); |
124
|
|
|
} |
125
|
|
|
|
126
|
36 |
|
protected function get(array $directories): array |
127
|
|
|
{ |
128
|
36 |
|
$locales = $this->normalizeNames($directories); |
129
|
|
|
|
130
|
36 |
|
$this->addDefaultLocale($locales); |
131
|
|
|
|
132
|
36 |
|
return $this->filterLocales($locales); |
133
|
|
|
} |
134
|
|
|
|
135
|
36 |
|
protected function getInstalledDirectories(bool $is_json = false): array |
136
|
|
|
{ |
137
|
36 |
|
return $is_json |
138
|
23 |
|
? File::files($this->getResourcePath()) |
139
|
36 |
|
: File::directories($this->getResourcePath()); |
140
|
|
|
} |
141
|
|
|
|
142
|
36 |
|
protected function normalizeNames(array $directories): array |
143
|
|
|
{ |
144
|
36 |
|
return array_map(function ($dir) { |
145
|
35 |
|
return File::name($dir); |
146
|
36 |
|
}, $directories); |
147
|
|
|
} |
148
|
|
|
|
149
|
36 |
|
protected function addDefaultLocale(array &$locales): void |
150
|
|
|
{ |
151
|
36 |
|
array_push($locales, Config::getLocale()); |
152
|
36 |
|
} |
153
|
|
|
|
154
|
36 |
|
protected function filterLocales(array $locales): array |
155
|
|
|
{ |
156
|
36 |
|
$unique = ArrFacade::unique($locales); |
157
|
36 |
|
$ignore = Config::getIgnore(); |
158
|
|
|
|
159
|
36 |
|
return array_values(array_filter($unique, static function ($locale) use ($ignore) { |
160
|
36 |
|
return ! in_array($locale, $ignore); |
161
|
36 |
|
})); |
162
|
|
|
} |
163
|
|
|
|
164
|
36 |
|
protected function getResourcePath(): string |
165
|
|
|
{ |
166
|
36 |
|
return resource_path('lang'); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: