2222use Closure ;
2323use Illuminate \Contracts \Container \Container as IlluminateContainer ;
2424use Illuminate \Http \Request ;
25- use Illuminate \Pagination \AbstractPaginator ;
2625use LaravelJsonApi \Contracts \Routing \Route as RouteContract ;
2726use LaravelJsonApi \Contracts \Server \Repository ;
2827use LaravelJsonApi \Contracts \Server \Server ;
28+ use LaravelJsonApi \Eloquent \Pagination \PagePagination ;
2929use LaravelJsonApi \Laravel \Routing \Route ;
3030
3131class 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