diff --git a/composer.json b/composer.json index 202ff54..1a34c6d 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ ], "require": { "php": "^8.1", - "honeybadger-io/honeybadger-php": ">=2.21.0", + "honeybadger-io/honeybadger-php": ">=2.23.0", "sixlive/dotenv-editor": "^1.1|^2.0", "illuminate/console": "^10.0|^11.0", "illuminate/support": "^10.0|^11.0", diff --git a/config/honeybadger.php b/config/honeybadger.php index b575f21..f0a35c8 100644 --- a/config/honeybadger.php +++ b/config/honeybadger.php @@ -3,6 +3,7 @@ use Honeybadger\BulkEventDispatcher; use Honeybadger\Honeybadger; use Honeybadger\HoneybadgerLaravel\HoneybadgerLaravel; +use Honeybadger\HoneybadgerLaravel\Middleware\AssignRequestId; return [ /** @@ -165,4 +166,8 @@ */ 'automatic' => HoneybadgerLaravel::DEFAULT_EVENTS, ], + + 'middleware' => [ + AssignRequestId::class + ] ]; diff --git a/src/HoneybadgerServiceProvider.php b/src/HoneybadgerServiceProvider.php index a9577d2..f813ea5 100644 --- a/src/HoneybadgerServiceProvider.php +++ b/src/HoneybadgerServiceProvider.php @@ -245,7 +245,19 @@ protected function registerReporters(): void protected function registerMiddleware(): void { + $middleware = config('honeybadger.middleware', [ + // the default middleware if the config is not found - this should happen for + // all versions of the package up to and including 4.3.1 + Middleware\AssignRequestId::class, + ]); + + if ($middleware == null || !is_array($middleware)) { + return; + } + $kernel = app(Kernel::class); - $kernel->prependMiddleware(Middleware\AssignRequestId::class); + foreach ($middleware as $class) { + $kernel->prependMiddleware($class); + } } } diff --git a/tests/HoneybadgerServiceProviderTest.php b/tests/HoneybadgerServiceProviderTest.php index f67cbf8..79785f9 100644 --- a/tests/HoneybadgerServiceProviderTest.php +++ b/tests/HoneybadgerServiceProviderTest.php @@ -2,11 +2,14 @@ namespace Honeybadger\Tests; +use Honeybadger\HoneybadgerLaravel\HoneybadgerServiceProvider; +use Honeybadger\HoneybadgerLaravel\Middleware\AssignRequestId; use Honeybadger\LogEventHandler; use Honeybadger\LogHandler; use Honeybadger\Honeybadger; use Honeybadger\Contracts\Reporter; use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger as HoneybadgerFacade; +use Illuminate\Contracts\Http\Kernel; class HoneybadgerServiceProviderTest extends TestCase { @@ -35,4 +38,29 @@ public function it_registers_the_log_event_handler() { $this->assertInstanceOf(LogEventHandler::class, $this->app[LogEventHandler::class]); } + + /** @test */ + public function it_registers_middleware_by_default() + { + $this->partialMock(Kernel::class, function ($mock) { + $mock->shouldReceive('prependMiddleware') + ->with(AssignRequestId::class) + ->once(); + }); + $provider = new HoneybadgerServiceProvider($this->app); + $provider->boot(); + } + + /** @test */ + public function it_does_not_register_middleware_when_disabled() + { + $this->app['config']->set('honeybadger.middleware', []); + $this->partialMock(Kernel::class, function ($mock) { + $mock->shouldReceive('prependMiddleware') + ->with(AssignRequestId::class) + ->never(); + }); + $provider = new HoneybadgerServiceProvider($this->app); + $provider->boot(); + } } diff --git a/tests/LogEventDriverTest.php b/tests/LogEventDriverTest.php index 3384361..f4fcb16 100644 --- a/tests/LogEventDriverTest.php +++ b/tests/LogEventDriverTest.php @@ -95,6 +95,14 @@ public function getEvents(): array { return $this->events; } + + public function beforeNotify(callable $callback): void + { + } + + public function beforeEvent(callable $callback): void + { + } }; }