1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Longman\LaravelLodash\SelfDiagnosis\Checks; |
6
|
|
|
|
7
|
|
|
use BeyondCode\SelfDiagnosis\Checks\Check; |
8
|
|
|
use Illuminate\Filesystem\FilesystemManager; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use Throwable; |
11
|
|
|
|
12
|
|
|
use function app; |
13
|
|
|
use function count; |
14
|
|
|
use function implode; |
15
|
|
|
use function now; |
16
|
|
|
use function trans; |
17
|
|
|
|
18
|
|
|
use const PHP_EOL; |
19
|
|
|
|
20
|
|
|
class FilesystemsAreAvailable implements Check |
21
|
|
|
{ |
22
|
|
|
/** @var \Illuminate\Filesystem\FilesystemManager */ |
23
|
|
|
private $filesystemManager; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
private $options = []; |
27
|
|
|
|
28
|
|
|
public function name(array $config): string |
29
|
|
|
{ |
30
|
|
|
return trans('lodash::checks.filesystems_are_available.name'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function check(array $config): bool |
34
|
|
|
{ |
35
|
|
|
$this->filesystemManager = app(FilesystemManager::class); |
36
|
|
|
|
37
|
|
|
foreach ($config['disks'] as $disk) { |
38
|
|
|
try { |
39
|
|
|
$status = $this->checkDisk($disk); |
40
|
|
|
} catch (Throwable $e) { |
41
|
|
|
$this->options[$disk] = $e->getMessage(); |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (! $status) { |
46
|
|
|
$this->options[$disk] = false; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return count($this->options) === 0; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function message(array $config): string |
54
|
|
|
{ |
55
|
|
|
$options = []; |
56
|
|
|
foreach ($this->options as $option => $value) { |
57
|
|
|
$message = 'Disk "' . $option . '" is not available'; |
58
|
|
|
if (! empty($value)) { |
59
|
|
|
$message .= '. Reason: ' . $value; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$options[] = $message; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return trans('lodash::checks.filesystems_are_available.message', [ |
66
|
|
|
'options' => implode(PHP_EOL, $options), |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function checkDisk(string $disk): bool |
71
|
|
|
{ |
72
|
|
|
/** @var \Illuminate\Filesystem\FilesystemAdapter $diskInstance */ |
73
|
|
|
$diskInstance = $this->filesystemManager->disk($disk); |
74
|
|
|
|
75
|
|
|
$file = 'check_' . Str::random(32); |
76
|
|
|
|
77
|
|
|
$status = $diskInstance->put($file, now()->toString()); |
78
|
|
|
if (! $status) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$status = $diskInstance->delete($file); |
83
|
|
|
if (! $status) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $status; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|