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

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

        if (!$attributes) {
            $this->serializeRawData($event, $request, $controllerResult);

            return;
        }

        $context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);

        if (isset($context['output']) && \array_key_exists('class', $context['output']) && null === $context['output']['class']) {
            $event->setControllerResult(null);

            return;
        }

        if ($included = $request->attributes->get('_api_included')) {
            $context['api_included'] = $included;
        }
        $resources = new ResourceList();
        $context['resources'] = &$resources;

        $resourcesToPush = new ResourceList();
        $context['resources_to_push'] = &$resourcesToPush;

        $request->attributes->set('_api_normalization_context', $context);

        $event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context));

        $request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources);
        if (!\count($resourcesToPush)) {
            return;
        }

        $linkProvider = $request->attributes->get('_links', new GenericLinkProvider());
        foreach ($resourcesToPush as $resourceToPush) {
            $linkProvider = $linkProvider->withLink(new Link('preload', $resourceToPush));
        }
        $request->attributes->set('_links', $linkProvider);
    }
public function onKernelView(GetResponseForControllerResultEvent $event): void
    {
        $controllerResult = $event->getControllerResult();
        $request = $event->getRequest();

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

        if (!$attributes) {
            $this->serializeRawData($event, $request, $controllerResult);

            return;
        }

        $context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);

        if (isset($context['output']) && \array_key_exists('class', $context['output']) && null === $context['output']['class']) {
            $event->setControllerResult(null);

            return;
        }

        if ($included = $request->attributes->get('_api_included')) {
            $context['api_included'] = $included;
        }
        $resources = new ResourceList();
        $context['resources'] = &$resources;

        $resourcesToPush = new ResourceList();
        $context['resources_to_push'] = &$resourcesToPush;

        $request->attributes->set('_api_normalization_context', $context);

        $event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context));

        $request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources);
        if (!\count($resourcesToPush)) {
            return;
        }

        $linkProvider = $request->attributes->get('_links', new GenericLinkProvider());
        foreach ($resourcesToPush as $resourceToPush) {
            $linkProvider = $linkProvider->withLink(new Link('preload', $resourceToPush));
        }
        $request->attributes->set('_links', $linkProvider);
    }
public function onKernelView ( GetResponseForControllerResultEvent $ event ) : void { $ controllerResult = $ event -> getControllerResult ( ) ; $ request = $ event -> getRequest ( ) ; if ( $ controllerResult instanceof Response || ! ( ( $ attributes = RequestAttributesExtractor :: extractAttributes ( $ request ) ) [ 'respond' ] ?? $ request -> attributes -> getBoolean ( '_api_respond' , false ) ) ) { return ; } if ( ! $ attributes ) { $ this -> serializeRawData ( $ event , $ request , $ controllerResult ) ; return ; } $ context = $ this -> serializerContextBuilder -> createFromRequest ( $ request , true , $ attributes ) ; if ( isset ( $ context [ 'output' ] ) && \ array_key_exists ( 'class' , $ context [ 'output' ] ) && null === $ context [ 'output' ] [ 'class' ] ) { $ event -> setControllerResult ( null ) ; return ; } if ( $ included = $ request -> attributes -> get ( '_api_included' ) ) { $ context [ 'api_included' ] = $ included ; } $ resources = new ResourceList ( ) ; $ context [ 'resources' ] = & $ resources ; $ resourcesToPush = new ResourceList ( ) ; $ context [ 'resources_to_push' ] = & $ resourcesToPush ; $ request -> attributes -> set ( '_api_normalization_context' , $ context ) ; $ event -> setControllerResult ( $ this -> serializer -> serialize ( $ controllerResult , $ request -> getRequestFormat ( ) , $ context ) ) ; $ request -> attributes -> set ( '_resources' , $ request -> attributes -> get ( '_resources' , [ ] ) + ( array ) $ resources ) ; if ( ! \ count ( $ resourcesToPush ) ) { return ; } $ linkProvider = $ request -> attributes -> get ( '_links' , new GenericLinkProvider ( ) ) ; foreach ( $ resourcesToPush as $ resourceToPush ) { $ linkProvider = $ linkProvider -> withLink ( new Link ( 'preload' , $ resourceToPush ) ) ; } $ request -> attributes -> set ( '_links' , $ linkProvider ) ; }
Serializes the data to the requested format.
Serializes the data to the requested format .