1 | <?php |
||
2 | |||
3 | namespace Honeybadger\HoneybadgerLaravel; |
||
4 | |||
5 | use GuzzleHttp\Client; |
||
6 | use Honeybadger\CheckInsManager; |
||
7 | use Honeybadger\Contracts\SyncCheckIns; |
||
8 | use Honeybadger\HoneybadgerLaravel\Commands\HoneybadgerCheckInsSyncCommand; |
||
9 | use Honeybadger\LogHandler; |
||
10 | use Honeybadger\Honeybadger; |
||
11 | use Honeybadger\Contracts\Reporter; |
||
12 | use Honeybadger\Exceptions\ServiceException; |
||
13 | use Honeybadger\HoneybadgerLaravel\Commands\HoneybadgerCheckInCommand; |
||
14 | use Honeybadger\HoneybadgerLaravel\Commands\HoneybadgerDeployCommand; |
||
15 | use Honeybadger\HoneybadgerLaravel\Commands\HoneybadgerInstallCommand; |
||
16 | use Honeybadger\HoneybadgerLaravel\Commands\HoneybadgerTestCommand; |
||
17 | use Honeybadger\HoneybadgerLaravel\Contracts\Installer as InstallerContract; |
||
18 | use Illuminate\Console\Scheduling\Event; |
||
19 | use Illuminate\Support\Facades\Blade; |
||
20 | use Illuminate\Support\ServiceProvider; |
||
21 | |||
22 | class HoneybadgerServiceProvider extends ServiceProvider |
||
23 | { |
||
24 | /** |
||
25 | * Bootstrap the application services. |
||
26 | */ |
||
27 | public function boot() |
||
28 | { |
||
29 | if ($this->app->runningInConsole()) { |
||
30 | $this->bindCommands(); |
||
31 | $this->registerCommands(); |
||
32 | $this->app->bind(InstallerContract::class, Installer::class); |
||
33 | |||
34 | $this->registerPublishableAssets(); |
||
35 | } |
||
36 | |||
37 | $this->registerEventHooks(); |
||
38 | $this->loadViewsFrom(__DIR__.'/../resources/views', 'honeybadger'); |
||
39 | $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'honeybadger'); |
||
40 | $this->registerBladeDirectives(); |
||
41 | $this->setUpAutomaticBreadcrumbs(); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Register the application services. |
||
46 | */ |
||
47 | public function register() |
||
48 | { |
||
49 | $this->mergeConfigFrom(__DIR__.'/../config/honeybadger.php', 'honeybadger'); |
||
50 | |||
51 | $this->registerReporters(); |
||
52 | $this->registerCheckInsSync(); |
||
53 | |||
54 | $this->app->bind(LogHandler::class, function ($app) { |
||
55 | return new LogHandler($app[Reporter::class]); |
||
56 | }); |
||
57 | |||
58 | $this->app->singleton('honeybadger.isLumen', function () { |
||
59 | return preg_match('/lumen/i', $this->app->version()); |
||
60 | }); |
||
61 | |||
62 | $this->app->when(HoneybadgerDeployCommand::class) |
||
63 | ->needs(Client::class) |
||
64 | ->give(function () { |
||
65 | return new Client([ |
||
66 | 'http_errors' => false, |
||
67 | ]); |
||
68 | }); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return void |
||
73 | */ |
||
74 | private function registerCommands() |
||
75 | { |
||
76 | $this->commands([ |
||
77 | 'command.honeybadger:test', |
||
78 | 'command.honeybadger:checkin', |
||
79 | 'command.honeybadger:checkins:sync', |
||
80 | 'command.honeybadger:install', |
||
81 | 'command.honeybadger:deploy', |
||
82 | ]); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return void |
||
87 | */ |
||
88 | private function bindCommands() |
||
89 | { |
||
90 | $this->app->bind( |
||
91 | 'command.honeybadger:test', |
||
92 | HoneybadgerTestCommand::class |
||
93 | ); |
||
94 | |||
95 | $this->app->bind( |
||
96 | 'command.honeybadger:checkin', |
||
97 | HoneybadgerCheckInCommand::class |
||
98 | ); |
||
99 | |||
100 | $this->app->bind( |
||
101 | 'command.honeybadger:checkins:sync', |
||
102 | HoneybadgerCheckInsSyncCommand::class |
||
103 | ); |
||
104 | |||
105 | $this->app->bind( |
||
106 | 'command.honeybadger:install', |
||
107 | HoneybadgerInstallCommand::class |
||
108 | ); |
||
109 | |||
110 | $this->app->bind( |
||
111 | 'command.honeybadger:deploy', |
||
112 | HoneybadgerDeployCommand::class |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return void |
||
118 | */ |
||
119 | private function registerEventHooks() |
||
120 | { |
||
121 | /** @param string|array|null $environments */ |
||
122 | Event::macro('thenPingHoneybadger', function (string $checkinIdOrName, $environments = null) { |
||
123 | return $this->then(function () use ($checkinIdOrName, $environments) { |
||
124 | if ($environments === null || app()->environment($environments)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
125 | app(Reporter::class)->checkin($checkinIdOrName); |
||
126 | } |
||
127 | }); |
||
128 | }); |
||
129 | |||
130 | /** @param string|array|null $environments */ |
||
131 | Event::macro('pingHoneybadgerOnSuccess', function (string $checkinIdOrName, $environments = null) { |
||
132 | return $this->onSuccess(function () use ($checkinIdOrName, $environments) { |
||
133 | if ($environments === null || app()->environment($environments)) { |
||
134 | app(Reporter::class)->checkin($checkinIdOrName); |
||
135 | } |
||
136 | }); |
||
137 | }); |
||
138 | } |
||
139 | |||
140 | private function registerBladeDirectives() |
||
141 | { |
||
142 | // Views are not enabled on Lumen by default |
||
143 | if (app()->bound('blade.compiler')) { |
||
144 | Blade::directive('honeybadgerError', function ($options) { |
||
145 | if ($options === '') { |
||
146 | $options = '[]'; |
||
147 | } |
||
148 | |||
149 | $defaults = "['class' => 'text-gray-500 text-sm', 'text' => 'Error ID:']"; |
||
150 | |||
151 | return "<?php echo \$__env->make('honeybadger::informer', $options, $defaults)->render(); ?>"; |
||
152 | }); |
||
153 | |||
154 | Blade::directive('honeybadgerFeedback', function () { |
||
155 | $action = rtrim(Honeybadger::API_URL, '/').'/v1/feedback'; |
||
156 | |||
157 | return "<?php echo \$__env->make('honeybadger::feedback', ['action' => '$action'])->render(); ?>"; |
||
158 | }); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | protected function registerPublishableAssets(): void |
||
163 | { |
||
164 | $this->publishes([ |
||
165 | __DIR__.'/../config/honeybadger.php' => base_path('config/honeybadger.php'), |
||
166 | ], 'honeybadger-config'); |
||
167 | $this->publishes([ |
||
168 | __DIR__.'/../resources/views' => resource_path('views/vendor/honeybadger'), |
||
169 | ], 'honeybadger-views'); |
||
170 | $this->publishes([ |
||
171 | __DIR__.'/../resources/lang' => resource_path('lang/vendor/honeybadger'), |
||
172 | ], 'honeybadger-translations'); |
||
173 | } |
||
174 | |||
175 | protected function setUpAutomaticBreadcrumbs() |
||
176 | { |
||
177 | if (config('honeybadger.breadcrumbs.enabled', true) === false) { |
||
178 | return; |
||
179 | } |
||
180 | |||
181 | $breadcrumbs = config('honeybadger.breadcrumbs.automatic', HoneybadgerLaravel::DEFAULT_BREADCRUMBS); |
||
182 | foreach ($breadcrumbs as $breadcrumb) { |
||
183 | (new $breadcrumb)->register(); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | protected function registerCheckInsSync(): void |
||
188 | { |
||
189 | $this->app->singleton(SyncCheckIns::class, function ($app) { |
||
190 | return new CheckInsManager($app['config']['honeybadger']); |
||
191 | }); |
||
192 | } |
||
193 | |||
194 | protected function registerReporters(): void |
||
195 | { |
||
196 | $this->app->singleton(Reporter::class, function ($app) { |
||
197 | return HoneybadgerLaravel::make($app['config']['honeybadger']); |
||
198 | }); |
||
199 | |||
200 | $this->app->alias(Reporter::class, Honeybadger::class); |
||
201 | $this->app->alias(Reporter::class, 'honeybadger'); |
||
202 | |||
203 | // In some cases (like the test command), we definitely want to throw any errors |
||
204 | // Laravel's contextual binding doesn't support method injection, |
||
205 | // so the handle() method will have to request this client specifically. |
||
206 | $this->app->singleton('honeybadger.loud', function ($app) { |
||
207 | $config = $app['config']['honeybadger']; |
||
208 | $config['service_exception_handler'] = function (ServiceException $e) { |
||
209 | throw $e; |
||
210 | }; |
||
211 | |||
212 | return HoneybadgerLaravel::make($config); |
||
213 | }); |
||
214 | } |
||
215 | } |
||
216 |