This commit is contained in:
mathiaspalmqvist committed 2026-11-11 23:19:20 +01:00
commit 3ed6eb9d19
23 files changed
+1216

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
<?php
namespace Mattitja\BadIpBlocker;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Support\ServiceProvider;
use Mattitja\BadIpBlocker\Middleware\CheckBadIps;
class BadIpBlockerServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/bad-ip-blocker.php', 'bad-ip-blocker');
$this->app->singleton(BadIpStore::class, fn ($app) => new BadIpStore(
$app['config']->get('bad-ip-blocker.storage_path') ?? storage_path('app/bad-ip-blocker.json'),
));
}
public function boot(): void
{
$this->registerMiddleware();
$this->registerSchedule();
if (! $this->app->runningInConsole()) {
return;
}
$this->publishes([
__DIR__.'/../config/bad-ip-blocker.php' => config_path('bad-ip-blocker.php'),
], 'bad-ip-blocker-config');
$this->commands([
Console\AddCommand::class,
Console\RemoveCommand::class,
Console\ListCommand::class,
Console\PruneCommand::class,
Console\ClearCommand::class,
]);
}
/**
* Append the middleware to the global stack. Runs after bootstrap/app.php's
* withMiddleware() has set the global middleware, so it is not overwritten,
* and pushMiddleware() skips it if the app already registered it manually.
*/
protected function registerMiddleware(): void
{
if (! $this->app['config']->get('bad-ip-blocker.auto_register', true)) {
return;
}
$this->callAfterResolving(HttpKernel::class, function ($kernel) {
if (method_exists($kernel, 'pushMiddleware')) {
$kernel->pushMiddleware(CheckBadIps::class);
}
});
}
protected function registerSchedule(): void
{
if (! $this->app['config']->get('bad-ip-blocker.schedule_prune', true)) {
return;
}
$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
$schedule->command('bad-ip-blocker:prune')->daily();
});
}
}