Skip to content

Fixed loading middleware #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
787 changes: 0 additions & 787 deletions .editorconfig

This file was deleted.

67 changes: 0 additions & 67 deletions .styleci.yml

This file was deleted.

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ After you've installed the package via composer, you're done. There's no step tw
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.

## Troubleshooting

> Why are my error messages not being converted to JSON?

This is a feature of the [Laravel architecture](https://github.com/laravel/framework/blob/9.x/src/Illuminate/Foundation/Http/Kernel.php#L108-L116). The package makes corrections during the loading of the framework and it stores its state internally, and in case of an error, it takes the value of the request from the outside. Therefore, the error text is returned in html format.

To fix this problem, you need to explicitly pass the `Accept: application/json` header in requests, or use the [`dragon-code/api-response`](https://github.com/TheDragonCode/api-response#best-practice-use-with-the-laravel-and-lumen-frameworks) package.


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

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"illuminate/contracts": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"illuminate/http": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"lmc/http-constants": "^1.2"
"lmc/http-constants": "^1.2",
"symfony/http-foundation": "^4.3 || ^5.0 || ^6.0"
},
"require-dev": {
"orchestra/testbench": "^4.0 || ^5.0 || ^6.0 || ^7.0",
Expand Down
15 changes: 13 additions & 2 deletions src/Middlewares/SetHeaderMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ class SetHeaderMiddleware
*/
public function handle(Request $request, Closure $next)
{
/** @var \Illuminate\Http\Response $response */
$response = $next($request);

return $response->header(Header::ACCEPT, 'application/json');
$this->set($response);

return $response;
}

/**
* @param \Illuminate\Http\JsonResponse|\Illuminate\Http\Response $response
*
* @return void
*/
protected function set($response)
{
$response->headers->set(Header::ACCEPT, 'application/json');
}
}
36 changes: 2 additions & 34 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,15 @@

use DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
{
protected $groups = ['web', 'api'];
protected $middleware = SetHeaderMiddleware::class;

public function boot(): void
{
foreach ($this->groups as $group) {
$this->prepend($group, SetHeaderMiddleware::class);
}
}

protected function prepend(string $group, string $middleware): void
{
if ($this->has($group, $middleware)) {
$this->resolve()->prependMiddlewareToGroup($group, $middleware);
}
}

protected function has(string $group, string $middleware): bool
{
return $this->hasGroup($group) && $this->doesntMiddleware($group, $middleware);
}

protected function hasGroup(string $group): bool
{
return Arr::has($this->getGroups(), $group);
}

protected function doesntMiddleware(string $group, string $middleware): bool
{
$group = Arr::get($this->getGroups(), $group);

return ! in_array($middleware, $group);
}

protected function getGroups(): array
{
return $this->resolve()->getMiddlewareGroups();
$this->resolve()->prependMiddleware($this->middleware);
}

protected function resolve(): Kernel
Expand Down
20 changes: 0 additions & 20 deletions tests/Middlewares/DuplicateTest.php

This file was deleted.

14 changes: 12 additions & 2 deletions tests/Middlewares/SetHeaderMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testWeb(): void
->assertSuccessful()
->assertHeader(Header::ACCEPT, 'application/json')
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, JSON!']);
->assertJson(['data' => 'Hello, Web!']);
}

public function testApi(): void
Expand All @@ -24,6 +24,16 @@ public function testApi(): void
->assertSuccessful()
->assertHeader(Header::ACCEPT, 'application/json')
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, JSON!']);
->assertJson(['data' => 'Hello, Api!']);
}

public function testCustom(): void
{
$this
->get('custom')
->assertSuccessful()
->assertHeader(Header::ACCEPT, 'application/json')
->assertJsonStructure(['data'])
->assertJson(['data' => 'Hello, Custom!']);
}
}
19 changes: 10 additions & 9 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tests;

use DragonCode\LaravelJsonResponse\ServiceProvider;
use Illuminate\Contracts\Http\Kernel;
use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
Expand All @@ -20,17 +19,19 @@ protected function getEnvironmentSetUp($app)

protected function setRoutes($app): void
{
$app['router']->get('web', function () {
return ['data' => 'Hello, JSON!'];
/** @var \Illuminate\Routing\RouteRegistrar $router */
$router = $app['router'];

$router->get('web', function () {
return ['data' => 'Hello, Web!'];
})->middleware('web');

$app['router']->get('api', function () {
return ['data' => 'Hello, JSON!'];
$router->get('api', function () {
return ['data' => 'Hello, Api!'];
})->middleware('api');
}

protected function getMiddlewareGroups(): array
{
return $this->app->make(Kernel::class)->getMiddlewareGroups();
$router->get('custom', function () {
return ['data' => 'Hello, Custom!'];
});
}
}