Skip to content

Commit

Permalink
Create ParseInclude middleware to support Lumen 5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
maximebeaudoin committed May 10, 2017
1 parent 4f64229 commit 25b8d2d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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 @@
"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": {
Expand Down
43 changes: 43 additions & 0 deletions src/Laravel/Middleware/ParseInclude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace EllipseSynergie\ApiResponse\Laravel\Middleware;

use Closure;
use EllipseSynergie\ApiResponse\Contracts\Response;
use Illuminate\Http\Request;

/**
* Class ParseInclude
* @package EllipseSynergie\ApiResponse\Laravel\Middleware
*/
class ParseInclude
{
/**
* @var Response
*/
private $response;

/**
* ParseInclude constructor.
* @param Response $response
*/
public function __construct(Response $response)
{
$this->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);
}
}
35 changes: 35 additions & 0 deletions tests/Laravel/Middleware/ParseIncludeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace EllipseSynergie\ApiResponse\Tests\Laravel\Middleware;

use EllipseSynergie\ApiResponse\Laravel\Middleware\ParseInclude;
use EllipseSynergie\ApiResponse\Laravel\Response;
use Illuminate\Http\Request;
use League\Fractal\Manager;

class ParseIncludeTest extends \PHPUnit_Framework_TestCase
{
public function testHandleWorkProperly()
{
$manager = new Manager();

$response = new Response($manager);

$middleware = new ParseInclude($response);

$request = Request::create('foo', 'GET', ['include' => '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);
}
}

0 comments on commit 25b8d2d

Please sign in to comment.