Skip to content

Commit b73cc68

Browse files
committed
[Feature] Allow dependency injection in the serving hook
Allows a developer to type-hint dependencies in their server's `serving` method, so that any dependencies can be injected. See #44
1 parent 659c37b commit b73cc68

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. This projec
1111
full details on how to apply this to resource schemas, refer to the new *Soft Deleting* chapter in the documentation.
1212
- Multi-resource models are now supported. This allows developers to represent a single model class as multiple
1313
different JSON:API resource types within an API. Refer to documentation for details of how to implement.
14+
- Developers can now type-hint dependencies in their server's `serving()` method.
1415

1516
## [1.0.0-alpha.4] - 2021-02-27
1617

src/Http/Middleware/BootJsonApi.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
use Closure;
2323
use Illuminate\Contracts\Container\Container as IlluminateContainer;
2424
use Illuminate\Http\Request;
25-
use Illuminate\Pagination\AbstractPaginator;
2625
use LaravelJsonApi\Contracts\Routing\Route as RouteContract;
2726
use LaravelJsonApi\Contracts\Server\Repository;
2827
use LaravelJsonApi\Contracts\Server\Server;
28+
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
2929
use LaravelJsonApi\Laravel\Routing\Route;
3030

3131
class BootJsonApi
@@ -63,6 +63,10 @@ public function __construct(IlluminateContainer $container, Repository $servers)
6363
*/
6464
public function handle($request, Closure $next, string $name)
6565
{
66+
/**
67+
* When handling a HTTP request, both the JSON:API server and
68+
* request classes can be singletons bound into the container.
69+
*/
6670
$this->container->instance(
6771
Server::class,
6872
$server = $this->servers->server($name)
@@ -73,25 +77,29 @@ public function handle($request, Closure $next, string $name)
7377
$route = new Route($this->container, $server, $request->route())
7478
);
7579

76-
$server->serving();
80+
/**
81+
* Before we do anything, we must ensure the server is set up to
82+
* handle a HTTP request. We do that by invoking the `serving()`
83+
* hook on the server instance.
84+
*/
85+
if (method_exists($server, 'serving')) {
86+
$this->container->call([$server, 'serving']);
87+
}
88+
89+
/**
90+
* Once the server is set up, we can substitute bindings. This must
91+
* happen after the `serving` hook, in case that hook has added any
92+
* Eloquent scopes.
93+
*/
7794
$route->substituteBindings();
78-
$this->bindPageResolver();
7995

80-
return $next($request);
81-
}
96+
/**
97+
* We will also override the Laravel page resolver, as we know this is
98+
* a JSON:API request, and the specification would have the page number
99+
* nested under the `page` query parameter.
100+
*/
101+
PagePagination::bindPageResolver();
82102

83-
/**
84-
* Override the page resolver to read the page parameter from the JSON API request.
85-
*
86-
* @return void
87-
*/
88-
protected function bindPageResolver(): void
89-
{
90-
/** Override the current page resolution */
91-
AbstractPaginator::currentPageResolver(static function ($pageName) {
92-
$pagination = \request()->query($pageName);
93-
94-
return $pagination['number'] ?? null;
95-
});
103+
return $next($request);
96104
}
97105
}

0 commit comments

Comments
 (0)