diff --git a/README.md b/README.md index 78a75da..8acc3d2 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,25 @@ Register this service provider to your `bootstrap/app.php` file. $app->register('EllipseSynergie\ApiResponse\Laravel\LumenServiceProvider'); ``` +#### Install in Lumen 5.4+ + +Because of the request object change ([see reference](https://laravel-news.com/request-object-changes-in-lumen-5-4)) you can't no longuer access Request object properly in Service provider. To be convenient, we have created a middleware to be use for parsing include parameter. + +Register this service provider to your `bootstrap/app.php` file. + + +```php +$app->register('EllipseSynergie\ApiResponse\Laravel\LumenServiceProvider'); +``` + +Register the global middleware `bootstrap/app.php` file. + +```php +$app->middleware([ + 'EllipseSynergie\ApiResponse\Laravel\Middleware\ParseInclude' +]); +``` + #### Install in your favorite framework or vanilla php This package can be used in ANY framework or vanilla php. You simply need to extend `EllipseSynergie\ApiResponse\AbstractResponse` and implement the `withArray()` method in your custom class. You can take a look at `EllipseSynergie\ApiResponse\Laravel\Response::withArray()` for a example. diff --git a/composer.json b/composer.json index 510fee6..756d1a7 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "require-dev": { "phpunit/phpunit": "~4.0", "mockery/mockery": "~0.9", - "illuminate/contracts": "~5.0" + "illuminate/contracts": "~5.0", + "illuminate/http": "~5.0" }, "autoload": { "psr-4": { diff --git a/src/Laravel/Middleware/ParseInclude.php b/src/Laravel/Middleware/ParseInclude.php new file mode 100644 index 0000000..b4ec124 --- /dev/null +++ b/src/Laravel/Middleware/ParseInclude.php @@ -0,0 +1,43 @@ +response = $response; + } + + /** + * Handle middleware + * + * @param Request $request + * @param callable $next + * @return mixed + */ + public function handle(Request $request, Closure $next) + { + // Are we going to try and include embedded data? + $this->response->getManager()->parseIncludes(explode(',', $request->get('include'))); + + return $next($next); + } +} diff --git a/tests/Laravel/Middleware/ParseIncludeTest.php b/tests/Laravel/Middleware/ParseIncludeTest.php new file mode 100644 index 0000000..0e183c6 --- /dev/null +++ b/tests/Laravel/Middleware/ParseIncludeTest.php @@ -0,0 +1,35 @@ + 'foo,bar']); + + $result = $middleware->handle($request, function () { + return 'callback working !'; + }); + + $includes = $response->getManager()->getRequestedIncludes(); + + $this->assertSame($manager, $response->getManager()); + $this->assertSame([ + 'foo', + 'bar' + ], $includes); + $this->assertSame('callback working !', $result); + } +}