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