|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Honeybadger\HoneybadgerLaravel\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Honeybadger\Honeybadger; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Support\Facades\Config; |
|
8
|
|
|
use Honeybadger\Exceptions\ServiceException; |
|
9
|
|
|
use Honeybadger\HoneybadgerLaravel\CommandTasks; |
|
10
|
|
|
use Honeybadger\HoneybadgerLaravel\Contracts\Installer; |
|
11
|
|
|
use Honeybadger\HoneybadgerLaravel\Exceptions\TaskFailed; |
|
12
|
|
|
use Honeybadger\HoneybadgerLaravel\Concerns\RequiredInput; |
|
13
|
|
|
|
|
14
|
|
|
class HoneybadgerInstallCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
use RequiredInput; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The name and signature of the console command. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $signature = 'honeybadger:install {apiKey?}'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The console command description. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $description = 'Install and configure Honeybadger'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Configuration from gathered input. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $config = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \Honeybadger\HoneybadgerLaravel\Contracts\Installer; |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $installer; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var \Honeybadger\HoneybadgerLaravel\CommandTasks |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $tasks; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Execute the console command. |
|
51
|
|
|
* |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public function handle(Installer $installer, CommandTasks $commandTasks) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->installer = $installer; |
|
57
|
|
|
$this->tasks = $commandTasks; |
|
58
|
|
|
$this->tasks->setOutput($this->output); |
|
59
|
|
|
|
|
60
|
|
|
$this->config = $this->gatherConfig(); |
|
61
|
|
|
|
|
62
|
|
|
$this->writeEnv(); |
|
63
|
|
|
|
|
64
|
|
|
if ($this->installer->shouldPublishConfig()) { |
|
65
|
|
|
$this->tasks->addTask( |
|
66
|
|
|
'Publish the config file', |
|
67
|
|
|
function () { |
|
68
|
|
|
return $this->publishConfig(); |
|
69
|
|
|
} |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$results = $this->sendTest(); |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
|
|
$this->tasks->runTasks(); |
|
77
|
|
|
$this->outputSuccessMessage(array_get($results ?? [], 'id', '')); |
|
78
|
|
|
} catch (TaskFailed $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
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
private function sendTest() : array |
|
112
|
|
|
{ |
|
113
|
|
|
Config::set('honeybadger.api_key', $this->config['api_key']); |
|
114
|
|
|
|
|
115
|
|
|
try { |
|
116
|
|
|
$result = $this->installer->sendTestException(); |
|
117
|
|
|
} catch (ServiceException $e) { |
|
118
|
|
|
$result = []; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$this->tasks->addTask( |
|
122
|
|
|
'Send test exception to Honeybadger', |
|
123
|
|
|
function () use ($result) { |
|
124
|
|
|
return ! empty($result); |
|
125
|
|
|
} |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
return $result; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Write configuration values to the env files. |
|
133
|
|
|
* |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
|
|
private function writeEnv() : void |
|
137
|
|
|
{ |
|
138
|
|
|
$this->tasks->addTask( |
|
139
|
|
|
'Write HONEYBADGER_API_KEY to .env', |
|
140
|
|
|
function () { |
|
141
|
|
|
return $this->installer->writeConfig( |
|
142
|
|
|
['HONEYBADGER_API_KEY' => $this->config['api_key']], |
|
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
|
|
|
['HONEYBADGER_API_KEY' => ''], |
|
153
|
|
|
base_path('.env.example') |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Publish the config file for Lumen or Laravel. |
|
161
|
|
|
* |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
|
|
public function publishConfig() : bool |
|
165
|
|
|
{ |
|
166
|
|
|
if (app('honeybadger.isLumen')) { |
|
167
|
|
|
return $this->installer->publishLumenConfig(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $this->installer->publishLaravelConfig(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Output the success message. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $noticeId |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
private function outputSuccessMessage(string $noticeId) : void |
|
180
|
|
|
{ |
|
181
|
|
|
$this->line(SuccessMessage::make($noticeId)); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|