-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ParseInclude middleware to support Lumen 5.4
- Loading branch information
1 parent
4f64229
commit 25b8d2d
Showing
4 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |