22
22
use Closure ;
23
23
use Illuminate \Contracts \Container \Container as IlluminateContainer ;
24
24
use Illuminate \Http \Request ;
25
- use Illuminate \Pagination \AbstractPaginator ;
26
25
use LaravelJsonApi \Contracts \Routing \Route as RouteContract ;
27
26
use LaravelJsonApi \Contracts \Server \Repository ;
28
27
use LaravelJsonApi \Contracts \Server \Server ;
28
+ use LaravelJsonApi \Eloquent \Pagination \PagePagination ;
29
29
use LaravelJsonApi \Laravel \Routing \Route ;
30
30
31
31
class BootJsonApi
@@ -63,6 +63,10 @@ public function __construct(IlluminateContainer $container, Repository $servers)
63
63
*/
64
64
public function handle ($ request , Closure $ next , string $ name )
65
65
{
66
+ /**
67
+ * When handling a HTTP request, both the JSON:API server and
68
+ * request classes can be singletons bound into the container.
69
+ */
66
70
$ this ->container ->instance (
67
71
Server::class,
68
72
$ server = $ this ->servers ->server ($ name )
@@ -73,25 +77,29 @@ public function handle($request, Closure $next, string $name)
73
77
$ route = new Route ($ this ->container , $ server , $ request ->route ())
74
78
);
75
79
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
+ */
77
94
$ route ->substituteBindings ();
78
- $ this ->bindPageResolver ();
79
95
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 ();
82
102
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 );
96
104
}
97
105
}
0 commit comments