1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Lodash package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Longman\LaravelLodash\SelfDiagnosis\Checks; |
13
|
|
|
|
14
|
|
|
use BeyondCode\SelfDiagnosis\Checks\Check; |
15
|
|
|
use Illuminate\Contracts\Redis\Factory as RedisFactory; |
16
|
|
|
use Illuminate\Redis\Connections\PhpRedisClusterConnection; |
17
|
|
|
use Illuminate\Redis\Connections\PhpRedisConnection; |
18
|
|
|
use Illuminate\Support\Arr; |
19
|
|
|
|
20
|
|
|
class RedisCanBeAccessed implements Check |
21
|
|
|
{ |
22
|
|
|
private $message; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The name of the check. |
26
|
|
|
* |
27
|
|
|
* @param array $config |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function name(array $config): string |
31
|
|
|
{ |
32
|
|
|
return trans('lodash::checks.redis_can_be_accessed.name'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Perform the actual verification of this check. |
37
|
|
|
* |
38
|
|
|
* @param array $config |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function check(array $config): bool |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
if (Arr::get($config, 'default_connection', true)) { |
45
|
|
|
if (! $this->testConnection()) { |
46
|
|
|
$this->message = trans('lodash::checks.redis_can_be_accessed.message.default_cache'); |
47
|
|
|
|
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
foreach (Arr::get($config, 'connections', []) as $connection) { |
53
|
|
|
if (! $this->testConnection($connection)) { |
54
|
|
|
$this->message = trans('lodash::checks.redis_can_be_accessed.message.named_cache', [ |
55
|
|
|
'name' => $connection, |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} catch (\Throwable $e) { |
62
|
|
|
$this->message = $e->getMessage(); |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The error message to display in case the check does not pass. |
72
|
|
|
* |
73
|
|
|
* @param array $config |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function message(array $config): string |
77
|
|
|
{ |
78
|
|
|
return trans('lodash::checks.redis_can_be_accessed.message.not_accessible', [ |
79
|
|
|
'error' => $this->message, |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Tests a redis connection and returns whether the connection is opened or not. |
85
|
|
|
* |
86
|
|
|
* @param string|null $name |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
private function testConnection(?string $name = null): bool |
90
|
|
|
{ |
91
|
|
|
$redis = app(RedisFactory::class); |
92
|
|
|
$connection = $redis->connection($name); |
93
|
|
|
|
94
|
|
|
// PHPRedis connects automatically |
95
|
|
|
if ($connection instanceof PhpRedisConnection || $connection instanceof PhpRedisClusterConnection) { |
96
|
|
|
return ! empty($connection->info()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$connection->connect(); |
100
|
|
|
|
101
|
|
|
return $connection->isConnected(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|