Skip to content

Commit e923b56

Browse files
authored
Custom middleware for repositories (#194)
* Custom middleware * Apply fixes from StyleCI (#193)
1 parent c3afc33 commit e923b56

11 files changed

+139
-6
lines changed

src/Http/Controllers/ProfileAvatarController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ public function __invoke(ProfileAvatarRequest $request)
2525

2626
$user->{ProfileAvatarRequest::$userAvatarAttribute} = url($user->{ProfileAvatarRequest::$userAvatarAttribute});
2727

28-
return $this->response()->data($user);
28+
return $this->response()->model($user);
2929
}
3030
}

src/Http/Controllers/ProfileController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public function __invoke(RestifyRequest $request)
1515
$user->{ProfileAvatarRequest::$userAvatarAttribute} = url($user->{ProfileAvatarRequest::$userAvatarAttribute});
1616
}
1717

18-
return $this->response()->data($user);
18+
return $this->response()->model($user);
1919
}
2020
}

src/Http/Controllers/RepositoryIndexController.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public function __invoke(RepositoryIndexRequest $request)
1818
->dump($e, $request->isDev());
1919
} catch (UnauthorizedException $e) {
2020
return $this->response()->forbidden()->addError($e->getMessage())->dump($e, $request->isDev());
21-
} catch (\Throwable $e) {
22-
return $this->response()->error()->dump($e, $request->isDev());
2321
}
2422
}
2523
}

src/Http/Requests/InteractWithRepositories.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Binaryk\LaravelRestify\Repositories\Repository;
88
use Binaryk\LaravelRestify\Restify;
99
use Illuminate\Database\Eloquent\Model;
10+
use Illuminate\Pipeline\Pipeline;
1011

1112
/**
1213
* @author Eduard Lupacescu <[email protected]>
@@ -49,6 +50,11 @@ public function repository($key = null): ?Repository
4950
'name' => $repository,
5051
]), 403);
5152
}
53+
54+
app(Pipeline::class)
55+
->send($this)
56+
->through($repository::collectMiddlewares()->toArray())
57+
->thenReturn();
5258
});
5359

5460
return $repository::resolveWith($repository::newModel());

src/Http/Requests/RestifyRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Binaryk\LaravelRestify\Http\Requests;
44

5-
use Binaryk\LaravelRestify\Restify;
65
use Illuminate\Foundation\Http\FormRequest;
76
use Illuminate\Support\Facades\App;
87

src/Repositories/Repository.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ abstract class Repository implements RestifySearchable, JsonSerializable
9797
*/
9898
public static $globalSearchResults = 5;
9999

100+
/**
101+
* The list of middlewares for the current repository.
102+
*
103+
* @var array
104+
*/
105+
public static $middlewares = [];
106+
100107
/**
101108
* Get the underlying model instance for the resource.
102109
*
@@ -731,4 +738,9 @@ public function availableFilters(RestifyRequest $request)
731738
return collect($this->filter($this->filters($request)))->each(fn (Filter $filter) => $filter->authorizedToSee($request))
732739
->values();
733740
}
741+
742+
public static function collectMiddlewares(): Collection
743+
{
744+
return collect(static::$middlewares);
745+
}
734746
}

tests/Controllers/GlobalSearchControllerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public function test_global_search_filter_will_filter_with_index_query()
5858

5959
$response = $this
6060
->withoutExceptionHandling()
61-
->getJson('/restify-api/search?search=1');
61+
->getJson('/restify-api/search?search=1')
62+
->dump();
6263

6364
$this->assertCount(1, $response->json('data'));
6465

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Controllers;
4+
5+
use Binaryk\LaravelRestify\Restify;
6+
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostAbortMiddleware;
7+
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostWithCustomMiddlewareRepository;
8+
use Binaryk\LaravelRestify\Tests\IntegrationTest;
9+
use Mockery as m;
10+
11+
class RepositoryMiddlewaresTest extends IntegrationTest
12+
{
13+
protected function tearDown(): void
14+
{
15+
m::close();
16+
}
17+
18+
public function test_repository_can_have_custom_middleware()
19+
{
20+
$middleware = m::mock(PostAbortMiddleware::class);
21+
22+
$nextParam = null;
23+
24+
$middleware
25+
->expects('handle')
26+
->once();
27+
28+
PostWithCustomMiddlewareRepository::$middlewares = [
29+
$middleware,
30+
];
31+
32+
Restify::repositories([
33+
PostWithCustomMiddlewareRepository::class,
34+
]);
35+
36+
$this->getJson('restify-api/post-with-middleware')
37+
->assertStatus(200);
38+
}
39+
40+
public function test_request_fails_if_middleware_abort()
41+
{
42+
PostWithCustomMiddlewareRepository::$middlewares = [
43+
PostAbortMiddleware::class,
44+
];
45+
46+
Restify::repositories([
47+
PostWithCustomMiddlewareRepository::class,
48+
]);
49+
50+
$this->getJson('restify-api/post-with-middleware')
51+
->assertStatus(404);
52+
}
53+
54+
public function test_foreign_repository_middleware_should_not_be_invoked()
55+
{
56+
$middleware = m::mock(PostAbortMiddleware::class);
57+
58+
$nextParam = null;
59+
60+
$middleware
61+
->expects('handle')
62+
->never();
63+
64+
PostWithCustomMiddlewareRepository::$middlewares = [
65+
$middleware,
66+
];
67+
68+
Restify::repositories([
69+
PostWithCustomMiddlewareRepository::class,
70+
]);
71+
72+
$this->getJson('restify-api/posts')
73+
->assertStatus(200);
74+
}
75+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Fixtures\Post;
4+
5+
use Illuminate\Http\Request;
6+
7+
class PostAbortMiddleware
8+
{
9+
public function handle(Request $request, $next)
10+
{
11+
abort(404);
12+
13+
$next($request);
14+
}
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Fixtures\Post;
4+
5+
use Binaryk\LaravelRestify\Fields\Field;
6+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
7+
use Binaryk\LaravelRestify\Repositories\Repository;
8+
9+
class PostWithCustomMiddlewareRepository extends Repository
10+
{
11+
public static $model = Post::class;
12+
13+
public static $middlewares = [
14+
PostAbortMiddleware::class,
15+
];
16+
17+
public static $uriKey = 'post-with-middleware';
18+
19+
public function fields(RestifyRequest $request)
20+
{
21+
return [
22+
Field::new('title'),
23+
];
24+
}
25+
}

0 commit comments

Comments
 (0)