|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the "andrey-helldar/laravel-lang-publisher" project. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Andrey Helldar <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2021 Andrey Helldar |
|
12
|
|
|
* |
|
13
|
|
|
* @license MIT |
|
14
|
|
|
* |
|
15
|
|
|
* @see https://github.com/andrey-helldar/laravel-lang-publisher |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
declare(strict_types=1); |
|
19
|
|
|
|
|
20
|
|
|
namespace Helldar\LaravelLangPublisher\Helpers; |
|
21
|
|
|
|
|
22
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Has; |
|
23
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Paths; |
|
24
|
|
|
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; |
|
25
|
|
|
use Helldar\LaravelLangPublisher\Facades\Helpers\Config as ConfigHelper; |
|
26
|
|
|
use Helldar\Support\Facades\Helpers\Ables\Arrayable; |
|
27
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\Directory; |
|
28
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\File; |
|
29
|
|
|
use Helldar\Support\Facades\Helpers\Reflection; |
|
30
|
|
|
use Illuminate\Support\Facades\Config as Illuminate; |
|
31
|
|
|
|
|
32
|
|
|
class Locales |
|
33
|
|
|
{ |
|
34
|
|
|
use Has; |
|
35
|
|
|
use Paths; |
|
36
|
|
|
|
|
37
|
|
|
public function available(): array |
|
38
|
|
|
{ |
|
39
|
|
|
$locales = Reflection::getConstants(LocalesList::class); |
|
40
|
|
|
|
|
41
|
|
|
return Arrayable::of($locales) |
|
42
|
|
|
->unique() |
|
43
|
|
|
->sort() |
|
44
|
|
|
->values() |
|
45
|
|
|
->get(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function installed(): array |
|
49
|
|
|
{ |
|
50
|
|
|
return Arrayable::of($this->findJson()) |
|
51
|
|
|
->addUnique($this->findPhp()) |
|
52
|
|
|
->filter(function (string $locale) { |
|
53
|
|
|
return $this->isAvailable($locale); |
|
54
|
|
|
}) |
|
55
|
|
|
->unique() |
|
56
|
|
|
->sort() |
|
57
|
|
|
->values() |
|
58
|
|
|
->get(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function protects(): array |
|
62
|
|
|
{ |
|
63
|
|
|
return Arrayable::of([ |
|
64
|
|
|
$this->getDefault(), |
|
65
|
|
|
$this->getFallback(), |
|
66
|
|
|
])->unique()->get(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function isAvailable(string $locale): bool |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->in($locale, $this->available()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function isProtected(string $locale): bool |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->in($locale, $this->protects()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function isInstalled(string $locale): bool |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->in($locale, $this->installed()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getDefault(): string |
|
85
|
|
|
{ |
|
86
|
|
|
return Illuminate::get('app.locale') ?: $this->getFallback(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getFallback(): string |
|
90
|
|
|
{ |
|
91
|
|
|
return Illuminate::get('app.fallback_locale', LocalesList::ENGLISH); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function in(string $locale, array $locales): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return in_array($locale, $locales, true); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function findJson(): array |
|
100
|
|
|
{ |
|
101
|
|
|
$files = File::names($this->resources(), null, true); |
|
102
|
|
|
|
|
103
|
|
|
return Arrayable::of($files) |
|
104
|
|
|
->filter(function (string $filename) { |
|
105
|
|
|
return $this->hasJson($filename); |
|
106
|
|
|
}) |
|
107
|
|
|
->map(function (string $filename) { |
|
108
|
|
|
return $this->filename($filename); |
|
109
|
|
|
}) |
|
110
|
|
|
->values() |
|
111
|
|
|
->get(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function findPhp(): array |
|
115
|
|
|
{ |
|
116
|
|
|
return Directory::names($this->resources()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
protected function resources(): string |
|
120
|
|
|
{ |
|
121
|
|
|
return ConfigHelper::resources(); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|