1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Support; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Facades\Arr as ArrFacade; |
6
|
|
|
use Helldar\LaravelLangPublisher\Facades\Config; |
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\File; |
|
|
|
|
8
|
|
|
|
9
|
|
|
final class Locale |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* List of available locations. |
13
|
|
|
* |
14
|
|
|
* @param bool $is_json |
15
|
|
|
* |
16
|
12 |
|
* @return array |
17
|
|
|
*/ |
18
|
12 |
|
public function available(bool $is_json = false): array |
19
|
|
|
{ |
20
|
|
|
return $this->get($this->getSourceDirectories($is_json)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* List of installed locations. |
25
|
|
|
* |
26
|
|
|
* @param bool $is_json |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function installed(bool $is_json = false): array |
31
|
|
|
{ |
32
|
|
|
$locales = $this->get($this->getInstalledDirectories($is_json)); |
33
|
|
|
$available = $this->available($is_json); |
34
|
|
|
|
35
|
|
|
return array_values(array_filter($locales, function ($locale) use ($available) { |
36
|
|
|
return in_array($locale, $available); |
37
|
|
|
})); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieving a list of protected locales. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function protects(): array |
46
|
|
|
{ |
47
|
|
|
return ArrFacade::unique([ |
48
|
|
|
$this->getDefault(), |
49
|
|
|
$this->getFallback(), |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The checked locale protecting. |
55
|
|
|
* |
56
|
|
|
* @param string $locale |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function isProtected(string $locale): bool |
61
|
|
|
{ |
62
|
|
|
return $locale === $this->getDefault() || $locale === $this->getFallback(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Checks if a language pack is installed. |
67
|
|
|
* |
68
|
|
|
* @param string $locale |
69
|
|
|
* @param bool $is_json |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function isAvailable(string $locale, bool $is_json = false): bool |
74
|
|
|
{ |
75
|
|
|
return in_array($locale, $this->available($is_json), true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Checks whether it is possible to install the language pack. |
80
|
18 |
|
* |
81
|
|
|
* @param string $locale |
82
|
18 |
|
* @param bool $is_json |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isInstalled(string $locale, bool $is_json = false): bool |
87
|
|
|
{ |
88
|
|
|
return in_array($locale, $this->installed($is_json), true); |
89
|
|
|
} |
90
|
18 |
|
|
91
|
|
|
/** |
92
|
18 |
|
* Getting the default localization name. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getDefault(): string |
97
|
|
|
{ |
98
|
|
|
return Config::getLocale(); |
99
|
|
|
} |
100
|
6 |
|
|
101
|
|
|
/** |
102
|
6 |
|
* Getting the fallback localization name. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
12 |
|
*/ |
106
|
|
|
public function getFallback(): string |
107
|
12 |
|
{ |
108
|
|
|
return Config::getFallbackLocale(); |
109
|
12 |
|
} |
110
|
|
|
|
111
|
12 |
|
protected function get(array $directories): array |
112
|
|
|
{ |
113
|
|
|
$locales = $this->normalizeNames($directories); |
114
|
12 |
|
|
115
|
|
|
$this->addDefaultLocale($locales); |
116
|
12 |
|
|
117
|
|
|
return $this->filterLocales($locales); |
118
|
12 |
|
} |
119
|
6 |
|
|
120
|
12 |
|
protected function getSourceDirectories(bool $is_json = false): array |
121
|
|
|
{ |
122
|
|
|
$vendor = Config::getVendorPath(); |
123
|
|
|
|
124
|
|
|
return $is_json |
125
|
|
|
? File::files($vendor . '/json') |
126
|
|
|
: File::directories($vendor . '/src'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function getInstalledDirectories(bool $is_json = false): array |
130
|
|
|
{ |
131
|
|
|
return $is_json |
132
|
12 |
|
? File::files($this->getResourcePath()) |
133
|
|
|
: File::directories($this->getResourcePath()); |
134
|
|
|
} |
135
|
12 |
|
|
136
|
12 |
|
protected function normalizeNames(array $directories): array |
137
|
|
|
{ |
138
|
|
|
return array_map(function ($dir) { |
139
|
12 |
|
return File::name($dir); |
140
|
|
|
}, $directories); |
141
|
12 |
|
} |
142
|
12 |
|
|
143
|
|
|
protected function addDefaultLocale(array &$locales): void |
144
|
12 |
|
{ |
145
|
|
|
array_push($locales, Config::getLocale()); |
146
|
12 |
|
} |
147
|
|
|
|
148
|
|
|
protected function filterLocales(array $locales): array |
149
|
|
|
{ |
150
|
|
|
return ArrFacade::unique($locales); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function getResourcePath(): string |
154
|
|
|
{ |
155
|
|
|
return resource_path('lang'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
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: