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