protected function finalize($result) {
        $response = Response::create($result);
        $response->meta(['request' => $this->request], true);
        $response->contentTypeFromAccept($this->request->getEnv('HTTP_ACCEPT'));
        $response->contentAsset($this->request->getEnv('HTTP_X_ASSET'));

        $contentType = $response->contentType();

        if ($this->request->getMethod() === Request::METHOD_HEAD) {
            $response->flushHeaders();
            return null;
        }

        // Check for known response types.
        switch ($contentType) {
            case 'application/internal':
                if ($result instanceof \Exception) {
                    throw $result;
                }

                if ($response->contentAsset() === 'response') {
                    return $response;
                } else {
                    return $response->jsonSerialize();
                }
                // No break because everything returns.
            case 'application/json':
                $response->flushHeaders();
                echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
                break;
            default:
                $data = $response->data();
                if (is_string($data)) {
                    $response->flushHeaders();
                    echo $data;
                } else {
                    $response->status(415);
                    $response->flushHeaders();
                    echo "Unsupported response type: $contentType";
                }
                break;
        }
        return null;
    }
protected function finalize($result) {
        $response = Response::create($result);
        $response->meta(['request' => $this->request], true);
        $response->contentTypeFromAccept($this->request->getEnv('HTTP_ACCEPT'));
        $response->contentAsset($this->request->getEnv('HTTP_X_ASSET'));

        $contentType = $response->contentType();

        if ($this->request->getMethod() === Request::METHOD_HEAD) {
            $response->flushHeaders();
            return null;
        }

        // Check for known response types.
        switch ($contentType) {
            case 'application/internal':
                if ($result instanceof \Exception) {
                    throw $result;
                }

                if ($response->contentAsset() === 'response') {
                    return $response;
                } else {
                    return $response->jsonSerialize();
                }
                // No break because everything returns.
            case 'application/json':
                $response->flushHeaders();
                echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
                break;
            default:
                $data = $response->data();
                if (is_string($data)) {
                    $response->flushHeaders();
                    echo $data;
                } else {
                    $response->status(415);
                    $response->flushHeaders();
                    echo "Unsupported response type: $contentType";
                }
                break;
        }
        return null;
    }
protected function finalize ( $ result ) { $ response = Response :: create ( $ result ) ; $ response -> meta ( [ 'request' => $ this -> request ] , true ) ; $ response -> contentTypeFromAccept ( $ this -> request -> getEnv ( 'HTTP_ACCEPT' ) ) ; $ response -> contentAsset ( $ this -> request -> getEnv ( 'HTTP_X_ASSET' ) ) ; $ contentType = $ response -> contentType ( ) ; if ( $ this -> request -> getMethod ( ) === Request :: METHOD_HEAD ) { $ response -> flushHeaders ( ) ; return null ; } // Check for known response types. switch ( $ contentType ) { case 'application/internal' : if ( $ result instanceof \ Exception ) { throw $ result ; } if ( $ response -> contentAsset ( ) === 'response' ) { return $ response ; } else { return $ response -> jsonSerialize ( ) ; } // No break because everything returns. case 'application/json' : $ response -> flushHeaders ( ) ; echo json_encode ( $ response , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) ; break ; default : $ data = $ response -> data ( ) ; if ( is_string ( $ data ) ) { $ response -> flushHeaders ( ) ; echo $ data ; } else { $ response -> status ( 415 ) ; $ response -> flushHeaders ( ) ; echo "Unsupported response type: $contentType" ; } break ; } return null ; }
Finalize the result from a dispatch.

@param mixed $result The result of the dispatch.
@return mixed Returns relevant debug data or processes the response.
@throws \Exception Throws an exception when finalizing internal content types and the result is an exception.
Finalize the result from a dispatch .