Skip to content

Commit a2c552c

Browse files
author
Andrey Helldar
authored
Merge pull request #12 from TheDragonCode/2.x
Fixed loading middleware
2 parents 689e200 + 595e885 commit a2c552c

File tree

9 files changed

+47
-922
lines changed

9 files changed

+47
-922
lines changed

.editorconfig

Lines changed: 0 additions & 787 deletions
This file was deleted.

.styleci.yml

Lines changed: 0 additions & 67 deletions
This file was deleted.

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ After you've installed the package via composer, you're done. There's no step tw
4040
This package will automatically register the `DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware` middleware in the `web` and `api` groups, if they
4141
exist. The middleware will add a header `Accept` that will effectively convert all responses to JSON format. This header will apply to all responses.
4242

43+
## Troubleshooting
44+
45+
> Why are my error messages not being converted to JSON?
46+
47+
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.
48+
49+
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.
50+
4351

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

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"illuminate/contracts": "^6.0 || ^7.0 || ^8.0 || ^9.0",
2828
"illuminate/http": "^6.0 || ^7.0 || ^8.0 || ^9.0",
2929
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0",
30-
"lmc/http-constants": "^1.2"
30+
"lmc/http-constants": "^1.2",
31+
"symfony/http-foundation": "^4.3 || ^5.0 || ^6.0"
3132
},
3233
"require-dev": {
3334
"orchestra/testbench": "^4.0 || ^5.0 || ^6.0 || ^7.0",

src/Middlewares/SetHeaderMiddleware.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,20 @@ class SetHeaderMiddleware
1616
*/
1717
public function handle(Request $request, Closure $next)
1818
{
19-
/** @var \Illuminate\Http\Response $response */
2019
$response = $next($request);
2120

22-
return $response->header(Header::ACCEPT, 'application/json');
21+
$this->set($response);
22+
23+
return $response;
24+
}
25+
26+
/**
27+
* @param \Illuminate\Http\JsonResponse|\Illuminate\Http\Response $response
28+
*
29+
* @return void
30+
*/
31+
protected function set($response)
32+
{
33+
$response->headers->set(Header::ACCEPT, 'application/json');
2334
}
2435
}

src/ServiceProvider.php

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,15 @@
44

55
use DragonCode\LaravelJsonResponse\Middlewares\SetHeaderMiddleware;
66
use Illuminate\Contracts\Http\Kernel;
7-
use Illuminate\Support\Arr;
87
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
98

109
class ServiceProvider extends BaseServiceProvider
1110
{
12-
protected $groups = ['web', 'api'];
11+
protected $middleware = SetHeaderMiddleware::class;
1312

1413
public function boot(): void
1514
{
16-
foreach ($this->groups as $group) {
17-
$this->prepend($group, SetHeaderMiddleware::class);
18-
}
19-
}
20-
21-
protected function prepend(string $group, string $middleware): void
22-
{
23-
if ($this->has($group, $middleware)) {
24-
$this->resolve()->prependMiddlewareToGroup($group, $middleware);
25-
}
26-
}
27-
28-
protected function has(string $group, string $middleware): bool
29-
{
30-
return $this->hasGroup($group) && $this->doesntMiddleware($group, $middleware);
31-
}
32-
33-
protected function hasGroup(string $group): bool
34-
{
35-
return Arr::has($this->getGroups(), $group);
36-
}
37-
38-
protected function doesntMiddleware(string $group, string $middleware): bool
39-
{
40-
$group = Arr::get($this->getGroups(), $group);
41-
42-
return ! in_array($middleware, $group);
43-
}
44-
45-
protected function getGroups(): array
46-
{
47-
return $this->resolve()->getMiddlewareGroups();
15+
$this->resolve()->prependMiddleware($this->middleware);
4816
}
4917

5018
protected function resolve(): Kernel

tests/Middlewares/DuplicateTest.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/Middlewares/SetHeaderMiddlewareTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testWeb(): void
1414
->assertSuccessful()
1515
->assertHeader(Header::ACCEPT, 'application/json')
1616
->assertJsonStructure(['data'])
17-
->assertJson(['data' => 'Hello, JSON!']);
17+
->assertJson(['data' => 'Hello, Web!']);
1818
}
1919

2020
public function testApi(): void
@@ -24,6 +24,16 @@ public function testApi(): void
2424
->assertSuccessful()
2525
->assertHeader(Header::ACCEPT, 'application/json')
2626
->assertJsonStructure(['data'])
27-
->assertJson(['data' => 'Hello, JSON!']);
27+
->assertJson(['data' => 'Hello, Api!']);
28+
}
29+
30+
public function testCustom(): void
31+
{
32+
$this
33+
->get('custom')
34+
->assertSuccessful()
35+
->assertHeader(Header::ACCEPT, 'application/json')
36+
->assertJsonStructure(['data'])
37+
->assertJson(['data' => 'Hello, Custom!']);
2838
}
2939
}

tests/TestCase.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Tests;
44

55
use DragonCode\LaravelJsonResponse\ServiceProvider;
6-
use Illuminate\Contracts\Http\Kernel;
76
use Orchestra\Testbench\TestCase as BaseTestCase;
87

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

2120
protected function setRoutes($app): void
2221
{
23-
$app['router']->get('web', function () {
24-
return ['data' => 'Hello, JSON!'];
22+
/** @var \Illuminate\Routing\RouteRegistrar $router */
23+
$router = $app['router'];
24+
25+
$router->get('web', function () {
26+
return ['data' => 'Hello, Web!'];
2527
})->middleware('web');
2628

27-
$app['router']->get('api', function () {
28-
return ['data' => 'Hello, JSON!'];
29+
$router->get('api', function () {
30+
return ['data' => 'Hello, Api!'];
2931
})->middleware('api');
30-
}
3132

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

0 commit comments

Comments
 (0)