Commit e8cd473d authored by Igor Wiedler's avatar Igor Wiedler

populate the request attributes before onSilexBefore is fired

if we don't do this, it's impossible to use route parameters in before
handlers.
parent 42926bf5
...@@ -28,6 +28,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; ...@@ -28,6 +28,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\Exception\Exception as MatcherException;
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Matcher\Exception\NotFoundException; use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
use Symfony\Component\ClassLoader\UniversalClassLoader; use Symfony\Component\ClassLoader\UniversalClassLoader;
...@@ -323,19 +324,27 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -323,19 +324,27 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$matcher = new RedirectableUrlMatcher($this['routes'], $this['request_context']); $matcher = new RedirectableUrlMatcher($this['routes'], $this['request_context']);
$this['dispatcher']->dispatch(Events::onSilexBefore);
try { try {
$attributes = $matcher->match($this['request']->getPathInfo()); $attributes = $matcher->match($this['request']->getPathInfo());
$this['request']->attributes->add($attributes); $this['request']->attributes->add($attributes);
} catch (NotFoundException $e) { } catch (MatcherException $e) {
$message = sprintf('No route found for "%s %s"', $this['request']->getMethod(), $this['request']->getPathInfo()); // make sure onSilexBefore event is dispatched
throw new NotFoundHttpException($message, $e);
} catch (MethodNotAllowedException $e) { $this['dispatcher']->dispatch(Events::onSilexBefore);
$message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $this['request']->getMethod(), $this['request']->getPathInfo(), strtoupper(implode(', ', $e->getAllowedMethods())));
throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e); if ($e instanceof NotFoundException) {
$message = sprintf('No route found for "%s %s"', $this['request']->getMethod(), $this['request']->getPathInfo());
throw new NotFoundHttpException($message, $e);
} else if ($e instanceof MethodNotAllowedException) {
$message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $this['request']->getMethod(), $this['request']->getPathInfo(), strtoupper(implode(', ', $e->getAllowedMethods())));
throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
}
throw $e;
} }
$this['dispatcher']->dispatch(Events::onSilexBefore);
} }
/** /**
......
...@@ -162,4 +162,22 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase ...@@ -162,4 +162,22 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(2, $i); $this->assertEquals(2, $i);
} }
public function testRequestShouldBePopulatedOnBefore() {
$app = new Application();
$app->before(function () use ($app) {
$app['locale'] = $app['request']->get('locale');
});
$app->match('/foo/{locale}', function () use ($app) {
return $app['locale'];
});
$request = Request::create('/foo/en');
$this->assertEquals('en', $app->handle($request)->getContent());
$request = Request::create('/foo/de');
$this->assertEquals('de', $app->handle($request)->getContent());
}
} }
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