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