1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Honeybadger\HoneybadgerLaravel\Commands; |
4
|
|
|
|
5
|
|
|
use Honeybadger\HoneybadgerLaravel\CommandTasks; |
6
|
|
|
use Honeybadger\HoneybadgerLaravel\Concerns\RequiredInput; |
7
|
|
|
use Honeybadger\HoneybadgerLaravel\Contracts\Installer; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
use Illuminate\Support\Facades\Config; |
11
|
|
|
use Throwable; |
12
|
|
|
|
13
|
|
|
class HoneybadgerInstallCommand extends Command |
14
|
|
|
{ |
15
|
|
|
use RequiredInput; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The name and signature of the console command. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'honeybadger:install {apiKey?}'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Install and configure Honeybadger'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Configuration from gathered input. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $config = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Honeybadger\HoneybadgerLaravel\Contracts\Installer; |
40
|
|
|
*/ |
41
|
|
|
protected $installer; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \Honeybadger\HoneybadgerLaravel\CommandTasks |
45
|
|
|
*/ |
46
|
|
|
protected $tasks; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Execute the console command. |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function handle(Installer $installer, CommandTasks $commandTasks) |
54
|
|
|
{ |
55
|
|
|
$this->installer = $installer; |
56
|
|
|
$this->tasks = $commandTasks; |
57
|
|
|
$this->tasks->setOutput($this->output); |
58
|
|
|
|
59
|
|
|
$this->config = $this->gatherConfig(); |
60
|
|
|
|
61
|
|
|
$this->writeEnv(); |
62
|
|
|
|
63
|
|
|
if ($this->installer->shouldPublishConfig()) { |
64
|
|
|
$this->tasks->addTask( |
65
|
|
|
'Publish the config file', |
66
|
|
|
function () { |
67
|
|
|
return $this->publishConfig(); |
68
|
|
|
} |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->addTestExceptionTask(); |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$this->tasks->runTasks(); |
76
|
|
|
$testExceptionResult = $this->tasks->getResults()['Send test exception to Honeybadger']; |
77
|
|
|
$this->outputSuccessMessage(Arr::get($testExceptionResult, 'id', '')); |
78
|
|
|
} catch (Throwable $e) { |
79
|
|
|
$this->line(''); |
80
|
|
|
$this->error($e->getMessage()); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Prompt for input and gather responses. |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
private function gatherConfig(): array |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
'api_key' => $this->argument('apiKey') ?? $this->promptForApiKey(), |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Prompt for the API key. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
private function promptForApiKey(): string |
102
|
|
|
{ |
103
|
|
|
return $this->requiredSecret('Your API key', 'The API key is required'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Send test exception to Honeybadger. |
108
|
|
|
*/ |
109
|
|
|
private function addTestExceptionTask(): void |
110
|
|
|
{ |
111
|
|
|
Config::set('honeybadger.api_key', $this->config['api_key']); |
112
|
|
|
|
113
|
|
|
$this->tasks->addTask( |
114
|
|
|
'Send test exception to Honeybadger', |
115
|
|
|
function () { |
116
|
|
|
if (! config('honeybadger.report_data')) { |
117
|
|
|
$this->info("You have `report_data` set to false in your config. Errors won't be reported in this environment."); |
118
|
|
|
$this->info("We've switched it to true for this test, but you should check that it's enabled for your production environments."); |
119
|
|
|
} |
120
|
|
|
$result = $this->installer->sendTestException(); |
121
|
|
|
|
122
|
|
|
return empty($result) ? false : $result; |
123
|
|
|
}, |
124
|
|
|
true |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Write configuration values to the env files. |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
private function writeEnv(): void |
134
|
|
|
{ |
135
|
|
|
$this->tasks->addTask( |
136
|
|
|
'Write HONEYBADGER_API_KEY to .env', |
137
|
|
|
function () { |
138
|
|
|
return $this->installer->writeConfig( |
139
|
|
|
[ |
140
|
|
|
'HONEYBADGER_API_KEY' => $this->config['api_key'], |
141
|
|
|
'HONEYBADGER_VERIFY_SSL' => 'true', |
142
|
|
|
], |
143
|
|
|
base_path('.env') |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$this->tasks->addTask( |
149
|
|
|
'Write HONEYBADGER_API_KEY placeholder to .env.example', |
150
|
|
|
function () { |
151
|
|
|
return $this->installer->writeConfig( |
152
|
|
|
[ |
153
|
|
|
'HONEYBADGER_API_KEY' => '', |
154
|
|
|
'HONEYBADGER_VERIFY_SSL' => 'true', |
155
|
|
|
], |
156
|
|
|
base_path('.env.example') |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
$this->tasks->addTask( |
162
|
|
|
'Write HONEYBADGER_VERIFY_SSL placeholder to .env.example', |
163
|
|
|
function () { |
164
|
|
|
return $this->installer->writeConfig( |
165
|
|
|
['HONEYBADGER_VERIFY_SSL' => ''], |
166
|
|
|
base_path('.env.example') |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Publish the config file for Lumen or Laravel. |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function publishConfig(): bool |
178
|
|
|
{ |
179
|
|
|
if (app('honeybadger.isLumen')) { |
180
|
|
|
return $this->installer->publishLumenConfig(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $this->installer->publishLaravelConfig(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Output the success message. |
188
|
|
|
* |
189
|
|
|
* @param string $noticeId |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
private function outputSuccessMessage(string $noticeId): void |
193
|
|
|
{ |
194
|
|
|
$this->line(SuccessMessage::make($noticeId)); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|