PHP-DI / Silex-Bridge

PHP-DI integration in Silex
http://php-di.org/doc/frameworks/silex.html
MIT License
24 stars 8 forks source link

Short-circuiting middleware not working #19

Closed lord-disease closed 8 years ago

lord-disease commented 8 years ago

As described in the documentation, Silex allows short-circuiting the "before" middleware pipeline when a middleware returns an instance of \Symfony\Component\HttpFoundation\Response. When using \DI\Bridge\Silex\Application instead of \Silex\Application, this mechanism no longer works. In the provided example, instead of returning the "expected result" response, the exception is thrown.

Versions:

"silex/silex": "v1.3.5", "php-di/silex-bridge": "^1.5"

Code example:

require_once './vendor/autoload.php';

class TestController implements \Silex\ControllerProviderInterface
{

    public function connect(\Silex\Application $app)
    {
        $controller = $app['controllers_factory'];
        $controller->get('/')
            ->before(function () {
                return new \Symfony\Component\HttpFoundation\Response('expected result');
            })
            ->before(function() {
                throw new Exception('not working');
            });
        return $controller;
    }
}

try {
    $app = new \DI\Bridge\Silex\Application();
    $app->error(function (Exception $e, $code) {
        $msg = $e->getMessage() . PHP_EOL;
        $msg .= $code . PHP_EOL;
        $msg .= $e->getTraceAsString();
        return new \Symfony\Component\HttpFoundation\Response($msg);
    });
    $app->mount('/', new TestController());
    $app->run();
} catch (Exception $exc) {
    echo $exc->getMessage() . PHP_EOL;
    echo $exc->getTraceAsString();
}
jdreesen commented 8 years ago

Thx for the report. There was missing a return. Should be fixed by #20.

jdreesen commented 8 years ago

v1.5.1 has just been released with the fix.

mnapoli commented 8 years ago

Awesome!