Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add a separate Rate Limiting Service Provider (Deferrable) #34843

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Illuminate/Cache/CacheServiceProvider.php
Expand Up @@ -30,8 +30,6 @@ public function register()
$this->app->singleton('memcached.connector', function () {
return new MemcachedConnector;
});

$this->app->singleton(RateLimiter::class);
}

/**
Expand All @@ -42,7 +40,7 @@ public function register()
public function provides()
{
return [
'cache', 'cache.store', 'cache.psr6', 'memcached.connector', RateLimiter::class,
'cache', 'cache.store', 'cache.psr6', 'memcached.connector',
];
}
}
31 changes: 31 additions & 0 deletions src/Illuminate/Cache/RateLimitingServiceProvider.php
@@ -0,0 +1,31 @@
<?php

namespace Illuminate\Cache;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;

class RateLimitingServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton(RateLimiter::class);
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
RateLimiter::class,
];
}
}
6 changes: 6 additions & 0 deletions tests/Integration/Http/ThrottleRequestsTest.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Cache\RateLimiter;
use Illuminate\Cache\RateLimiting\GlobalLimit;
use Illuminate\Cache\RateLimitingServiceProvider;
use Illuminate\Container\Container;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Routing\Middleware\ThrottleRequests;
Expand Down Expand Up @@ -98,4 +99,9 @@ public function testLimitingUsingNamedLimiter()
$this->assertEquals(Carbon::now()->addSeconds(2)->getTimestamp(), $e->getHeaders()['X-RateLimit-Reset']);
}
}

protected function getPackageProviders($app)
{
return [RateLimitingServiceProvider::class];
}
}
6 changes: 6 additions & 0 deletions tests/Integration/Http/ThrottleRequestsWithRedisTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Http;

use Illuminate\Cache\RateLimitingServiceProvider;
use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
use Illuminate\Routing\Middleware\ThrottleRequestsWithRedis;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -61,4 +62,9 @@ public function testLockOpensImmediatelyAfterDecay()
}
});
}

protected function getPackageProviders($app)
{
return [RateLimitingServiceProvider::class];
}
}