Skip to content

Commit 8ab08ed

Browse files
committed
Resolve string based throttles from the container and properly type-hint against the contract.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 14e1f5e commit 8ab08ed

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

src/Http/RateLimit/Handler.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Illuminate\Cache\CacheManager;
77
use Illuminate\Support\Collection;
88
use Illuminate\Container\Container;
9+
use Dingo\Api\Http\RateLimit\Throttle\Route;
10+
use Dingo\Api\Contract\Http\RateLimit\Throttle;
911

1012
class Handler
1113
{
@@ -33,7 +35,7 @@ class Handler
3335
/**
3436
* Throttle used for rate limiting.
3537
*
36-
* @var \Dingo\Api\Http\RateLimit\Throttle\Throttle
38+
* @var \Dingo\Api\Contract\Http\RateLimit\Throttle
3739
*/
3840
protected $throttle;
3941

@@ -89,14 +91,14 @@ public function rateLimitRequest(Request $request, $limit = 0, $expires = 0)
8991

9092
// If the throttle instance is already set then we'll just carry on as
9193
// per usual.
92-
if ($this->throttle instanceof Throttle\Throttle) {
94+
if ($this->throttle instanceof Throttle) {
9395
//
9496

9597
// If the developer specified a certain amount of requests or expiration
9698
// time on a specific route then we'll always use the route specific
9799
// throttle with the given values.
98100
} elseif ($limit > 0 || $expires > 0) {
99-
$this->throttle = new Throttle\Route(['limit' => $limit, 'expires' => $expires]);
101+
$this->throttle = new Route(['limit' => $limit, 'expires' => $expires]);
100102

101103
$this->keyPrefix = md5($request->path());
102104

@@ -256,7 +258,7 @@ public function setRateLimiter(callable $limiter)
256258
/**
257259
* Set the throttle to use for rate limiting.
258260
*
259-
* @param string|\Dingo\Api\Http\RateLimit\Throttle\Throttle
261+
* @param string|\Dingo\Api\Contract\Http\RateLimit\Throttle
260262
*
261263
* @return void
262264
*/
@@ -272,7 +274,7 @@ public function setThrottle($throttle)
272274
/**
273275
* Get the throttle used to rate limit the request.
274276
*
275-
* @return \Dingo\Api\Http\RateLimit\Throttle\Throttle
277+
* @return \Dingo\Api\Contract\Http\RateLimit\Throttle
276278
*/
277279
public function getThrottle()
278280
{

src/Routing/Route.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ protected function setupThrottle()
167167
$this->findControllerOptions('throttles', function ($value) {
168168
$this->throttle = $value['throttle'];
169169
});
170+
171+
if (is_string($this->throttle)) {
172+
$this->throttle = $this->container->make($this->throttle);
173+
}
170174
}
171175

172176
/**

tests/Routing/RouteTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testCreatingNewRoute()
3434
'providers' => ['foo'],
3535
'limit' => 5,
3636
'expires' => 10,
37-
'throttle' => 'Foo',
37+
'throttle' => 'Dingo\Api\Tests\Stubs\BasicThrottleStub',
3838
'version' => ['v1'],
3939
'conditionalRequest' => false,
4040
],
@@ -45,7 +45,7 @@ public function testCreatingNewRoute()
4545
$this->assertEquals(5, $route->getRateLimit(), 'Route did not setup rate limit correctly.');
4646
$this->assertEquals(10, $route->getRateExpiration(), 'Route did not setup rate limit expiration correctly.');
4747
$this->assertTrue($route->hasThrottle(), 'Route did not setup throttle correctly.');
48-
$this->assertEquals('Foo', $route->getThrottle(), 'Route did not setup throttle correctly.');
48+
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\BasicThrottleStub', $route->getThrottle(), 'Route did not setup throttle correctly.');
4949
$this->assertFalse($route->requestIsConditional(), 'Route did not setup conditional request correctly.');
5050
}
5151

@@ -73,6 +73,6 @@ public function testControllerOptionsMergeAndOverrideRouteOptions()
7373
$this->assertEquals(10, $route->getRateLimit(), 'Route did not setup rate limit correctly.');
7474
$this->assertEquals(20, $route->getRateExpiration(), 'Route did not setup rate limit expiration correctly.');
7575
$this->assertTrue($route->hasThrottle(), 'Route did not setup throttle correctly.');
76-
$this->assertEquals('Zippy', $route->getThrottle(), 'Route did not setup throttle correctly.');
76+
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\BasicThrottleStub', $route->getThrottle(), 'Route did not setup throttle correctly.');
7777
}
7878
}

tests/Routing/RouterTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Dingo\Api\Http;
77
use Dingo\Api\Routing\Router;
88
use Illuminate\Container\Container;
9+
use Dingo\Api\Tests\Stubs\ThrottleStub;
910
use Symfony\Component\HttpKernel\Exception\HttpException;
1011

1112
class RouterTest extends Adapter\BaseAdapterTest
@@ -41,7 +42,7 @@ public function testRouteOptionsMergeCorrectly()
4142
$request = $this->createRequest('baz', 'GET', ['accept' => 'application/vnd.api.v1+json']);
4243
$this->router->dispatch($request);
4344

44-
$this->router->version('v2', ['providers' => 'foo', 'throttle' => 'Bar', 'namespace' => 'Dingo\Api\Tests'], function () {
45+
$this->router->version('v2', ['providers' => 'foo', 'throttle' => new ThrottleStub(['limit' => 10, 'expires' => 15]), 'namespace' => 'Dingo\Api\Tests'], function () {
4546
$this->router->get('foo', 'Stubs\RoutingControllerStub@index');
4647
});
4748

@@ -54,7 +55,7 @@ public function testRouteOptionsMergeCorrectly()
5455
$this->assertEquals(['foo', 'red', 'black'], $route->getAuthProviders());
5556
$this->assertEquals(10, $route->getRateLimit());
5657
$this->assertEquals(20, $route->getRateExpiration());
57-
$this->assertEquals('Zippy', $route->getThrottle());
58+
$this->assertInstanceOf('Dingo\Api\Tests\Stubs\BasicThrottleStub', $route->getThrottle());
5859
}
5960

6061
/**

tests/Stubs/BasicThrottleStub.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Dingo\Api\Tests\Stubs;
4+
5+
use Illuminate\Container\Container;
6+
use Dingo\Api\Contract\Http\RateLimit\Throttle;
7+
8+
class BasicThrottleStub implements Throttle
9+
{
10+
public function match(Container $app)
11+
{
12+
return true;
13+
}
14+
15+
public function getLimit()
16+
{
17+
return 15;
18+
}
19+
20+
public function getExpires()
21+
{
22+
return 10;
23+
}
24+
}

tests/Stubs/RoutingControllerStub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct()
1717

1818
$this->rateLimit(10, 20);
1919

20-
$this->throttle('Zippy');
20+
$this->throttle('Dingo\Api\Tests\Stubs\BasicThrottleStub');
2121
}
2222

2323
public function index()

0 commit comments

Comments
 (0)