Patches response on empty responses.

@param GetResponseForControllerResultEvent $event Event instance
495288
public function onKernelView(GetResponseForControllerResultEvent $event)
    {
        $request = $event->getRequest();

        try {
            $viewName = $this->viewNameDeducer->deduce($request);
        }
        catch (NotInBundleException $e) {
            return;
        }
        catch (NoControllerNameException $e) {
            return;
        }
        $viewParams = $event->getControllerResult();
        if (!is_array($viewParams) && !is_null($viewParams)) {
            return;
        }

        if ($this->templating->exists($viewName)) {
            $response = $this->templating->renderResponse($viewName, $viewParams ?: array());
            $event->setResponse($response);
            return;
        }

        $this->missingViewHandler->handleMissingView($event, $viewName, $viewParams ?: array());
    }
1247226
public function onKernelView(GetResponseForControllerResultEvent $event): void
    {
        $request = $event->getRequest();
        if (
            $request->isMethodSafe(false)
            || $request->isMethod('DELETE')
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
            || !$attributes['receive']
        ) {
            return;
        }

        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
        $inputMetadata = $resourceMetadata->getOperationAttribute($attributes, 'input', [], true);
        if (\array_key_exists('class', $inputMetadata) && null === $inputMetadata['class']) {
            return;
        }

        $validationGroups = $resourceMetadata->getOperationAttribute($attributes, 'validation_groups', null, true);
        $this->validator->validate($event->getControllerResult(), ['groups' => $validationGroups]);
    }
437194
public function onKernelView(GetResponseForControllerResultEvent $event)
    {
        $request    = $event->getRequest();
        $response   = $event->getResponse();
        $parameters = $event->getControllerResult();

        if (!$response instanceof Response && $template = $request->attributes->get('_template')) {
            return $event->setControllerResult($this->templating->render($template, $parameters));
        }
    }
1228225
public function onRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();

        // skip if route does not match
        if (false === $this->isEnabled($request)) {
            return;
        }

        $content = $request->getContent();

        if ('' === $content) {
            // no content provided
            return;
        }

        if (!count($request->request->all())
            && in_array($request->getMethod(), array('POST', 'PUT', 'PATCH', 'DELETE'))
        ) {
            $contentType = $request->headers->get('Content-Type');

            $format = null === $contentType
                ? $request->getRequestFormat()
                : $request->getFormat($contentType);

            if ($format && !$this->encoderProvider->supports($format)) {
                if ($this->throwExceptionOnUnsupportedContentType) {
                    throw new UnsupportedMediaTypeHttpException("Request body format '$format' not supported");
                }

                return;
            }

            $decoder = $this->encoderProvider->getEncoder($format);

            if (!empty($content)) {
                $data = $decoder->decode($content, $format);

                if (is_array($data)) {
                    $request->request = new ParameterBag($data);
                } else {
                    throw new BadRequestHttpException('Invalid '.$format.' message received');
                }
            }
        }
    }
1053781
public function onKernelRequest(GetResponseEvent $event)
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        if (!$this->csrfTokenManager instanceof CsrfTokenManagerInterface) {
            return;
        }

        $request = $event->getRequest();

        if ($request->isMethodCacheable()) {
            return;
        }

        if ($request->attributes->get('_route') !== $this->routeName) {
            return;
        }

        $session = $event->getRequest()->getSession();
        if (!$session->isStarted()) {
            return;
        }

        if ($request->headers->get('X-CSRF-Token') !== null) {
            return;
        }

        $csrfToken = $this->csrfTokenManager->getToken($this->csrfTokenId)->getValue();

        $request->headers->set('X-CSRF-Token', $csrfToken);
    }
994988
public function onResponseLate(FilterResponseEvent $event)
    {
        if (!$this->eventRequestMatches($event)) {
            return;
        }
        $this->responseTransformer->transformLate($event->getRequest(), $event->getResponse());
    }
963458
public function handle(GetResponseEvent $event)
    {
        if (!$this->registered && null !== $this->dispatcher && $event->isMasterRequest()) {
            $this->dispatcher->addListener(KernelEvents::RESPONSE, array($this, 'onKernelResponse'));
            $this->registered = true;
        }

        $request = $event->getRequest();
        $session = $request->getSession();
        /*$sessionId = $session->getId();
        if(empty($sessionId))
        {
            $session = null;
        }*/

        if (null === $session || null === $token = $session->get('_security_'.$this->contextKey)) {
            $this->context->setToken(null);

            return;
        }

        $token = unserialize($token);

        if (null !== $this->logger) {
            $this->logger->debug('Read SecurityContext from the session');
        }

        if ($token instanceof TokenInterface) {
            $token = $this->refreshUser($token);
        } elseif (null !== $token) {
            if (null !== $this->logger) {
                $this->logger->warning(sprintf('Session includes a "%s" where a security token is expected', is_object($token) ? get_class($token) : gettype($token)));
            }

            $token = null;
        }

        $this->context->setToken($token);
    }
568493
public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        
        if ($exception instanceof NotFoundHttpException) {
            $response = $event->getResponse();

            $values = array(
                'status_code' => 404,
                'message' => $exception->getMessage(),
            );
            
            $event->setResponse($this->templating->renderResponse('AlphaLemonThemeEngineBundle:Error:error.html.twig', $values, $response));
        }
    }
1152477
private function handleInternalError(GetResponseForExceptionEvent $event)
    {
        $content = $this->templating->render(
            'SonataPageBundle::internal_error.html.twig',
            [
                'exception' => $event->getException(),
            ]
        );

        $event->setResponse(new Response($content, 500));
    }
918997
public function onKernelView(GetResponseForControllerResultEvent $event): void
    {
        $request = $event->getRequest();
        if (
            $request->isMethodSafe(false)
            || $request->isMethod('DELETE')
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
            || !$attributes['receive']
        ) {
            return;
        }

        $data = $event->getControllerResult();
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);

        $validationGroups = $resourceMetadata->getOperationAttribute($attributes, 'validation_groups');

        if (!$validationGroups) {
            // Fallback to the resource
            $validationGroups = $resourceMetadata->getAttributes()['validation_groups'] ?? null;
        }

        if (
            $this->container &&
            \is_string($validationGroups) &&
            $this->container->has($validationGroups) &&
            ($service = $this->container->get($validationGroups)) &&
            \is_callable($service)
        ) {
            $validationGroups = $service($data);
        } elseif (\is_callable($validationGroups)) {
            $validationGroups = $validationGroups($data);
        }

        $violations = $this->validator->validate($data, null, (array) $validationGroups);
        if (0 !== \count($violations)) {
            throw new ValidationException($violations);
        }
    }
995706
public function onKernelResponse(FilterResponseEvent $event)
    {
        $response = $event->getResponse();
        $request = $event->getRequest();

        $ifNoneMatch = $request->headers->get('if-none-match');
        $etag = $response->headers->get('ETag', 'empty');

        if ($ifNoneMatch === $etag) {
            $response->setStatusCode(304);
            $response->setContent(null);
        }
    }
661969
public function onKernelException(GetResponseForExceptionEvent $event)
    {
        if (!$this->auto) {
            return;
        }

        $exception = $event->getException();

        $report = Report::fromPHPThrowable(
            $this->client->getConfig(),
            $exception
        );
        $report->setUnhandled(true);
        $report->setSeverityReason([
            'type' => 'unhandledExceptionMiddleware',
            'attributes' => [
                'framework' => 'Symfony',
            ],
        ]);

        $this->client->notify($report);
    }
1350675
public function onKernelRequest(GetResponseEvent $event): void
    {
        $request = $event->getRequest();
        $method = $request->getMethod();

        if (
            'DELETE' === $method
            || $request->isMethodSafe(false)
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
            || !$attributes['receive']
            || (
                    '' === ($requestContent = $request->getContent())
                    && ('POST' === $method || 'PUT' === $method)
               )
        ) {
            return;
        }

        $context = $this->serializerContextBuilder->createFromRequest($request, false, $attributes);
        if (isset($context['input']) && \array_key_exists('class', $context['input']) && null === $context['input']['class']) {
            return;
        }

        // BC check to be removed in 3.0
        if (null !== $this->formatsProvider) {
            $this->formats = $this->formatsProvider->getFormatsFromAttributes($attributes);
        }
        $this->formatMatcher = new FormatMatcher($this->formats);
        $format = $this->getFormat($request);

        $data = $request->attributes->get('data');
        if (null !== $data) {
            $context[AbstractNormalizer::OBJECT_TO_POPULATE] = $data;
        }

        $request->attributes->set(
            'data',
            $this->serializer->deserialize(
                $requestContent, $context['resource_class'], $format, $context
            )
        );
    }
1304844
public function onKernelException(GetResponseForExceptionEvent $event) {

        $ex = $event->getException();

        // Handle the exception.
        if (true === ($ex instanceof BadUserRoleException)) {
            $this->handleBadUserRoleException($event, $ex);
        }
        if (true === ($ex instanceof RedirectResponseException)) {
            $this->handleRedirectResponseException($event, $ex);
        }

        return $event;
    }
1330278
public function onKernelView(GetResponseForControllerResultEvent $event)
    {
        $request = $event->getRequest();

        if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
            return false;
        }

        $configuration = $request->attributes->get('_template');

        $view = $event->getControllerResult();
        if (!$view instanceof View) {
            if (!$configuration instanceof ViewAnnotation && !$this->forceView) {
                return;
            }

            $view = new View($view);
        }

        if ($configuration instanceof ViewAnnotation) {
            if ($configuration->getTemplateVar()) {
                $view->setTemplateVar($configuration->getTemplateVar());
            }
            if (null !== $configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
                $view->setStatusCode($configuration->getStatusCode());
            }

            $context = $view->getContext();
            if ($configuration->getSerializerGroups()) {
                if (null === $context->getGroups()) {
                    $context->setGroups($configuration->getSerializerGroups());
                } else {
                    $context->setGroups(array_merge($context->getGroups(), $configuration->getSerializerGroups()));
                }
            }
            if ($configuration->getSerializerEnableMaxDepthChecks()) {
                $context->setMaxDepth(0, false);
            }
            if (true === $configuration->getSerializerEnableMaxDepthChecks()) {
                $context->enableMaxDepth();
            } elseif (false === $configuration->getSerializerEnableMaxDepthChecks()) {
                $context->disableMaxDepth();
            }

            list($controller, $action) = $configuration->getOwner();
            $vars = $this->getDefaultVars($configuration, $controller, $action);
        } else {
            $vars = null;
        }

        if (null === $view->getFormat()) {
            $view->setFormat($request->getRequestFormat());
        }

        if ($this->viewHandler->isFormatTemplating($view->getFormat())
            && !$view->getRoute()
            && !$view->getLocation()
        ) {
            if (null !== $vars && 0 !== count($vars)) {
                $parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
                foreach ($vars as $var) {
                    if (!array_key_exists($var, $parameters)) {
                        $parameters[$var] = $request->attributes->get($var);
                    }
                }
                $view->setData($parameters);
            }

            if ($configuration && ($template = $configuration->getTemplate()) && !$view->getTemplate()) {
                if ($template instanceof TemplateReferenceInterface) {
                    $template->set('format', null);
                }

                $view->setTemplate($template);
            }
        }

        $response = $this->viewHandler->handle($view, $request);

        $event->setResponse($response);
    }
731698
public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        $routeName = $request->attributes->get('_route');
        if (!$route = $this->app['routes']->get($routeName)) {
            return;
        }

        foreach ((array) $route->getOption('_before_middlewares') as $callback) {
            $ret = call_user_func($this->app['callback_resolver']->resolveCallback($callback), $request, $this->app);
            if ($ret instanceof Response) {
                $event->setResponse($ret);

                return;
            } elseif (null !== $ret) {
                throw new \RuntimeException(sprintf('A before middleware for route "%s" returned an invalid response value. Must return null or an instance of Response.', $routeName));
            }
        }
    }
594229
public function onKernelView(GetResponseForControllerResultEvent $event): void
    {
        $presentation = $event->getControllerResult();

        if (!$presentation instanceof PresentationInterface) {
            // Not the presentation instance
            return;
        }

        $data = null;
        $headers = [];

        if ($presentation->getResource()) {
            $acceptableMediaTypes = $event->getRequest()->getAcceptableContentTypes();
            $serializer = $this->serializerResolver->resolveByMediaTypes(
                get_class($presentation->getResource()),
                $acceptableMediaTypes,
                $acceptableMediaType
            );

            $serializationContext = $this->serializationContextCollector->collect();
            $data = $serializer->serialize($presentation->getResource(), $serializationContext);
            $headers = [
                'Content-Type' => $acceptableMediaType,
            ];
        }

        $response = new Response($data, $presentation->getStatusCode());
        $response->headers->add($headers);

        $event->setResponse($response);
    }