1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Zing\LaravelSms\Commands; |
6
|
|
|
|
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Illuminate\Support\Facades\Config; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
|
13
|
|
|
class SmsSwitchConnectionCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The console command name. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name = 'sms:connection'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Get the console command arguments. |
24
|
|
|
* |
25
|
|
|
* @return \Symfony\Component\Console\Input\InputArgument[] |
26
|
|
|
*/ |
27
|
3 |
|
protected function getArguments(): array |
28
|
|
|
{ |
29
|
3 |
|
return [new InputArgument('connection', InputArgument::REQUIRED, 'Which connection to use')]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the console command options. |
34
|
|
|
* |
35
|
|
|
* @return \Symfony\Component\Console\Input\InputOption[] |
36
|
|
|
*/ |
37
|
3 |
|
protected function getOptions(): array |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
3 |
|
new InputOption( |
41
|
3 |
|
'show', |
42
|
3 |
|
's', |
43
|
|
|
InputOption::VALUE_NONE, |
44
|
3 |
|
'Display the sms default connection instead of modifying files' |
45
|
|
|
), |
46
|
3 |
|
new InputOption( |
47
|
3 |
|
'always-no', |
48
|
|
|
null, |
49
|
|
|
InputOption::VALUE_NONE, |
50
|
3 |
|
'Skip generating sms default connection if it already exists' |
51
|
|
|
), |
52
|
3 |
|
new InputOption( |
53
|
3 |
|
'force', |
54
|
3 |
|
'f', |
55
|
|
|
InputOption::VALUE_NONE, |
56
|
3 |
|
'Skip confirmation when overwriting an existing sms default connection' |
57
|
|
|
), |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The console command description. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $description = 'Set the sms default connection used to send message'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Execute the console command. |
70
|
|
|
*/ |
71
|
3 |
|
public function handle(): void |
72
|
|
|
{ |
73
|
3 |
|
$connection = $this->argument('connection'); |
74
|
3 |
|
if (\is_array($connection)) { |
75
|
1 |
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
if ($connection === null) { |
79
|
1 |
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
if ($this->option('show')) { |
83
|
1 |
|
$this->comment('SMS_CONNECTION=' . $connection); |
84
|
|
|
|
85
|
1 |
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
$path = $this->envPath(); |
89
|
3 |
|
if (! file_exists($path)) { |
90
|
1 |
|
$this->displayConnection($connection); |
91
|
|
|
|
92
|
1 |
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
if ($this->putEnvToFile($connection, $path)) { |
96
|
3 |
|
$this->displayConnection($connection); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* put default sms connection to the .env file path. |
102
|
|
|
* |
103
|
|
|
* @param string $connection the default sms connection |
104
|
|
|
* @param string $path the .env file path. |
105
|
|
|
*/ |
106
|
3 |
|
protected function putEnvToFile(string $connection, string $path): bool |
107
|
|
|
{ |
108
|
|
|
/** @var string $contents */ |
109
|
3 |
|
$contents = file_get_contents($path); |
110
|
|
|
|
111
|
3 |
|
if (! Str::contains($contents, 'SMS_CONNECTION')) { |
112
|
|
|
// create new entry |
113
|
3 |
|
file_put_contents($path, PHP_EOL . sprintf('SMS_CONNECTION=%s', $connection) . PHP_EOL, FILE_APPEND); |
114
|
|
|
|
115
|
3 |
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
if ($this->option('always-no')) { |
119
|
1 |
|
$this->comment('Sms default connection already exists. Skipping...'); |
120
|
|
|
|
121
|
1 |
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
if (! $this->isConfirmed()) { |
125
|
1 |
|
$this->comment('Phew... No changes were made to your sms default connection.'); |
126
|
|
|
|
127
|
1 |
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
file_put_contents( |
131
|
|
|
$path, |
132
|
2 |
|
str_replace('SMS_CONNECTION=' . Config::get('sms.default'), 'SMS_CONNECTION=' . $connection, $contents) |
133
|
|
|
); |
134
|
|
|
|
135
|
2 |
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Display the key. |
140
|
|
|
*/ |
141
|
3 |
|
protected function displayConnection(string $connection): void |
142
|
|
|
{ |
143
|
3 |
|
Config::set('sms.default', $connection); |
144
|
|
|
|
145
|
3 |
|
$this->info(sprintf('sms default connection switch to [%s] successfully.', $connection)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Check if the modification is confirmed. |
150
|
|
|
*/ |
151
|
2 |
|
protected function isConfirmed(): bool |
152
|
|
|
{ |
153
|
2 |
|
if ($this->option('force')) { |
154
|
1 |
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
return $this->confirm( |
158
|
1 |
|
'This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?' |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the .env file path. |
164
|
|
|
*/ |
165
|
3 |
|
protected function envPath(): string |
166
|
|
|
{ |
167
|
3 |
|
if (method_exists($this->laravel, 'environmentFilePath')) { |
168
|
3 |
|
return $this->laravel->environmentFilePath(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->laravel->basePath('.env'); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|