1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Honeybadger\HoneybadgerLaravel\Commands; |
4
|
|
|
|
5
|
|
|
use Honeybadger\Honeybadger; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Illuminate\Support\Facades\Config; |
9
|
|
|
use sixlive\DotenvEditor\DotenvEditor; |
10
|
|
|
use Honeybadger\HoneybadgerLaravel\Concerns\RequiredInput; |
11
|
|
|
|
12
|
|
|
abstract class HoneybadgerInstallCommand extends Command |
13
|
|
|
{ |
14
|
|
|
use RequiredInput; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'honeybadger:install {apiKey?}'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = 'Install and configure Honeybadger'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Results of each step of the install. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $results = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Configuration from gathered input. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $config = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Execute the console command. |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function handle() |
50
|
|
|
{ |
51
|
|
|
$this->config = $this->gatherConfig(); |
52
|
|
|
|
53
|
|
|
$this->writeEnv(); |
54
|
|
|
|
55
|
|
|
if ($this->shouldPublishConfig()) { |
56
|
|
|
$this->task( |
57
|
|
|
'Publish the config file', |
58
|
|
|
$this->publishConfig() |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($this->config['send_test']) { |
63
|
|
|
$this->sendTest(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->outputResults(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Publish the configuration file to the framework. |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
abstract public function publishConfig(); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Prompt for input and gather responses. |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
private function gatherConfig() |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
|
|
'api_key' => $this->argument('apiKey') ?? $this->promptForApiKey(), |
85
|
|
|
'send_test' => $this->confirm('Would you like to send a test exception now?', true), |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Prompt for the API key. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
private function promptForApiKey() |
95
|
|
|
{ |
96
|
|
|
return $this->requiredSecret('Your API key', 'The API key is required'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Send test exception to Honeybadger. |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
private function sendTest() |
105
|
|
|
{ |
106
|
|
|
Config::set('honeybadger.api_key', $this->config['api_key']); |
107
|
|
|
|
108
|
|
|
$this->task( |
109
|
|
|
'Send test exception to Honeybadger', |
110
|
|
|
$this->callSilent('honeybadger:test') === 0 |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Write configuration values to the env files. |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
private function writeEnv() |
120
|
|
|
{ |
121
|
|
|
$this->task( |
122
|
|
|
'Write HONEYBADGER_API_KEY to .env', |
123
|
|
|
$this->writeConfig(['HONEYBADGER_API_KEY' => $this->config['api_key']]) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$this->task( |
127
|
|
|
'Write HONEYBADGER_API_KEY placeholder to .env.example', |
128
|
|
|
$this->writeConfig(['HONEYBADGER_API_KEY' => ''], '.env.example') |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Whether the configuration needs to be published or no. |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
private function shouldPublishConfig() |
138
|
|
|
{ |
139
|
|
|
return ! file_exists(base_path('config/honeybadger.php')); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Write the configurations to dotenv files. |
144
|
|
|
* |
145
|
|
|
* @param array $config |
146
|
|
|
* @param string $file |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
private function writeConfig($config, $file = '.env') |
150
|
|
|
{ |
151
|
|
|
try { |
152
|
|
|
$env = new DotenvEditor; |
153
|
|
|
$env->load(base_path($file)); |
154
|
|
|
} catch (InvalidArgumentException $e) { |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
collect($config)->each(function ($value, $key) use ($env) { |
159
|
|
|
$env->set($key, $value); |
160
|
|
|
}); |
161
|
|
|
|
162
|
|
|
return $env->save(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Add the results of each installation step. |
167
|
|
|
* |
168
|
|
|
* @param string $name |
169
|
|
|
* @param bool $result |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
|
|
public function task($name, $result) |
173
|
|
|
{ |
174
|
|
|
$this->results[$name] = $result; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Output the results of each step of the installation. |
179
|
|
|
* |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
private function outputResults() |
183
|
|
|
{ |
184
|
|
|
collect($this->results)->each(function ($result, $description) { |
185
|
|
|
$this->line(vsprintf('%s: %s', [ |
186
|
|
|
$description, |
187
|
|
|
$result ? '<fg=green>✔</>' : '<fg=red>✘</>', |
188
|
|
|
])); |
189
|
|
|
}); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|