Skip to content

Commit 2f5af2d

Browse files
author
Andrey Helldar
authored
Merge pull request #26 from TheDragonCode/2.x
Added ability to assign middleware to specific route groups
2 parents 78f47ed + 43dede6 commit 2f5af2d

File tree

8 files changed

+277
-14
lines changed

8 files changed

+277
-14
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ Or manually update `require` block of `composer.json` and run `composer update`.
3232

3333
After you've installed the package via composer, you're done. There's no step two.
3434

35-
This package will automatically register the `DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware` middleware in the `web` and `api` groups, if they exist. The middleware will add a header `Accept` that will effectively convert all responses to JSON format. This header will apply to all responses.
35+
This package will automatically register the `DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware` middleware in the `web` and `api` groups, if they exist. The
36+
middleware will add a header `Accept` that will effectively convert all responses to JSON format. This header will apply to all responses.
37+
38+
> If you need to redefine the header for specific groups of routes, you can do this by changing the [`settings`](config/http.php).
3639
3740

3841
[badge_build]: https://img.shields.io/github/workflow/status/TheDragonCode/laravel-json-response/phpunit?style=flat-square

config/http.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'response' => [
7+
/*
8+
* This setting is responsible for applying middleware to routes.
9+
*
10+
* You can specify route group names to apply the `SetHeaderMiddleware`
11+
* middleware to them, or specify null to apply it to all routes.
12+
*
13+
* For example,
14+
*
15+
* json => null
16+
* json => 'api'
17+
* json => ['api', 'web']
18+
*/
19+
'json' => null,
20+
],
21+
];

src/ServiceProvider.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware;
66
use Illuminate\Contracts\Http\Kernel;
7+
use Illuminate\Support\Arr;
78
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
89

910
class ServiceProvider extends BaseServiceProvider
@@ -12,7 +13,55 @@ class ServiceProvider extends BaseServiceProvider
1213

1314
public function boot(): void
1415
{
15-
$this->resolve()->prependMiddleware($this->middleware);
16+
$this->publishConfig();
17+
18+
$this->registerMiddleware($this->middlewareGroups());
19+
}
20+
21+
public function register(): void
22+
{
23+
$this->registerConfig();
24+
}
25+
26+
protected function registerMiddleware(array $groups): void
27+
{
28+
$this->toAll($groups)
29+
? $this->resolve()->prependMiddleware($this->middleware)
30+
: $this->prependMiddlewareToGroups($this->middleware, $groups);
31+
}
32+
33+
protected function prependMiddlewareToGroups(string $middleware, array $groups): void
34+
{
35+
foreach ($groups as $group) {
36+
$this->resolve()->prependMiddlewareToGroup($group, $middleware);
37+
}
38+
}
39+
40+
protected function toAll(array $groups): bool
41+
{
42+
return empty($groups);
43+
}
44+
45+
protected function middlewareGroups(): array
46+
{
47+
return array_filter(Arr::wrap(
48+
$this->app['config']->get('http.response.json')
49+
));
50+
}
51+
52+
protected function publishConfig(): void
53+
{
54+
$this->publishes([
55+
__DIR__ . '/../config/http.php' => $this->app->configPath('http.php'),
56+
]);
57+
}
58+
59+
protected function registerConfig(): void
60+
{
61+
$this->mergeConfigFrom(
62+
__DIR__ . '/../config/http.php',
63+
'http'
64+
);
1665
}
1766

1867
protected function resolve(): Kernel

tests/Middlewares/SetHeaderMiddlewareTest.php renamed to tests/Middlewares/AllTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55
use Lmc\HttpConstants\Header;
66
use Tests\TestCase;
77

8-
class SetHeaderMiddlewareTest extends TestCase
8+
class AllTest extends TestCase
99
{
10-
public function testWeb(): void
10+
public function testApi(): void
1111
{
1212
$this
13-
->get('web')
13+
->get('api')
1414
->assertSuccessful()
1515
->assertHeader(Header::ACCEPT, 'application/json')
1616
->assertJsonStructure(['data'])
17-
->assertJson(['data' => 'Hello, Web!']);
17+
->assertJson(['data' => 'Hello, Api!']);
1818
}
1919

20-
public function testApi(): void
20+
public function testWeb(): void
2121
{
2222
$this
23-
->get('api')
23+
->get('web')
2424
->assertSuccessful()
2525
->assertHeader(Header::ACCEPT, 'application/json')
2626
->assertJsonStructure(['data'])
27-
->assertJson(['data' => 'Hello, Api!']);
27+
->assertJson(['data' => 'Hello, Web!']);
2828
}
2929

3030
public function testCustom(): void

tests/Middlewares/ApiAndWebTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Tests\Middlewares;
4+
5+
use Lmc\HttpConstants\Header;
6+
use Tests\TestCase;
7+
8+
class ApiAndWebTest extends TestCase
9+
{
10+
protected $groups = ['api', 'web'];
11+
12+
public function testApi(): void
13+
{
14+
$this
15+
->get('api')
16+
->assertSuccessful()
17+
->assertHeader(Header::ACCEPT, 'application/json')
18+
->assertJsonStructure(['data'])
19+
->assertJson(['data' => 'Hello, Api!']);
20+
}
21+
22+
public function testWeb(): void
23+
{
24+
$this
25+
->get('web')
26+
->assertSuccessful()
27+
->assertHeader(Header::ACCEPT, 'application/json')
28+
->assertJsonStructure(['data'])
29+
->assertJson(['data' => 'Hello, Web!']);
30+
}
31+
32+
public function testCustom(): void
33+
{
34+
$this
35+
->get('custom')
36+
->assertSuccessful()
37+
->assertHeaderMissing(Header::ACCEPT)
38+
->assertJsonStructure(['data'])
39+
->assertJson(['data' => 'Hello, Custom!']);
40+
}
41+
42+
public function testCustomHeader(): void
43+
{
44+
$this
45+
->get('custom', [Header::ACCEPT => 'application/xml'])
46+
->assertSuccessful()
47+
->assertHeaderMissing(Header::ACCEPT)
48+
->assertJsonStructure(['data'])
49+
->assertJson(['data' => 'Hello, Custom!']);
50+
}
51+
52+
public function testAsterisk(): void
53+
{
54+
$this
55+
->get('custom', [Header::ACCEPT => '*/*'])
56+
->assertSuccessful()
57+
->assertHeaderMissing(Header::ACCEPT)
58+
->assertJsonStructure(['data'])
59+
->assertJson(['data' => 'Hello, Custom!']);
60+
}
61+
}

tests/Middlewares/OnlyApiTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Tests\Middlewares;
4+
5+
use Lmc\HttpConstants\Header;
6+
use Tests\TestCase;
7+
8+
class OnlyApiTest extends TestCase
9+
{
10+
protected $groups = 'api';
11+
12+
public function testApi(): void
13+
{
14+
$this
15+
->get('api')
16+
->assertSuccessful()
17+
->assertHeader(Header::ACCEPT, 'application/json')
18+
->assertJsonStructure(['data'])
19+
->assertJson(['data' => 'Hello, Api!']);
20+
}
21+
22+
public function testWeb(): void
23+
{
24+
$this
25+
->get('web')
26+
->assertSuccessful()
27+
->assertHeaderMissing(Header::ACCEPT)
28+
->assertJsonStructure(['data'])
29+
->assertJson(['data' => 'Hello, Web!']);
30+
}
31+
32+
public function testCustom(): void
33+
{
34+
$this
35+
->get('custom')
36+
->assertSuccessful()
37+
->assertHeaderMissing(Header::ACCEPT)
38+
->assertJsonStructure(['data'])
39+
->assertJson(['data' => 'Hello, Custom!']);
40+
}
41+
42+
public function testCustomHeader(): void
43+
{
44+
$this
45+
->get('custom', [Header::ACCEPT => 'application/xml'])
46+
->assertSuccessful()
47+
->assertHeaderMissing(Header::ACCEPT)
48+
->assertJsonStructure(['data'])
49+
->assertJson(['data' => 'Hello, Custom!']);
50+
}
51+
52+
public function testAsterisk(): void
53+
{
54+
$this
55+
->get('custom', [Header::ACCEPT => '*/*'])
56+
->assertSuccessful()
57+
->assertHeaderMissing(Header::ACCEPT)
58+
->assertJsonStructure(['data'])
59+
->assertJson(['data' => 'Hello, Custom!']);
60+
}
61+
}

tests/Middlewares/OnlyWebTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Tests\Middlewares;
4+
5+
use Lmc\HttpConstants\Header;
6+
use Tests\TestCase;
7+
8+
class OnlyWebTest extends TestCase
9+
{
10+
protected $groups = 'web';
11+
12+
public function testApi(): void
13+
{
14+
$this
15+
->get('api')
16+
->assertSuccessful()
17+
->assertHeaderMissing(Header::ACCEPT)
18+
->assertJsonStructure(['data'])
19+
->assertJson(['data' => 'Hello, Api!']);
20+
}
21+
22+
public function testWeb(): void
23+
{
24+
$this
25+
->get('web')
26+
->assertSuccessful()
27+
->assertHeader(Header::ACCEPT, 'application/json')
28+
->assertJsonStructure(['data'])
29+
->assertJson(['data' => 'Hello, Web!']);
30+
}
31+
32+
public function testCustom(): void
33+
{
34+
$this
35+
->get('custom')
36+
->assertSuccessful()
37+
->assertHeaderMissing(Header::ACCEPT)
38+
->assertJsonStructure(['data'])
39+
->assertJson(['data' => 'Hello, Custom!']);
40+
}
41+
42+
public function testCustomHeader(): void
43+
{
44+
$this
45+
->get('custom', [Header::ACCEPT => 'application/xml'])
46+
->assertSuccessful()
47+
->assertHeaderMissing(Header::ACCEPT)
48+
->assertJsonStructure(['data'])
49+
->assertJson(['data' => 'Hello, Custom!']);
50+
}
51+
52+
public function testAsterisk(): void
53+
{
54+
$this
55+
->get('custom', [Header::ACCEPT => '*/*'])
56+
->assertSuccessful()
57+
->assertHeaderMissing(Header::ACCEPT)
58+
->assertJsonStructure(['data'])
59+
->assertJson(['data' => 'Hello, Custom!']);
60+
}
61+
}

tests/TestCase.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33
namespace Tests;
44

55
use DragonCode\LaravelJsonResponse\ServiceProvider;
6+
use Illuminate\Contracts\Config\Repository;
7+
use Illuminate\Routing\Router;
68
use Orchestra\Testbench\TestCase as BaseTestCase;
79

810
abstract class TestCase extends BaseTestCase
911
{
12+
protected $groups;
13+
1014
protected function getPackageProviders($app): array
1115
{
1216
return [ServiceProvider::class];
1317
}
1418

1519
protected function getEnvironmentSetUp($app)
1620
{
17-
$this->setRoutes($app);
21+
$this->setRoutes($app['router']);
22+
$this->setConfig($app['config']);
1823
}
1924

20-
protected function setRoutes($app): void
25+
protected function setRoutes(Router $router): void
2126
{
22-
/** @var \Illuminate\Routing\RouteRegistrar $router */
23-
$router = $app['router'];
24-
2527
$router->get('web', function () {
2628
return ['data' => 'Hello, Web!'];
2729
})->middleware('web');
@@ -34,4 +36,9 @@ protected function setRoutes($app): void
3436
return ['data' => 'Hello, Custom!'];
3537
});
3638
}
39+
40+
protected function setConfig(Repository $config): void
41+
{
42+
$config->set('http.response.json', $this->groups);
43+
}
3744
}

0 commit comments

Comments
 (0)