72 lines
2.2 KiB
PHP
72 lines
2.2 KiB
PHP
<?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();
|
|
});
|
|
}
|
|
}
|