src/EventListener/RespondListener.php
public function onKernelView(GetResponseForControllerResultEvent $event): void
    {
        $controllerResult = $event->getControllerResult();
        $request = $event->getRequest();

        $attributes = RequestAttributesExtractor::extractAttributes($request);
        if ($controllerResult instanceof Response && ($attributes['respond'] ?? false)) {
            $event->setResponse($controllerResult);

            return;
        }
        if ($controllerResult instanceof Response || !($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond', false))) {
            return;
        }

        $headers = [
            'Content-Type' => sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
            'Vary' => 'Accept',
            'X-Content-Type-Options' => 'nosniff',
            'X-Frame-Options' => 'deny',
        ];

        if ($request->attributes->has('_api_write_item_iri')) {
            $headers['Content-Location'] = $request->attributes->get('_api_write_item_iri');

            if ($request->isMethod('POST')) {
                $headers['Location'] = $request->attributes->get('_api_write_item_iri');
            }
        }

        $status = null;
        if ($this->resourceMetadataFactory && $attributes) {
            $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);

            if ($sunset = $resourceMetadata->getOperationAttribute($attributes, 'sunset', null, true)) {
                $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123);
            }

            $status = $resourceMetadata->getOperationAttribute($attributes, 'status');
        }

        $event->setResponse(new Response(
            $controllerResult,
            $status ?? self::METHOD_TO_CODE[$request->getMethod()] ?? Response::HTTP_OK,
            $headers
        ));
    }
public function onKernelView(GetResponseForControllerResultEvent $event): void
    {
        $controllerResult = $event->getControllerResult();
        $request = $event->getRequest();

        $attributes = RequestAttributesExtractor::extractAttributes($request);
        if ($controllerResult instanceof Response && ($attributes['respond'] ?? false)) {
            $event->setResponse($controllerResult);

            return;
        }
        if ($controllerResult instanceof Response || !($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond', false))) {
            return;
        }

        $headers = [
            'Content-Type' => sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
            'Vary' => 'Accept',
            'X-Content-Type-Options' => 'nosniff',
            'X-Frame-Options' => 'deny',
        ];

        if ($request->attributes->has('_api_write_item_iri')) {
            $headers['Content-Location'] = $request->attributes->get('_api_write_item_iri');

            if ($request->isMethod('POST')) {
                $headers['Location'] = $request->attributes->get('_api_write_item_iri');
            }
        }

        $status = null;
        if ($this->resourceMetadataFactory && $attributes) {
            $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);

            if ($sunset = $resourceMetadata->getOperationAttribute($attributes, 'sunset', null, true)) {
                $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123);
            }

            $status = $resourceMetadata->getOperationAttribute($attributes, 'status');
        }

        $event->setResponse(new Response(
            $controllerResult,
            $status ?? self::METHOD_TO_CODE[$request->getMethod()] ?? Response::HTTP_OK,
            $headers
        ));
    }
public function onKernelView ( GetResponseForControllerResultEvent $ event ) : void { $ controllerResult = $ event -> getControllerResult ( ) ; $ request = $ event -> getRequest ( ) ; $ attributes = RequestAttributesExtractor :: extractAttributes ( $ request ) ; if ( $ controllerResult instanceof Response && ( $ attributes [ 'respond' ] ?? false ) ) { $ event -> setResponse ( $ controllerResult ) ; return ; } if ( $ controllerResult instanceof Response || ! ( $ attributes [ 'respond' ] ?? $ request -> attributes -> getBoolean ( '_api_respond' , false ) ) ) { return ; } $ headers = [ 'Content-Type' => sprintf ( '%s; charset=utf-8' , $ request -> getMimeType ( $ request -> getRequestFormat ( ) ) ) , 'Vary' => 'Accept' , 'X-Content-Type-Options' => 'nosniff' , 'X-Frame-Options' => 'deny' , ] ; if ( $ request -> attributes -> has ( '_api_write_item_iri' ) ) { $ headers [ 'Content-Location' ] = $ request -> attributes -> get ( '_api_write_item_iri' ) ; if ( $ request -> isMethod ( 'POST' ) ) { $ headers [ 'Location' ] = $ request -> attributes -> get ( '_api_write_item_iri' ) ; } } $ status = null ; if ( $ this -> resourceMetadataFactory && $ attributes ) { $ resourceMetadata = $ this -> resourceMetadataFactory -> create ( $ attributes [ 'resource_class' ] ) ; if ( $ sunset = $ resourceMetadata -> getOperationAttribute ( $ attributes , 'sunset' , null , true ) ) { $ headers [ 'Sunset' ] = ( new \ DateTimeImmutable ( $ sunset ) ) -> format ( \ DateTime :: RFC1123 ) ; } $ status = $ resourceMetadata -> getOperationAttribute ( $ attributes , 'status' ) ; } $ event -> setResponse ( new Response ( $ controllerResult , $ status ?? self :: METHOD_TO_CODE [ $ request -> getMethod ( ) ] ?? Response :: HTTP_OK , $ headers ) ) ; }
Creates a Response to send to the client according to the requested format.
Creates a Response to send to the client according to the requested format .