本文整理汇总了PHP中Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent::getResponse方法的典型用法代码示例。如果您正苦于以下问题:PHP GetResponseForExceptionEvent::getResponse方法的具体用法?PHP GetResponseForExceptionEvent::getResponse怎么用?PHP GetResponseForExceptionEvent::getResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
的用法示例。
在下文中一共展示了GetResponseForExceptionEvent::getResponse方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onException
/**
* @param GetResponseForExceptionEvent $event
*/
public function onException(GetResponseForExceptionEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$this->eventDispatcher->dispatch(Events::REQUEST_ENDS, new RequestEnded($event->getRequest(), $event->getResponse(), $event->getException()));
}
示例2: assertResponse
protected function assertResponse(GetResponseForExceptionEvent $event, $content, $code)
{
$response = $event->getResponse();
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
$this->assertSame($content, $response->getContent());
$this->assertSame($code, $response->getStatusCode());
}
示例3: onKernelException
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
if ($event->getResponse() !== null) {
return;
}
$exception = $event->getException();
if (!$exception instanceof OAuthException) {
return;
}
$response = new JsonResponse(["code" => $exception->getCode(), "error" => $exception->getMessage()]);
$event->setResponse($response);
}
示例4: onKernelException
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$statusCode = $this->getHttpStatusCode($exception->getCode());
if ($this->application->isDebugMode()) {
$this->response = $this->getDebugTraceResponse($exception, $statusCode);
} else {
$this->response = $this->getErrorPageResponse($exception, $statusCode);
}
$event->setResponse($this->response);
$filterEvent = new FilterResponseEvent($event->getKernel(), $event->getRequest(), $event->getRequestType(), $event->getResponse());
$event->getDispatcher()->dispatch(KernelEvents::RESPONSE, $filterEvent);
}
示例5: onKernelException
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
if (false == $event->getException() instanceof InteractiveRequestInterface) {
return;
}
$interactiveRequest = $event->getException();
if ($interactiveRequest instanceof SymfonyResponseInteractiveRequest) {
$event->setResponse($interactiveRequest->getResponse());
} elseif ($interactiveRequest instanceof ResponseInteractiveRequest) {
$event->setResponse(new Response($interactiveRequest->getContent()));
} elseif ($interactiveRequest instanceof RedirectUrlInteractiveRequest) {
$event->setResponse(new RedirectResponse($interactiveRequest->getUrl()));
}
if ($event->getResponse()) {
if (false == $event->getResponse()->headers->has('X-Status-Code')) {
$event->getResponse()->headers->set('X-Status-Code', $event->getResponse()->getStatusCode());
}
return;
}
$ro = new \ReflectionObject($interactiveRequest);
$event->setException(new LogicException(sprintf('Cannot convert interactive request %s to symfony response.', $ro->getShortName()), null, $interactiveRequest));
}
示例6: onKernelException
/**
* @param GetResponseForExceptionEvent $event
**/
public function onKernelException(GetResponseForExceptionEvent $event)
{
if ('html' === $this->responseFormat) {
return;
}
$exception = $event->getException();
$response = $event->getResponse();
$response->setContent($this->serializer->serialize($exception->getMessage(), array(), $this->responseFormat));
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
示例7: onKernelException
/**
* Handles a kernel exception and returns a relevant response.
* Aims to deliver content to the user that explains the exception, rather than falling
* back on symfony's exception handler which displays a less verbose error message.
*
* @param GetResponseForExceptionEvent $event The exception event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
// do nothing if request is no preview request
if (!$event->getRequest()->get('preview', false)) {
return;
}
$ex = $event->getException();
$code = 500;
$previousContent = $event->getResponse();
$content = $previousContent !== null ? $previousContent->getContent() : '';
$statusTexts = Response::$statusTexts;
$statusText = isset($statusTexts[$code]) ? $statusTexts[$code] : '';
$exception = FlattenException::create($ex, $code);
// log exception
$this->logger->error(sprintf('%s (%s %s)', $ex->getMessage(), $code, $statusText));
$responseContent = $this->templateEngine->render($this->findTemplate(), ['status_code' => $code, 'status_text' => $statusText, 'exception' => $exception, 'logger' => null, 'currentContent' => $content]);
$event->setResponse(new Response($responseContent));
}
示例8: logrefInResponseAndLogMatch
/**
* @test
*/
public function logrefInResponseAndLogMatch()
{
foreach ([400, 500] as $code) {
$logref = null;
$logger = $this->getMockForAbstractClass('Psr\\Log\\LoggerInterface');
$logger->expects($this->once())->method($this->anything())->with($this->callback(function ($message) use(&$logref) {
$matches = [];
if (preg_match('/logref ([a-z0-9]*)/', $message, $matches)) {
$logref = $matches[1];
return true;
}
return false;
}));
/** @var LoggerInterface $logger */
$this->exceptionListener->setLogger($logger);
$this->codeProperty->setValue($this->exception, $code);
$this->exceptionListener->onKernelException($this->event);
$response = $this->event->getResponse();
$this->assertEquals($logref, json_decode($response->getContent())->logref);
}
}
示例9: getValidationFailedExceptionResponse
/**
* Get correct response for validation failed exception
*
* @param GetResponseForExceptionEvent $event event
* @param EventDispatcherInterface $eventDispatcher eventDispatcher
* @param ValidationFailedException $exception exception
*
* @return Response|null
*/
private function getValidationFailedExceptionResponse(GetResponseForExceptionEvent $event, EventDispatcherInterface $eventDispatcher, ValidationFailedException $exception)
{
$event->stopPropagation();
$request = $event->getRequest();
$transaction = $request->get('transaction');
if (!$transaction) {
return $event->getResponse();
}
$data = $request->get($request->get(Alias::DATA));
$violations = $exception->getConstraintViolationList();
$request->attributes->set('violations', $violations);
$view = View::create($data);
$responseEvent = new GetResponseForControllerResultEvent($event->getKernel(), $request, $request->getMethod(), $view);
if ($view->getData() instanceof ArrayCollection) {
$responseEvent->getRequest()->attributes->set(EmbeddedManager::KEY_EMBED, EmbeddedManager::GROUP_VIOLATION_COLLECTION);
} else {
$responseEvent->getRequest()->attributes->set(EmbeddedManager::KEY_EMBED, EmbeddedManager::GROUP_VIOLATION_ENTITY);
}
$eventDispatcher->dispatch('kernel.view', $responseEvent);
return $responseEvent->getResponse();
}
示例10: getValidationFailedExceptionResponse
/**
* Get correct response for validation failed exception
*
* @param GetResponseForExceptionEvent $event event
* @param EventDispatcherInterface $eventDispatcher eventDispatcher
* @param ValidationFailedException $exception exception
*
* @return Response|null
*/
private function getValidationFailedExceptionResponse(GetResponseForExceptionEvent $event, EventDispatcherInterface $eventDispatcher, ValidationFailedException $exception)
{
$event->stopPropagation();
$request = $event->getRequest();
$transaction = $request->get('transaction');
if (!$transaction) {
return $event->getResponse();
}
$data = $request->get($request->get(Alias::DATA));
$violations = $exception->getConstraintViolationList();
$request->attributes->set('violations', $violations);
$view = View::create($data);
$responseEvent = new GetResponseForControllerResultEvent($event->getKernel(), $request, $request->getMethod(), $view);
$eventDispatcher->dispatch('kernel.view', $responseEvent);
$responseData = $view->getData();
if ($responseData instanceof EmbeddedInterface) {
$responseData->setShowAssociations(true);
}
if ($responseData instanceof CollectionResponse) {
$responseData->setInheritedShowAssociations(false);
}
return $responseEvent->getResponse();
}
示例11: onKernelException
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$statusCode = $exception->getCode();
if ($this->application->isDebugMode()) {
$this->response = (new \Symfony\Component\Debug\ExceptionHandler())->createResponse($exception);
}
if (!$this->application->isDebugMode()) {
switch ($statusCode) {
case 404:
case 500:
$parameterKey = "error.{$statusCode}";
break;
default:
$parameterKey = 'error.default';
}
$parameter = $this->application->getContainer()->getParameter($parameterKey);
$view = $this->getErrorTemplate($parameter);
$this->response->setStatusCode($statusCode)->setContent($this->renderer->partial($view, ['error' => $exception]));
}
$event->setResponse($this->response);
$filterEvent = new FilterResponseEvent($event->getKernel(), $event->getRequest(), $event->getRequestType(), $event->getResponse());
$event->getDispatcher()->dispatch(KernelEvents::RESPONSE, $filterEvent);
}
示例12: setNewResponse
/**
* Sets a new response to $event
* @param GetResponseForExceptionEvent $event The event
* @param int $statusCode The status code of the new response
* @param string $message Optional, the message of the new response
*/
private function setNewResponse(GetResponseForExceptionEvent $event, $statusCode, $message = '')
{
$event->setResponse(new Response());
$event->getResponse()->setStatusCode($statusCode, $message);
}
示例13: onException
public function onException(GetResponseForExceptionEvent $event)
{
$key = $this->getRequestKey($event->getRequest());
$this->errors++;
$this->calls[$key] = array_merge($this->calls[$key], array('response' => $event->getResponse(), "error" => $event->getException(), "stop" => microtime(true)));
$this->finishEvent($key);
}