Skip to content

Commit

Permalink
feat: allow disabling middleware from configuration
Browse files Browse the repository at this point in the history
Closes: #148
  • Loading branch information
subzero10 committed Dec 28, 2024
1 parent 8d190e5 commit e98a82f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config/honeybadger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Honeybadger\BulkEventDispatcher;
use Honeybadger\Honeybadger;
use Honeybadger\HoneybadgerLaravel\HoneybadgerLaravel;
use Honeybadger\HoneybadgerLaravel\Middleware\AssignRequestId;

return [
/**
Expand Down Expand Up @@ -165,4 +166,8 @@
*/
'automatic' => HoneybadgerLaravel::DEFAULT_EVENTS,
],

'middleware' => [
AssignRequestId::class
]
];
14 changes: 13 additions & 1 deletion src/HoneybadgerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
28 changes: 28 additions & 0 deletions tests/HoneybadgerServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}
}

0 comments on commit e98a82f

Please sign in to comment.