@param GetResponseForControllerResultEvent $event

@throws \Exception
439465
public function onKernelView(GetResponseForControllerResultEvent $event): void
    {
        $result = $event->getControllerResult();

        if (!$result instanceof FormRequest) {
            return;
        }

        $response = $result->getResponse();

        if ($response instanceof Response) {
            $event->setResponse($response);
        } else {
            $event->setControllerResult($response);
        }
    }
936403
public function onKernelView(GetResponseForControllerResultEvent $event) :? Response
    {
        $request = $event->getRequest();
        $result = $event->getControllerResult();

        // If the event already has a response, do not override it.
        if ($event->hasResponse()) {
            return $event->getResponse();
        }

        // If the request format is not supported, nothing more can be done
        // without the serializer throwing an exception.
        if (!$this->encoder->supportsEncoding($request->getRequestFormat())) {
            return null;
        }

        if (is_iterable($result)) {
            $data = $result;

            // Convert objects to arrays.
            if ($result instanceof \Traversable) {
                $data = iterator_to_array($result);
            }

            // Determine if array is a collection or just a standard array.
            $bad = array_filter($data, function ($item) use ($request) {
                if (!is_object($item)) {
                    return true;
                }

                if (!$this->normalizer->supportsNormalization($item, $request->getRequestFormat())) {
                    return true;
                }

                return false;
            });

            // If array is a collection and everything can be normalized,
            // normalize each item before serialization.
            if (!count($bad)) {
                $result = array_map(function ($item) use ($request) {
                    return $this->normalizer->normalize($item, $request->getRequestFormat(), [
                        'groups' => $this->groupResolver->resolve($request, $item),
                        'enable_max_depth' => true,
                    ]);
                }, $data);
            }
        }

        // If the normalizer cannot normalize the result, then there is nothing
        // more than can be done without the serializer throwing an exception.
        if (is_object($result) && !$this->normalizer->supportsNormalization($result, $request->getRequestFormat())) {
            return null;
        }

        $status = Response::HTTP_OK;
        switch ($request->getMethod()) {
            case Request::METHOD_POST:
                $status = Response::HTTP_CREATED;
                break;
            case Request::METHOD_DELETE:
                $status = Response::HTTP_NO_CONTENT;
                break;
        }

        $groups = null;
        if (is_object($result)) {
            $groups = $this->groupResolver->resolve($request, $result);
        }

        $serializeEvent = new SerializeEvent(
            $result,
            $request->getRequestFormat(),
            [
                'groups' => $groups,
                'enable_max_depth' => true,
            ],
            $request
        );

        $this->eventDispatcher->dispatch(SerializeEvent::NAME, $serializeEvent);

        $response = new Response(
            $this->serializer->serialize(
                $serializeEvent->getData(),
                $serializeEvent->getFormat(),
                $serializeEvent->getContext()
            ),
            $status
        );
        $event->setResponse($response);

        return $response;
    }
116776
public function onKernelView(GetResponseForControllerResultEvent $event): void
    {
        $request = $event->getRequest();
        if ($request->isMethodSafe(false)) {
            return;
        }

        $resourceClass = $request->attributes->get('_api_resource_class');
        if (null === $resourceClass) {
            return;
        }

        $controllerResult = $event->getControllerResult();
        if (null === $objectManager = $this->getManager($resourceClass, $controllerResult)) {
            return;
        }

        switch ($request->getMethod()) {
            case 'POST':
                $objectManager->persist($controllerResult);
                break;
            case 'DELETE':
                $objectManager->remove($controllerResult);
                $event->setControllerResult(null);
                break;
        }

        $objectManager->flush();
    }
243156
public function onKernelException(GetResponseForExceptionEvent $event)
    {
        if (($exception = $event->getException()) instanceof SyntaxErrorException) {
            // Set status code and content
            $response = new Response();
            $response
                ->setStatusCode(Response::HTTP_BAD_REQUEST)
                ->setContent(
                    $this->getSerializedContent(
                        [
                            'message' => sprintf('syntax error in rql: %s', $exception->getMessage())
                        ]
                    )
                );
            $event->setResponse($response);
        }
    }
841942
public function onCoreController(FilterControllerEvent $event)
    {
        if (!is_array($controller = $event->getController())) {
            return;
        }

        $class  = new \ReflectionClass($controller[0]);
        $method = $class->getMethod($controller[1]);

        if ($classAnnotations = $this->reader->getClassAnnotations($class)) {
            foreach ($classAnnotations as $classAnnotation) {
                if ($classAnnotation instanceof Firewall) {
                    if (is_array($classAnnotation->actions) && in_array($method->name, $classAnnotation->actions)) {
                        $this->loadFirewall($classAnnotation);
                    } elseif ($classAnnotation->actions === null) {
                        $this->loadFirewall($classAnnotation);
                    }
                }
            }
        }

        if ($methodAnnotation = $this->reader->getMethodAnnotation($method, 'M6Web\Bundle\FirewallBundle\Annotation\Firewall')) {
            $this->loadFirewall($methodAnnotation);
        }
    }
243185
public function onKernelException(GetResponseForExceptionEvent $event): void
    {
        $request = $event->getRequest();

        if ($this->kernelDebug && $this->debugParameter && $request->query->has($this->debugParameter)) {
            return;
        }

        $exception = $event->getException();
        $errorPresentation = $this->errorPresentationFactory->create($exception);

        if (!$errorPresentation) {
            return;
        }

        if (!$errorPresentation->getResource()) {
            // The presentation not have resource. Send response with only status code.
            $event->setResponse(new Response('', $errorPresentation->getStatusCode()));

            return;
        }

        $acceptMediaType = null;

        try {
            $serializer = $this->serializerResolver->resolveByMediaTypes(
                get_class($errorPresentation->getResource()),
                $request->getAcceptableContentTypes(),
                $acceptMediaType
            );
        } catch (ResourceSerializerNotFoundException $e) {
            // Cannot resolve the serializer for accept media types.

            return;
        }

        $context = $this->serializationContextCollector->collect();
        $serializedData = $serializer->serialize($errorPresentation->getResource(), $context);

        $response = new Response($serializedData, $errorPresentation->getStatusCode(), [
            'Content-Type' => $acceptMediaType,
        ]);

        $event->setResponse($response);
    }
252208
public function onKernelController(FilterControllerEvent $event)
    {
        $controller = $event->getController();
        $request = $event->getRequest();
        $configurations = [];

        if ($configuration = $request->attributes->get('_converters')) {
            foreach (\is_array($configuration) ? $configuration : [$configuration] as $configuration) {
                $configurations[$configuration->getName()] = $configuration;
            }
        }

        if (\is_array($controller)) {
            $r = new \ReflectionMethod($controller[0], $controller[1]);
        } elseif (\is_object($controller) && \is_callable([$controller, '__invoke'])) {
            $r = new \ReflectionMethod($controller, '__invoke');
        } else {
            $r = new \ReflectionFunction($controller);
        }

        // automatically apply conversion for non-configured objects
        if ($this->autoConvert) {
            $configurations = $this->autoConfigure($r, $request, $configurations);
        }

        $this->manager->apply($request, $configurations);
    }
821030
public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $request = $event->getRequest();
        $exception = $event->getException();
        if (!$exception instanceof HttpExceptionInterface || Zone::isBackend($request)) {
            return;
        }
        if ($exception->getStatusCode() !== Response::HTTP_NOT_FOUND) {
            return;
        }
        // If no zone is set, assume front-end as SELECT queries can leak unpublished records
        if (Zone::get($request) === null) {
            Zone::set($request, Zone::FRONTEND);
        }

        foreach ($this->notFoundPage as $item) {
            try {
                $this->renderNotFound($event, $item);

                return;
            } catch (LoaderError $e) {
                // Template not found, fall though to see if we can render a
                // record, failing that let the exception handler take over
            }

            $content = $this->storage->getContent($item, ['returnsingle' => true]);
            if ($content instanceof Content && !empty($content->id)) {
                $template = $this->templateChooser->record($content);
                $this->renderNotFound($event, $template, $content->getTemplateContext());

                return;
            }
        }

        $msg = sprintf(
            'No page could be shown, because the "notfound" setting "%s" in config.yml or theme.yml is not valid.',
            implode(', ', $this->notFoundPage)
        );
        $event->setException(new NotFoundHttpException($msg, $e));
    }
1072439
public function handleGetReferer(GetReferrerEvent $event)
    {
        /** @var System $systemAdapter */
        $systemAdapter = $this->framework->getAdapter(System::class);

        $event->setReferrerUrl($systemAdapter->getReferer($event->isEncodeAmpersands(), $event->getTableName()));
    }
997148
public function onKernelExceptionView(GetResponseForExceptionEvent $event)
    {
        if (!$event->getRequest()->attributes->get('is_rest_request')) {
            return;
        }

        $event->setResponse(
            $this->viewDispatcher->dispatch(
                $event->getRequest(),
                $event->getException()
            )
        );
        $event->stopPropagation();
    }
494041
public function onView(GetResponseForControllerResultEvent $event): void
    {
        $view = $event->getControllerResult();
        if (!$view instanceof ViewInterface) {
            return;
        }

        $response = $view->getResponse();
        if (!$response instanceof Response) {
            return;
        }

        $renderedView = '';

        try {
            $renderedView = $this->viewRenderer->renderView($view);
        } catch (Throwable $t) {
            $this->errorHandler->handleError($t);
        }

        $response->setContent($renderedView);

        $event->setResponse($response);
    }
439714
public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();

        if ($exception instanceof HttpException) {
            return;
        }

        $this->client->notifyOnException($exception);
        error_log($exception->getMessage().' in: '.$exception->getFile().':'.$exception->getLine());
    }
222984
public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        $response  = null;

        switch (true) {
            case ($exception instanceof NotFoundHttpException):
                $response = $this->createNotFoundResponse($event->getRequest(), $exception);
                break;
            case ($exception instanceof AccessDeniedHttpException):
            case ($exception instanceof UnauthorizedHttpException):
            case ($exception instanceof BadRequestHttpException):
            case ($exception instanceof ServiceUnavailableHttpException):
            case ($exception instanceof NotAcceptableHttpException):
            case ($exception instanceof HttpException):
                /** @var HttpException $exception */
                $response = $this->createHttpExceptionResponse($exception);
                break;
            case ($exception instanceof AuthenticationCredentialsNotFoundException):
                $response = $this->createUnauthenticatedResponse($exception);
                break;
            default:
        }

        if (null === $response) {
            $response = $this->createInternalServerError($exception);
        }

        $event->setResponse($response);
    }
900179
public function onKernelException(GetResponseForExceptionEvent $event)
    {
        if ($event->getException() instanceof DisplayBlockException) {
            $event->getRequest()->setRequestFormat('fragment.'. $event->getRequest()->getRequestFormat());
            $event->setException($event->getException()->getPrevious());
        } elseif ($event->getException() instanceof HttpExceptionInterface && '404' == $event->getException()->getStatusCode()) {
            $this->setCurrentSiteInfo(trim($this->request->getHost(), '/'), trim($this->request->getPathInfo(), '/'));

            if ($html = $this->getCustom404Html()) {
                $event->setResponse(new Response($html, 404));
            }
        }
    }
178989
public function onKernelController(ControllerEvent $event): void
    {
        if (!$this->check($event)) {
            return;
        }

        if ($annotation = $this->getAnnotation($event)) {
            $annotation->setData($this->getAttributes($event));
            /* @var $annotation Cache */
            $response = $this->getCache($annotation->getKey($event->getRequest()->get(self::ROUTE)));
            if (null !== $response) {
                $event->setController(function () use (
                    $response
                ) {
                    return $response;
                });
            }
        }
    }
611503
public function onError(GetResponseForExceptionEvent $event)
    {
        if ($event->getRequest()->isXmlHttpRequest()) {
            $exception = $event->getException();
            if ($exception instanceof InvalidDataException) {
                // return correct status code for invalid data sent by the user
                $response = new JsonResponse($exception->getErrors(), 422);
            } else {
                $response = new JsonResponse([
                    'message' => $exception->getMessage(),
                    'trace' => $exception->getTrace(),
                    //if <200, not http error code
                ], $exception->getCode() < 200 ? 500 : $exception->getCode());
            }

            $event->setResponse($response);
        }
    }
617866
public function onKernelException(GetResponseForExceptionEvent $event)
    {
        static $handling;

        if (true === $handling) {
            return;
        }

        $handling = true;

        $exception = $event->getException();
        $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));

        $request = $event->getRequest();

        // Only for debug
        if ($this->debug) {
            return;
        }

        // Not for xml http requests
        if ($request->isXmlHttpRequest()) {
            return;
        }

        // Only for phlexible tree nodes
        if (!$request->attributes->has('siterootUrl')) {
            $siteroot = $this->siterootRequestMatcher->matchRequest($request);
            if (!$siteroot) {
                return;
            }
            $request->attributes->set('siterootUrl', $siteroot->getDefaultUrl());
        }

        $exception = $event->getException();
        if ($exception instanceof HttpException) {
            $code = $exception->getStatusCode();
        } else {
            $code = $exception->getCode();
        }
        if (!in_array($code, array(401, 403, 404, 500))) {
            $code = 500;
        }

        $template = null;
        if ($this->twig->getLoader()->exists("::error/error-$code.html.twig")) {
            $template = "::error/error-$code.html.twig";
        } elseif ($this->twig->getLoader()->exists('::error/error.html.twig')) {
            $template = '::error/error.html.twig';
        }

        $tid = $siteroot->getSpecialTid($request->getLocale(), "error_$code");

        if (!$tid) {
            if (!$template) {
                return;
            }

            $content = $this->twig->render($template);
            $response = new Response($content, $code);

            $event->setResponse($response);

            return;
        }

        $treeNode = $this->treeManager->findByTreeId($tid)->get($tid);

        $request = $this->duplicateRequest($exception, $request, $tid, $treeNode, $template);

        try {
            $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false);
        } catch (\Exception $e) {
            $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), false);

            $wrapper = $e;

            while ($prev = $wrapper->getPrevious()) {
                if ($exception === $wrapper = $prev) {
                    throw $e;
                }
            }

            $prev = new \ReflectionProperty('Exception', 'previous');
            $prev->setAccessible(true);
            $prev->setValue($wrapper, $exception);

            throw $e;
        }

        $event->setResponse($response);

        return;

        $configuration = $this->configurator->configure($request);
        if ($configuration->hasResponse()) {
            $event->setResponse($configuration->getResponse());

            return;
        }

        $data = $configuration->getVariables();
        $content = $this->twig->render($data['template'], $data);
        $response = new Response($content, $code);

        $event->setResponse($response);
    }