forked from beyondcode/laravel-self-diagnosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalesAreInstalled.php
94 lines (78 loc) · 2.56 KB
/
LocalesAreInstalled.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace BeyondCode\SelfDiagnosis\Checks;
use BeyondCode\SelfDiagnosis\SystemFunctions;
use Illuminate\Support\Collection;
class LocalesAreInstalled implements Check
{
/** @var Collection */
protected $missingLocales;
/** @var string|null */
protected $message;
/** @var SystemFunctions */
protected $systemFunctions;
/**
* LocalesAreInstalled constructor.
*
* @param SystemFunctions $systemFunctions
*/
public function __construct(SystemFunctions $systemFunctions)
{
$this->systemFunctions = $systemFunctions;
}
/**
* The name of the check.
*
* @param array $config
* @return string
*/
public function name(array $config): string
{
return trans('self-diagnosis::checks.locales_are_installed.name');
}
/**
* Perform the actual verification of this check.
*
* @param array $config
* @return bool
*/
public function check(array $config): bool
{
$this->missingLocales = new Collection(array_get($config, 'required_locales', []));
if ($this->missingLocales->isEmpty()) {
return true;
}
if (!$this->systemFunctions->isFunctionAvailable('shell_exec')) {
$this->message = trans('self-diagnosis::checks.locales_are_installed.message.shell_exec_not_available');
return false;
}
if ($this->systemFunctions->isWindowsOperatingSystem()) {
$this->message = trans('self-diagnosis::checks.locales_are_installed.message.cannot_run_on_windows');
return false;
}
$locales = $this->systemFunctions->callShellExec('locale -a');
if ($locales === null || $locales === '') {
$this->message = trans('self-diagnosis::checks.locales_are_installed.message.locale_command_not_available');
return false;
}
$locales = explode("\n" , $locales);
$this->missingLocales = $this->missingLocales->reject(function ($loc) use ($locales) {
return in_array($loc, $locales);
});
return $this->missingLocales->isEmpty();
}
/**
* The error message to display in case the check does not pass.
*
* @param array $config
* @return string
*/
public function message(array $config): string
{
if ($this->message) {
return $this->message;
}
return trans('self-diagnosis::checks.locales_are_installed.message.missing_locales', [
'locales' => $this->missingLocales->implode(PHP_EOL),
]);
}
}