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
|
4 |
|
protected function getArguments() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
4 |
|
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
|
4 |
|
protected function getOptions() |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
4 |
|
new InputOption('show', 's', InputOption::VALUE_NONE, 'Display the sms default connection instead of modifying files'), |
42
|
4 |
|
new InputOption('always-no', null, InputOption::VALUE_NONE, 'Skip generating sms default connection if it already exists'), |
43
|
4 |
|
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
|
4 |
|
public function handle() |
60
|
|
|
{ |
61
|
4 |
|
$connection = $this->argument('connection'); |
62
|
|
|
|
63
|
4 |
|
if ($this->option('show')) { |
64
|
2 |
|
$this->comment('SMS_CONNECTION=' . $connection); |
|
|
|
|
65
|
|
|
|
66
|
2 |
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
if (file_exists($path = $this->envPath()) === false) { |
70
|
2 |
|
return $this->displayConnection($connection); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
if (Str::contains(file_get_contents($path), 'SMS_CONNECTION') === false) { |
74
|
|
|
// create new entry |
75
|
4 |
|
file_put_contents($path, PHP_EOL . "SMS_CONNECTION={$connection}" . PHP_EOL, FILE_APPEND); |
76
|
|
|
} else { |
77
|
4 |
|
if ($this->option('always-no')) { |
78
|
2 |
|
$this->comment('Sms default connection already exists. Skipping...'); |
79
|
|
|
|
80
|
2 |
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
if ($this->isConfirmed() === false) { |
84
|
2 |
|
$this->comment('Phew... No changes were made to your sms default connection.'); |
85
|
|
|
|
86
|
2 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// update existing entry |
90
|
2 |
|
file_put_contents( |
91
|
2 |
|
$path, |
92
|
2 |
|
str_replace( |
93
|
2 |
|
'SMS_CONNECTION=' . $this->laravel['config']['sms.default'], |
94
|
2 |
|
'SMS_CONNECTION=' . $connection, |
95
|
2 |
|
file_get_contents($path) |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
}//end if |
99
|
|
|
|
100
|
4 |
|
$this->displayConnection($connection); |
101
|
4 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Display the key. |
105
|
|
|
* |
106
|
|
|
* @param string $key |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
4 |
|
protected function displayConnection($key): void |
111
|
|
|
{ |
112
|
4 |
|
$this->laravel['config']['sms.default'] = $key; |
113
|
|
|
|
114
|
4 |
|
$this->info("sms default connection switch to [{$key}] successfully."); |
115
|
4 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Check if the modification is confirmed. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
2 |
|
protected function isConfirmed() |
123
|
|
|
{ |
124
|
2 |
|
return $this->option('force') ? true : $this->confirm( |
125
|
2 |
|
'This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?' |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the .env file path. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
4 |
|
protected function envPath() |
135
|
|
|
{ |
136
|
4 |
|
if (method_exists($this->laravel, 'environmentFilePath')) { |
137
|
2 |
|
return $this->laravel->environmentFilePath(); |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
return $this->laravel->basePath('.env'); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|