1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Support; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Logger; |
6
|
|
|
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; |
7
|
|
|
use Helldar\LaravelLangPublisher\Facades\Arr as ArrFacade; |
8
|
|
|
use Helldar\LaravelLangPublisher\Facades\Config as ConfigFacade; |
9
|
|
|
use Helldar\LaravelLangPublisher\Facades\Reflection as ReflectionFacade; |
10
|
|
|
use Helldar\Support\Facades\Helpers\Arr; |
|
|
|
|
11
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\Directory; |
12
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\File; |
13
|
|
|
use Helldar\Support\Facades\Helpers\Str; |
14
|
|
|
|
15
|
|
|
final class Locales |
16
|
|
|
{ |
17
|
|
|
use Logger; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* List of available locations. |
21
|
|
|
* |
22
|
|
|
* @param bool $all |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
16 |
|
public function available(bool $all = false): array |
27
|
|
|
{ |
28
|
16 |
|
$this->log('Getting list of available locations...'); |
29
|
|
|
|
30
|
16 |
|
$locales = $this->all(); |
31
|
|
|
|
32
|
16 |
|
return $all ? $locales : $this->filter($locales); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* List of installed locations. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
7 |
|
public function installed(): array |
41
|
|
|
{ |
42
|
7 |
|
$this->log('Getting list of installed locations...'); |
43
|
|
|
|
44
|
7 |
|
$json = $this->findJsonFiles(); |
45
|
7 |
|
$php = $this->findPhpFiles(); |
46
|
|
|
|
47
|
7 |
|
$installed = ArrFacade::unique(array_merge($json, $php)); |
48
|
|
|
|
49
|
7 |
|
$sorted = Arr::sort($installed); |
50
|
|
|
|
51
|
7 |
|
return array_values($sorted); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Retrieving a list of protected locales. |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function protects(): array |
60
|
|
|
{ |
61
|
|
|
$this->log('Retrieving a list of protected locales...'); |
62
|
|
|
|
63
|
|
|
return ArrFacade::unique([ |
64
|
|
|
$this->getDefault(), |
65
|
|
|
$this->getFallback(), |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Checks if a language pack is installed. |
71
|
|
|
* |
72
|
|
|
* @param string $locale |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
13 |
|
public function isAvailable(string $locale): bool |
77
|
|
|
{ |
78
|
13 |
|
$this->log('Checks if a language pack is installed...'); |
79
|
|
|
|
80
|
13 |
|
return in_array($locale, $this->available(true), true); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The checked locale protecting. |
85
|
|
|
* |
86
|
|
|
* @param string $locale |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
4 |
|
public function isProtected(string $locale): bool |
91
|
|
|
{ |
92
|
4 |
|
$this->log('The checked locale protecting...'); |
93
|
|
|
|
94
|
4 |
|
return $locale === $this->getDefault() || $locale === $this->getFallback(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Checks whether it is possible to install the language pack. |
99
|
|
|
* |
100
|
|
|
* @param string $locale |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function isInstalled(string $locale): bool |
105
|
|
|
{ |
106
|
|
|
$this->log('Checks whether it is possible to install the language pack...'); |
107
|
|
|
|
108
|
|
|
return in_array($locale, $this->installed(), true); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Getting the default localization name. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
4 |
|
public function getDefault(): string |
117
|
|
|
{ |
118
|
4 |
|
$this->log('Getting the default localization name...'); |
119
|
|
|
|
120
|
4 |
|
return ConfigFacade::defaultLocale(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Getting the fallback localization name. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
3 |
|
public function getFallback(): string |
129
|
|
|
{ |
130
|
3 |
|
$this->log('Getting the fallback localization name...'); |
131
|
|
|
|
132
|
3 |
|
return ConfigFacade::fallbackLocale(); |
133
|
|
|
} |
134
|
|
|
|
135
|
16 |
|
protected function all(): array |
136
|
|
|
{ |
137
|
16 |
|
$this->log('Getting a list of all available localizations without filtering...'); |
138
|
|
|
|
139
|
16 |
|
return array_values(ReflectionFacade::getConstants(LocalesList::class)); |
140
|
|
|
} |
141
|
|
|
|
142
|
10 |
|
protected function filter(array $locales): array |
143
|
|
|
{ |
144
|
10 |
|
$this->log('Filtering localizations...'); |
145
|
|
|
|
146
|
10 |
|
$unique = ArrFacade::unique($locales); |
147
|
10 |
|
$ignore = ConfigFacade::ignores(); |
148
|
|
|
|
149
|
10 |
|
return array_values(array_filter($unique, static function ($locale) use ($ignore) { |
150
|
10 |
|
return ! in_array($locale, $ignore, true); |
151
|
10 |
|
})); |
152
|
|
|
} |
153
|
|
|
|
154
|
7 |
|
protected function findJsonFiles(): array |
155
|
|
|
{ |
156
|
7 |
|
$this->log('Getting a list of localizations from json files...'); |
157
|
|
|
|
158
|
7 |
|
$files = File::names($this->resourcesPath()); |
159
|
|
|
|
160
|
7 |
|
return Arr::map($files, static function ($filename) { |
161
|
7 |
|
return Str::before($filename, '.'); |
162
|
7 |
|
}); |
163
|
|
|
} |
164
|
|
|
|
165
|
7 |
|
protected function findPhpFiles(): array |
166
|
|
|
{ |
167
|
7 |
|
$this->log('Getting a list of localizations from php files...'); |
168
|
|
|
|
169
|
7 |
|
return Directory::names($this->resourcesPath()); |
170
|
|
|
} |
171
|
|
|
|
172
|
7 |
|
protected function resourcesPath(): string |
173
|
|
|
{ |
174
|
7 |
|
$this->log('Getting the path to application resources...'); |
175
|
|
|
|
176
|
7 |
|
return ConfigFacade::resourcesPath(); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
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: