/**
  * Start JSON API support.
  *
  * This middleware:
  * - Loads the configuration for the named API that this request is being routed to.
  * - Registers the API in the service container.
  * - Triggers client/server content negotiation as per the JSON API spec.
  *
  * @param Request $request
  * @param Closure $next
  * @param $namespace
  *      the API namespace, as per your JSON API configuration.
  * @return mixed
  */
 public function handle($request, Closure $next, $namespace)
 {
     /** @var ApiFactory $factory */
     $factory = $this->container->make(ApiFactoryInterface::class);
     /** @var ServerRequestInterface $request */
     $serverRequest = $this->container->make(ServerRequestInterface::class);
     /** @var RequestFactoryInterface $requestFactory */
     $requestFactory = $this->container->make(RequestFactoryInterface::class);
     /** Build and register the API */
     $api = $factory->createApi($namespace, $request->getSchemeAndHttpHost());
     $this->container->instance(ApiInterface::class, $api);
     /** Build and register the JSON API request */
     $jsonApiRequest = $requestFactory->build($api, $serverRequest);
     $this->container->instance(RequestInterface::class, $jsonApiRequest);
     /** Override the current page resolution */
     AbstractPaginator::currentPageResolver(function () {
         /** @var PaginatorInterface $paginator */
         $paginator = $this->container->make(PaginatorInterface::class);
         return $paginator->getCurrentPage();
     });
     return $next($request);
 }