Commit c89133e8 authored by Fabien Potencier's avatar Fabien Potencier

simplified some code

parent d89cbcd9
......@@ -345,7 +345,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/
public function error($callback, $priority = 0)
{
$this['dispatcher']->addListener(SilexEvents::ERROR, function (GetResponseForErrorEvent $event) use ($callback) {
$this['dispatcher']->addListener(SilexEvents::ERROR, function (GetResponseForExceptionEvent $event) use ($callback) {
$exception = $event->getException();
if (is_array($callback)) {
......@@ -370,7 +370,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$result = call_user_func($callback, $exception, $code);
if (null !== $result) {
$event->setStringResponse($result);
$event->setResponse($result instanceof Response ? $result : new Response((string) $result));
}
}, $priority);
}
......@@ -567,8 +567,8 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$response = $event->getControllerResult();
$converter = new StringResponseConverter();
$event->setResponse($converter->convert($response));
$event->setResponse($response instanceof Response ? $response : new Response((string) $response));
}
/**
......@@ -621,7 +621,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
}
}
$errorEvent = new GetResponseForErrorEvent($this, $event->getRequest(), $event->getRequestType(), $event->getException());
$errorEvent = new GetResponseForExceptionEvent($this, $event->getRequest(), $event->getRequestType(), $event->getException());
$this['dispatcher']->dispatch(SilexEvents::ERROR, $errorEvent);
if ($errorEvent->hasResponse()) {
......
......@@ -13,6 +13,7 @@ namespace Silex;
use Symfony\Component\HttpKernel\Debug\ExceptionHandler as DebugExceptionHandler;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
/**
* Defaults exception handler.
......@@ -21,7 +22,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
*/
class ExceptionHandler implements EventSubscriberInterface
{
public function onSilexError(GetResponseForErrorEvent $event)
public function onSilexError(GetResponseForExceptionEvent $event)
{
$app = $event->getKernel();
$handler = new DebugExceptionHandler($app['debug']);
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
/**
* GetResponseForExceptionEvent with additional setStringResponse method
*
* setStringResponse will convert strings to response objects.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class GetResponseForErrorEvent extends GetResponseForExceptionEvent
{
public function setStringResponse($response)
{
$converter = new StringResponseConverter();
$this->setResponse($converter->convert($response));
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex;
use Symfony\Component\HttpFoundation\Response;
/**
* Converts string responses to Response objects.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class StringResponseConverter
{
/**
* Does the conversion
*
* @param string $response The response string
*
* @return Response A Response object
*/
public function convert($response)
{
if (!$response instanceof Response) {
return new Response((string) $response);
}
return $response;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment