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\HoneybadgerLaravel\CommandTasks; |
9
|
|
|
use Honeybadger\HoneybadgerLaravel\Contracts\Installer; |
10
|
|
|
use Honeybadger\HoneybadgerLaravel\Concerns\RequiredInput; |
11
|
|
|
|
12
|
|
|
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
|
|
|
* Configuration from gathered input. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $config = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \Honeybadger\HoneybadgerLaravel\Contracts\Installer; |
39
|
|
|
*/ |
40
|
|
|
protected $installer; |
41
|
|
|
|
42
|
|
|
protected $tasks; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Execute the console command. |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function handle(Installer $installer, CommandTasks $commandTasks) |
50
|
|
|
{ |
51
|
|
|
$this->installer = $installer; |
52
|
|
|
$this->tasks = $commandTasks; |
53
|
|
|
$this->tasks->setOutput($this->output); |
54
|
|
|
|
55
|
|
|
$this->config = $this->gatherConfig(); |
56
|
|
|
|
57
|
|
|
$this->writeEnv(); |
58
|
|
|
|
59
|
|
|
if ($this->shouldPublishConfig()) { |
60
|
|
|
$this->tasks->addTask( |
61
|
|
|
'Publish the config file', |
62
|
|
|
$this->publishConfig() |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->config['send_test']) { |
67
|
|
|
$this->sendTest(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->tasks->outputResults(); |
71
|
|
|
|
72
|
|
|
$this->outputSuccessMessage(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Publish the configuration file to the framework. |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function publishConfig() |
81
|
|
|
{ |
82
|
|
|
if(app('honeybadger.isLumen')) { |
83
|
|
|
return $this->installer->publishLumenConfig(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->installer->publishLaravelConfig(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Prompt for input and gather responses. |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
private function gatherConfig() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
'api_key' => $this->argument('apiKey') ?? $this->promptForApiKey(), |
98
|
|
|
'send_test' => $this->confirm('Would you like to send a test exception now?', true), |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Prompt for the API key. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
private function promptForApiKey() |
108
|
|
|
{ |
109
|
|
|
return $this->requiredSecret('Your API key', 'The API key is required'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Send test exception to Honeybadger. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
private function sendTest() |
118
|
|
|
{ |
119
|
|
|
Config::set('honeybadger.api_key', $this->config['api_key']); |
120
|
|
|
|
121
|
|
|
$result = $this->installer->sendTestException(); |
122
|
|
|
|
123
|
|
|
$this->tasks->addTask( |
124
|
|
|
'Send test exception to Honeybadger', |
125
|
|
|
! empty($result) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Write configuration values to the env files. |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
private function writeEnv() |
135
|
|
|
{ |
136
|
|
|
$this->tasks->addTask( |
137
|
|
|
'Write HONEYBADGER_API_KEY to .env', |
138
|
|
|
$this->installer->writeConfig( |
139
|
|
|
['HONEYBADGER_API_KEY' => $this->config['api_key']], |
140
|
|
|
base_path('.env') |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$this->tasks->addTask( |
145
|
|
|
'Write HONEYBADGER_API_KEY placeholder to .env.example', |
146
|
|
|
$this->installer->writeConfig( |
147
|
|
|
['HONEYBADGER_API_KEY' => ''], |
148
|
|
|
base_path('.env.example') |
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Whether the configuration needs to be published or no. |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
private function shouldPublishConfig() |
159
|
|
|
{ |
160
|
|
|
return ! file_exists(base_path('config/honeybadger.php')); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function outputSuccessMessage() |
164
|
|
|
{ |
165
|
|
|
$message = <<<'EOT' |
166
|
|
|
⚡ --- Honeybadger is installed! ----------------------------------------------- |
167
|
|
|
Good news: You're one deploy away from seeing all of your exceptions in |
168
|
|
|
Honeybadger. For now, we've generated a test exception for you: |
169
|
|
|
|
170
|
|
|
#{notice_url} |
171
|
|
|
|
172
|
|
|
If you ever need help: |
173
|
|
|
|
174
|
|
|
- Check out the documentation: https://docs.honeybadger.io/lib/php/index.html |
175
|
|
|
- Email the 'badgers: [email protected] |
176
|
|
|
|
177
|
|
|
Most people don't realize that Honeybadger is a small, bootstrapped company. We |
178
|
|
|
really couldn't do this without you. Thank you for allowing us to do what we |
179
|
|
|
love: making developers awesome. |
180
|
|
|
|
181
|
|
|
Happy 'badgering! |
182
|
|
|
|
183
|
|
|
Sincerely, |
184
|
|
|
Ben, Josh and Starr |
185
|
|
|
https://www.honeybadger.io/about/ |
186
|
|
|
⚡ --- End -------------------------------------------------------------------- |
187
|
|
|
EOT; |
188
|
|
|
$this->line($message); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|