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