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 array |
25
|
|
|
*/ |
26
|
2 |
|
protected function getArguments() |
27
|
|
|
{ |
28
|
2 |
|
return [new InputArgument('connection', InputArgument::REQUIRED, 'Which connection to use')]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the console command options. |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
2 |
|
protected function getOptions() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
2 |
|
new InputOption( |
40
|
2 |
|
'show', |
41
|
2 |
|
's', |
42
|
2 |
|
InputOption::VALUE_NONE, |
43
|
2 |
|
'Display the sms default connection instead of modifying files' |
44
|
|
|
), |
45
|
2 |
|
new InputOption( |
46
|
2 |
|
'always-no', |
47
|
2 |
|
null, |
48
|
2 |
|
InputOption::VALUE_NONE, |
49
|
2 |
|
'Skip generating sms default connection if it already exists' |
50
|
|
|
), |
51
|
2 |
|
new InputOption( |
52
|
2 |
|
'force', |
53
|
2 |
|
'f', |
54
|
2 |
|
InputOption::VALUE_NONE, |
55
|
2 |
|
'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
|
2 |
|
public function handle(): void |
71
|
|
|
{ |
72
|
2 |
|
$connection = (string) $this->argument('connection'); |
73
|
|
|
|
74
|
2 |
|
if ($this->option('show')) { |
75
|
1 |
|
$this->comment('SMS_CONNECTION=' . $connection); |
76
|
|
|
|
77
|
1 |
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
$path = $this->envPath(); |
81
|
2 |
|
if (! file_exists($path)) { |
82
|
1 |
|
$this->displayConnection($connection); |
83
|
|
|
|
84
|
1 |
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
if ($this->putEnvToFile($connection, $path)) { |
88
|
2 |
|
$this->displayConnection($connection); |
89
|
|
|
} |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* put default sms connection to the .env file path. |
94
|
|
|
* |
95
|
|
|
* @param string $connection the default sms connection |
96
|
|
|
* @param string $path the .env file path. |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
2 |
|
protected function putEnvToFile($connection, $path): bool |
101
|
|
|
{ |
102
|
2 |
|
if (! Str::contains(file_get_contents($path), 'SMS_CONNECTION')) { |
103
|
|
|
// create new entry |
104
|
2 |
|
file_put_contents($path, PHP_EOL . "SMS_CONNECTION={$connection}" . PHP_EOL, FILE_APPEND); |
105
|
2 |
|
} elseif ($this->option('always-no')) { |
106
|
1 |
|
$this->comment('Sms default connection already exists. Skipping...'); |
107
|
|
|
|
108
|
1 |
|
return false; |
109
|
1 |
|
} elseif (! $this->isConfirmed()) { |
110
|
1 |
|
$this->comment('Phew... No changes were made to your sms default connection.'); |
111
|
|
|
|
112
|
1 |
|
return false; |
113
|
|
|
} else { |
114
|
1 |
|
file_put_contents( |
115
|
1 |
|
$path, |
116
|
1 |
|
str_replace( |
117
|
1 |
|
'SMS_CONNECTION=' . $this->laravel['config']['sms.default'], |
118
|
1 |
|
'SMS_CONNECTION=' . $connection, |
119
|
1 |
|
file_get_contents($path) |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
}//end if |
123
|
|
|
|
124
|
2 |
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Display the key. |
129
|
|
|
* |
130
|
|
|
* @param string $connection |
131
|
|
|
*/ |
132
|
2 |
|
protected function displayConnection($connection): void |
133
|
|
|
{ |
134
|
2 |
|
$this->laravel['config']['sms.default'] = $connection; |
135
|
|
|
|
136
|
2 |
|
$this->info("sms default connection switch to [{$connection}] successfully."); |
137
|
2 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Check if the modification is confirmed. |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
1 |
|
protected function isConfirmed() |
145
|
|
|
{ |
146
|
1 |
|
return $this->option('force') ? true : $this->confirm( |
147
|
1 |
|
'This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?' |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the .env file path. |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
2 |
|
protected function envPath() |
157
|
|
|
{ |
158
|
2 |
|
if (method_exists($this->laravel, 'environmentFilePath')) { |
159
|
2 |
|
return $this->laravel->environmentFilePath(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this->laravel->basePath('.env'); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|