public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = $handler->handle($request);
        $code = $response->getStatusCode();
        $allowedCodes = array_keys($this->renderer) + array_keys($this->defaultErrors);
        if (in_array($code, $allowedCodes)) {
            if (!$response->getBody()->getSize()) {
                $format = FormatNegotiator::getPreferredFormat($request);

                if (isset($this->renderer[$code])) {
                    $content = $this->renderer[$code]($format);
                } else {
                    $content = $this->defaultRenderer($code, $format);
                }

                if (is_array($content)) {
                    $content = json_encode($content);
                }

                $body = $this->streamFactory->createStream($content);

                $response = $response->withBody($body);
                $response = $response->withStatus($code);

                if ($format === 'json') {
                    $response = $response->withHeader('content-type', 'application/json');
                } else {
                    $response = $response->withHeader('content-type', 'text/html; charset=utf-8');
                }
            }
        }

        return $response;
    }
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = $handler->handle($request);
        $code = $response->getStatusCode();
        $allowedCodes = array_keys($this->renderer) + array_keys($this->defaultErrors);
        if (in_array($code, $allowedCodes)) {
            if (!$response->getBody()->getSize()) {
                $format = FormatNegotiator::getPreferredFormat($request);

                if (isset($this->renderer[$code])) {
                    $content = $this->renderer[$code]($format);
                } else {
                    $content = $this->defaultRenderer($code, $format);
                }

                if (is_array($content)) {
                    $content = json_encode($content);
                }

                $body = $this->streamFactory->createStream($content);

                $response = $response->withBody($body);
                $response = $response->withStatus($code);

                if ($format === 'json') {
                    $response = $response->withHeader('content-type', 'application/json');
                } else {
                    $response = $response->withHeader('content-type', 'text/html; charset=utf-8');
                }
            }
        }

        return $response;
    }
public function process ( ServerRequestInterface $ request , RequestHandlerInterface $ handler ) : ResponseInterface { $ response = $ handler -> handle ( $ request ) ; $ code = $ response -> getStatusCode ( ) ; $ allowedCodes = array_keys ( $ this -> renderer ) + array_keys ( $ this -> defaultErrors ) ; if ( in_array ( $ code , $ allowedCodes ) ) { if ( ! $ response -> getBody ( ) -> getSize ( ) ) { $ format = FormatNegotiator :: getPreferredFormat ( $ request ) ; if ( isset ( $ this -> renderer [ $ code ] ) ) { $ content = $ this -> renderer [ $ code ] ( $ format ) ; } else { $ content = $ this -> defaultRenderer ( $ code , $ format ) ; } if ( is_array ( $ content ) ) { $ content = json_encode ( $ content ) ; } $ body = $ this -> streamFactory -> createStream ( $ content ) ; $ response = $ response -> withBody ( $ body ) ; $ response = $ response -> withStatus ( $ code ) ; if ( $ format === 'json' ) { $ response = $ response -> withHeader ( 'content-type' , 'application/json' ) ; } else { $ response = $ response -> withHeader ( 'content-type' , 'text/html; charset=utf-8' ) ; } } } return $ response ; }
Execute the middleware.
Execute the middleware .