diff --git a/README.md b/README.md index 0213bc4..a2f1809 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,10 @@ Or manually update `require` block of `composer.json` and run `composer update`. After you've installed the package via composer, you're done. There's no step two. -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. +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. + +> If you need to redefine the header for specific groups of routes, you can do this by changing the [`settings`](config/http.php). [badge_build]: https://img.shields.io/github/workflow/status/TheDragonCode/laravel-json-response/phpunit?style=flat-square diff --git a/config/http.php b/config/http.php new file mode 100644 index 0000000..c3e3b75 --- /dev/null +++ b/config/http.php @@ -0,0 +1,21 @@ + [ + /* + * This setting is responsible for applying middleware to routes. + * + * You can specify route group names to apply the `SetHeaderMiddleware` + * middleware to them, or specify null to apply it to all routes. + * + * For example, + * + * json => null + * json => 'api' + * json => ['api', 'web'] + */ + 'json' => null, + ], +]; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 984316a..0d52d20 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -4,6 +4,7 @@ use DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware; use Illuminate\Contracts\Http\Kernel; +use Illuminate\Support\Arr; use Illuminate\Support\ServiceProvider as BaseServiceProvider; class ServiceProvider extends BaseServiceProvider @@ -12,7 +13,55 @@ class ServiceProvider extends BaseServiceProvider public function boot(): void { - $this->resolve()->prependMiddleware($this->middleware); + $this->publishConfig(); + + $this->registerMiddleware($this->middlewareGroups()); + } + + public function register(): void + { + $this->registerConfig(); + } + + protected function registerMiddleware(array $groups): void + { + $this->toAll($groups) + ? $this->resolve()->prependMiddleware($this->middleware) + : $this->prependMiddlewareToGroups($this->middleware, $groups); + } + + protected function prependMiddlewareToGroups(string $middleware, array $groups): void + { + foreach ($groups as $group) { + $this->resolve()->prependMiddlewareToGroup($group, $middleware); + } + } + + protected function toAll(array $groups): bool + { + return empty($groups); + } + + protected function middlewareGroups(): array + { + return array_filter(Arr::wrap( + $this->app['config']->get('http.response.json') + )); + } + + protected function publishConfig(): void + { + $this->publishes([ + __DIR__ . '/../config/http.php' => $this->app->configPath('http.php'), + ]); + } + + protected function registerConfig(): void + { + $this->mergeConfigFrom( + __DIR__ . '/../config/http.php', + 'http' + ); } protected function resolve(): Kernel diff --git a/tests/Middlewares/SetHeaderMiddlewareTest.php b/tests/Middlewares/AllTest.php similarity index 97% rename from tests/Middlewares/SetHeaderMiddlewareTest.php rename to tests/Middlewares/AllTest.php index a31f29b..a4a2f9e 100644 --- a/tests/Middlewares/SetHeaderMiddlewareTest.php +++ b/tests/Middlewares/AllTest.php @@ -5,26 +5,26 @@ use Lmc\HttpConstants\Header; use Tests\TestCase; -class SetHeaderMiddlewareTest extends TestCase +class AllTest extends TestCase { - public function testWeb(): void + public function testApi(): void { $this - ->get('web') + ->get('api') ->assertSuccessful() ->assertHeader(Header::ACCEPT, 'application/json') ->assertJsonStructure(['data']) - ->assertJson(['data' => 'Hello, Web!']); + ->assertJson(['data' => 'Hello, Api!']); } - public function testApi(): void + public function testWeb(): void { $this - ->get('api') + ->get('web') ->assertSuccessful() ->assertHeader(Header::ACCEPT, 'application/json') ->assertJsonStructure(['data']) - ->assertJson(['data' => 'Hello, Api!']); + ->assertJson(['data' => 'Hello, Web!']); } public function testCustom(): void diff --git a/tests/Middlewares/ApiAndWebTest.php b/tests/Middlewares/ApiAndWebTest.php new file mode 100644 index 0000000..4df071c --- /dev/null +++ b/tests/Middlewares/ApiAndWebTest.php @@ -0,0 +1,61 @@ +get('api') + ->assertSuccessful() + ->assertHeader(Header::ACCEPT, 'application/json') + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Api!']); + } + + public function testWeb(): void + { + $this + ->get('web') + ->assertSuccessful() + ->assertHeader(Header::ACCEPT, 'application/json') + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Web!']); + } + + public function testCustom(): void + { + $this + ->get('custom') + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Custom!']); + } + + public function testCustomHeader(): void + { + $this + ->get('custom', [Header::ACCEPT => 'application/xml']) + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Custom!']); + } + + public function testAsterisk(): void + { + $this + ->get('custom', [Header::ACCEPT => '*/*']) + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Custom!']); + } +} diff --git a/tests/Middlewares/OnlyApiTest.php b/tests/Middlewares/OnlyApiTest.php new file mode 100644 index 0000000..9237592 --- /dev/null +++ b/tests/Middlewares/OnlyApiTest.php @@ -0,0 +1,61 @@ +get('api') + ->assertSuccessful() + ->assertHeader(Header::ACCEPT, 'application/json') + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Api!']); + } + + public function testWeb(): void + { + $this + ->get('web') + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Web!']); + } + + public function testCustom(): void + { + $this + ->get('custom') + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Custom!']); + } + + public function testCustomHeader(): void + { + $this + ->get('custom', [Header::ACCEPT => 'application/xml']) + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Custom!']); + } + + public function testAsterisk(): void + { + $this + ->get('custom', [Header::ACCEPT => '*/*']) + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Custom!']); + } +} diff --git a/tests/Middlewares/OnlyWebTest.php b/tests/Middlewares/OnlyWebTest.php new file mode 100644 index 0000000..815312b --- /dev/null +++ b/tests/Middlewares/OnlyWebTest.php @@ -0,0 +1,61 @@ +get('api') + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Api!']); + } + + public function testWeb(): void + { + $this + ->get('web') + ->assertSuccessful() + ->assertHeader(Header::ACCEPT, 'application/json') + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Web!']); + } + + public function testCustom(): void + { + $this + ->get('custom') + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Custom!']); + } + + public function testCustomHeader(): void + { + $this + ->get('custom', [Header::ACCEPT => 'application/xml']) + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Custom!']); + } + + public function testAsterisk(): void + { + $this + ->get('custom', [Header::ACCEPT => '*/*']) + ->assertSuccessful() + ->assertHeaderMissing(Header::ACCEPT) + ->assertJsonStructure(['data']) + ->assertJson(['data' => 'Hello, Custom!']); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 8e1b606..7018e1a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,10 +3,14 @@ namespace Tests; use DragonCode\LaravelJsonResponse\ServiceProvider; +use Illuminate\Contracts\Config\Repository; +use Illuminate\Routing\Router; use Orchestra\Testbench\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { + protected $groups; + protected function getPackageProviders($app): array { return [ServiceProvider::class]; @@ -14,14 +18,12 @@ protected function getPackageProviders($app): array protected function getEnvironmentSetUp($app) { - $this->setRoutes($app); + $this->setRoutes($app['router']); + $this->setConfig($app['config']); } - protected function setRoutes($app): void + protected function setRoutes(Router $router): void { - /** @var \Illuminate\Routing\RouteRegistrar $router */ - $router = $app['router']; - $router->get('web', function () { return ['data' => 'Hello, Web!']; })->middleware('web'); @@ -34,4 +36,9 @@ protected function setRoutes($app): void return ['data' => 'Hello, Custom!']; }); } + + protected function setConfig(Repository $config): void + { + $config->set('http.response.json', $this->groups); + } }