Exemplo n.º 1
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable|null $next
  * @return EmptyResponse
  * @throws \Exception
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     parent::__invoke($request, $response, $next);
     $this->action();
     $this->response = new JsonResponse($this->actionResult);
     return $this->next();
 }
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable|null $next
  * @return EmptyResponse
  * @throws \Exception
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     parent::__invoke($request, $response, $next);
     if ($response instanceof FileContentResponse || $response instanceof FileUploadedResponse) {
         $resourceDO = $this->resourceDO;
         $filePath = $resourceDO->getFilePath();
         if (empty($filePath)) {
             throw new WrongResponseException('Empty file path. File can\'t be saved.');
         }
         $resourceStream = $response->getResource();
         if (is_resource($resourceStream)) {
             $this->save($resourceDO, $resourceStream);
         } else {
             $body = $response->getContent();
             // If any previous middlewares have not created the resource content, just skip it.
             // It is ok in some special cases like image resizing, when generator should not do anything,
             // but next middlewares will create resource from original model.
             if (null !== $body) {
                 $this->save($resourceDO, $body);
             }
         }
         $this->response = new EmptyResponse($response->getStatusCode(), ['Content-Type' => $this->resourceDO->getMimeType()]);
     }
     return $this->next();
 }
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     parent::__invoke($request, $response, $next);
     if (!$this->fillResource()) {
         return new EmptyResponse(404, $response->getHeaders());
     }
     // Pass the resource to the next middleware
     $response = new ResourceDoResponse($this->resourceDO, $response->getStatusCode(), $response->getHeaders());
     return $next($request, $response);
 }
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     parent::__invoke($request, $response, $next);
     if ($this->isSupportedResponse($response)) {
         $uri = $this->getUri($this->resourceDO);
         $data = ['resource' => $this->resourceDO->toArray(), 'uri' => $uri];
         if (!empty($data)) {
             $headers = $response->getHeaders();
             $headers['Content-Type'] = 'application/json';
             // here is relative link without host url
             $headers['Link'] = '<' . rawurlencode($uri) . '>; rel="canonical"';
             $response = $this->getResponseObject($data, $response->getStatusCode(), $headers);
         }
     }
     return $next($request, $response);
 }