Skip to content

Commit 6e58192

Browse files
committed
Injecting application container into middleware to boot.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 9f31abc commit 6e58192

File tree

3 files changed

+49
-33
lines changed

3 files changed

+49
-33
lines changed

src/ApiServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ protected function registerAuthentication()
161161
*/
162162
protected function registerMiddlewares()
163163
{
164-
$this->app->middleware('Dingo\Api\Http\Middleware\Authentication');
164+
$this->app->middleware('Dingo\Api\Http\Middleware\Authentication', [$this->app]);
165165
}
166166

167167
}

src/Http/Middleware/Authentication.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Dingo\Api\Http\Response;
44
use Illuminate\Routing\Route;
55
use Dingo\Api\Http\InternalRequest;
6+
use Illuminate\Container\Container;
67
use Symfony\Component\HttpFoundation\Request;
78
use Symfony\Component\HttpKernel\HttpKernelInterface;
89
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
@@ -16,15 +17,24 @@ class Authentication implements HttpKernelInterface {
1617
*/
1718
protected $app;
1819

20+
/**
21+
* Laravel application container.
22+
*
23+
* @var \Illuminate\Container\Container
24+
*/
25+
protected $container;
26+
1927
/**
2028
* Create a new authentication middleware instance.
2129
*
2230
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $app
31+
* @param \Illuminate\Container\Container $container
2332
* @return void
2433
*/
25-
public function __construct(HttpKernelInterface $app)
34+
public function __construct(HttpKernelInterface $app, Container $container)
2635
{
2736
$this->app = $app;
37+
$this->container = $container;
2838
}
2939

3040
/**
@@ -41,14 +51,14 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
4151
// Our middleware needs to ensure that Laravel is booted before we
4252
// can do anything. This gives us access to all the booted
4353
// service providers and other container bindings.
44-
$this->app->boot();
54+
$this->container->boot();
4555

46-
if ($request instanceof InternalRequest or $this->app->make('dingo.api.auth')->user())
56+
if ($request instanceof InternalRequest or $this->container->make('dingo.api.auth')->user())
4757
{
4858
return $this->app->handle($request, $type, $catch);
4959
}
5060

51-
$router = $this->app->make('router');
61+
$router = $this->container->make('router');
5262

5363
$response = null;
5464

@@ -78,11 +88,11 @@ protected function authenticate(Request $request, Route $route)
7888
{
7989
try
8090
{
81-
$this->app->make('dingo.api.auth')->authenticate($request, $route);
91+
$this->container->make('dingo.api.auth')->authenticate($request, $route);
8292
}
8393
catch (UnauthorizedHttpException $exception)
8494
{
85-
$router = $this->app->make('router');
95+
$router = $this->container->make('router');
8696

8797
$response = $router->handleException($exception);
8898

tests/HttpMiddlewareAuthenticationTest.php

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ public function testLaravelMiddlewareIsHandledForInternalRequestsAndAuthenticate
2424
{
2525
$response = new Response;
2626
$app = m::mock('Symfony\Component\HttpKernel\HttpKernelInterface');
27-
$middleware = new Authentication($app);
27+
$container = m::mock('Illuminate\Container\Container');
28+
$middleware = new Authentication($app, $container);
2829
$request = InternalRequest::create('/', 'GET');
2930

30-
$app->shouldReceive('boot')->once();
31+
$container->shouldReceive('boot')->once();
3132
$app->shouldReceive('handle')->once()->with($request, HttpKernelInterface::MASTER_REQUEST, true)->andReturn($response);
3233
$this->assertEquals($response, $middleware->handle($request));
3334

3435
$request = Request::create('/', 'GET');
3536

36-
$app->shouldReceive('boot')->once();
37-
$app->shouldReceive('make')->once()->with('dingo.api.auth')->andReturn(m::mock(['user' => true]));
37+
$container->shouldReceive('boot')->once();
38+
$container->shouldReceive('make')->once()->with('dingo.api.auth')->andReturn(m::mock(['user' => true]));
3839
$app->shouldReceive('handle')->once()->with($request, HttpKernelInterface::MASTER_REQUEST, true)->andReturn($response);
3940

4041
$this->assertEquals($response, $middleware->handle($request));
@@ -45,13 +46,14 @@ public function testLaravelMiddlewareIsHandledWhenNoApiRouteCollectionForRequest
4546
{
4647
$response = new Response;
4748
$app = m::mock('Symfony\Component\HttpKernel\HttpKernelInterface');
49+
$container = m::mock('Illuminate\Container\Container');
4850
$router = m::mock('Dingo\Api\Routing\Router');
49-
$middleware = new Authentication($app);
51+
$middleware = new Authentication($app, $container);
5052
$request = Request::create('/', 'GET');
5153

52-
$app->shouldReceive('boot')->once();
53-
$app->shouldReceive('make')->once()->with('dingo.api.auth')->andReturn(m::mock(['user' => false]));
54-
$app->shouldReceive('make')->once()->with('router')->andReturn($router);
54+
$container->shouldReceive('boot')->once();
55+
$container->shouldReceive('make')->once()->with('dingo.api.auth')->andReturn(m::mock(['user' => false]));
56+
$container->shouldReceive('make')->once()->with('router')->andReturn($router);
5557
$app->shouldReceive('handle')->once()->with($request, HttpKernelInterface::MASTER_REQUEST, true)->andReturn($response);
5658

5759
$router->shouldReceive('getApiRouteCollectionFromRequest')->once()->with($request)->andReturn(null);
@@ -64,14 +66,15 @@ public function testLaravelMiddlewareIsHandledWhenRouteNotFoundInApiRouteCollect
6466
{
6567
$response = new Response;
6668
$app = m::mock('Symfony\Component\HttpKernel\HttpKernelInterface');
69+
$container = m::mock('Illuminate\Container\Container');
6770
$router = m::mock('Dingo\Api\Routing\Router');
6871
$collection = m::mock('Dingo\Api\Routing\ApiRouteCollection');
69-
$middleware = new Authentication($app);
72+
$middleware = new Authentication($app, $container);
7073
$request = Request::create('/', 'GET');
7174

72-
$app->shouldReceive('boot')->once();
73-
$app->shouldReceive('make')->once()->with('dingo.api.auth')->andReturn(m::mock(['user' => false]));
74-
$app->shouldReceive('make')->once()->with('router')->andReturn($router);
75+
$container->shouldReceive('boot')->once();
76+
$container->shouldReceive('make')->once()->with('dingo.api.auth')->andReturn(m::mock(['user' => false]));
77+
$container->shouldReceive('make')->once()->with('router')->andReturn($router);
7578
$app->shouldReceive('handle')->once()->with($request, HttpKernelInterface::MASTER_REQUEST, true)->andReturn($response);
7679

7780
$router->shouldReceive('getApiRouteCollectionFromRequest')->once()->with($request)->andReturn($collection);
@@ -86,15 +89,16 @@ public function testLaravelMiddlewareIsHandledWhenRouteIsNotProtected()
8689
{
8790
$response = new Response;
8891
$app = m::mock('Symfony\Component\HttpKernel\HttpKernelInterface');
92+
$container = m::mock('Illuminate\Container\Container');
8993
$router = m::mock('Dingo\Api\Routing\Router');
9094
$collection = m::mock('Dingo\Api\Routing\ApiRouteCollection');
91-
$middleware = new Authentication($app);
95+
$middleware = new Authentication($app, $container);
9296
$request = Request::create('/', 'GET');
9397
$route = new Route('GET', '/', ['protected' => false]);
9498

95-
$app->shouldReceive('boot')->once();
96-
$app->shouldReceive('make')->once()->with('dingo.api.auth')->andReturn(m::mock(['user' => false]));
97-
$app->shouldReceive('make')->once()->with('router')->andReturn($router);
99+
$container->shouldReceive('boot')->once();
100+
$container->shouldReceive('make')->once()->with('dingo.api.auth')->andReturn(m::mock(['user' => false]));
101+
$container->shouldReceive('make')->once()->with('router')->andReturn($router);
98102
$app->shouldReceive('handle')->once()->with($request, HttpKernelInterface::MASTER_REQUEST, true)->andReturn($response);
99103

100104
$router->shouldReceive('getApiRouteCollectionFromRequest')->once()->with($request)->andReturn($collection);
@@ -109,17 +113,18 @@ public function testAuthenticationFailsAndThrowExceptionIsHandled()
109113
{
110114
$response = new Response;
111115
$app = m::mock('Symfony\Component\HttpKernel\HttpKernelInterface');
116+
$container = m::mock('Illuminate\Container\Container');
112117
$router = m::mock('Dingo\Api\Routing\Router');
113118
$collection = m::mock('Dingo\Api\Routing\ApiRouteCollection');
114-
$middleware = new Authentication($app);
119+
$middleware = new Authentication($app, $container);
115120
$request = Request::create('/', 'GET');
116121
$route = new Route('GET', '/', ['protected']);
117122
$shield = m::mock('Dingo\Api\Auth\Shield');
118123

119-
$app->shouldReceive('boot')->once();
120-
$app->shouldReceive('make')->once()->once('dingo.api.auth')->andReturn(m::mock(['user' => false]));
121-
$app->shouldReceive('make')->twice()->with('router')->andReturn($router);
122-
$app->shouldReceive('make')->once()->once('dingo.api.auth')->andReturn($shield);
124+
$container->shouldReceive('boot')->once();
125+
$container->shouldReceive('make')->once()->once('dingo.api.auth')->andReturn(m::mock(['user' => false]));
126+
$container->shouldReceive('make')->twice()->with('router')->andReturn($router);
127+
$container->shouldReceive('make')->once()->once('dingo.api.auth')->andReturn($shield);
123128

124129
$router->shouldReceive('getApiRouteCollectionFromRequest')->once()->with($request)->andReturn($collection);
125130

@@ -139,17 +144,18 @@ public function testAuthenticationPassesAndLaravelMiddlewareIsHandled()
139144
{
140145
$response = new Response;
141146
$app = m::mock('Symfony\Component\HttpKernel\HttpKernelInterface');
147+
$container = m::mock('Illuminate\Container\Container');
142148
$router = m::mock('Dingo\Api\Routing\Router');
143149
$collection = m::mock('Dingo\Api\Routing\ApiRouteCollection');
144-
$middleware = new Authentication($app);
150+
$middleware = new Authentication($app, $container);
145151
$request = Request::create('/', 'GET');
146152
$route = new Route('GET', '/', ['protected']);
147153
$shield = m::mock('Dingo\Api\Auth\Shield');
148154

149-
$app->shouldReceive('boot')->once();
150-
$app->shouldReceive('make')->once()->once('dingo.api.auth')->andReturn(m::mock(['user' => false]));
151-
$app->shouldReceive('make')->once()->with('router')->andReturn($router);
152-
$app->shouldReceive('make')->once()->once('dingo.api.auth')->andReturn($shield);
155+
$container->shouldReceive('boot')->once();
156+
$container->shouldReceive('make')->once()->once('dingo.api.auth')->andReturn(m::mock(['user' => false]));
157+
$container->shouldReceive('make')->once()->with('router')->andReturn($router);
158+
$container->shouldReceive('make')->once()->once('dingo.api.auth')->andReturn($shield);
153159
$app->shouldReceive('handle')->once()->with($request, HttpKernelInterface::MASTER_REQUEST, true)->andReturn($response);
154160

155161
$router->shouldReceive('getApiRouteCollectionFromRequest')->once()->with($request)->andReturn($collection);

0 commit comments

Comments
 (0)