Commit add25faa authored by Fabien Potencier's avatar Fabien Potencier

fixed Request object accessibility from the application after handling

parent 5d1011c6
......@@ -3,6 +3,9 @@ Changelog
This changelog references all backward incompatibilities as we introduce them:
* **2012-05-20**: The Request instance is not available anymore from the
Application after it has been handled.
* **2012-04-01**: Added ``finish`` filters.
* **2012-03-20**: Added ``json`` helper::
......
......@@ -123,9 +123,11 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this['request.default_locale'] = 'en';
$this['request'] = function () {
$this['request_error'] = $this->protect(function () {
throw new \RuntimeException('Accessed request service outside of request scope. Try moving that call to a before handler or controller.');
};
});
$this['request'] = $this['request_error'];
$this['request.http_port'] = 80;
$this['request.https_port'] = 443;
......@@ -426,7 +428,11 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this->flush();
return $this['kernel']->handle($request, $type, $catch);
$response = $this['kernel']->handle($request, $type, $catch);
$this['request'] = $this['request_error'];
return $response;
}
/**
......
......@@ -49,17 +49,14 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
public function testGetRequest()
{
$app = new Application();
$app->get('/', function () {
return 'root';
});
$request = Request::create('/');
$app->handle($request);
$app = new Application();
$app->get('/', function (Request $req) use ($request) {
return $request === $req ? 'ok' : 'ko';
});
$this->assertEquals($request, $app['request']);
$this->assertEquals('ok', $app->handle($request)->getContent());
}
public function testGetRoutesWithNoRoutes()
......@@ -71,7 +68,7 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(0, count($routes->all()));
}
public function testgetRoutesWithRoutes()
public function testGetRoutesWithRoutes()
{
$app = new Application();
......@@ -321,6 +318,20 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$request = $app['request'];
}
/**
* @expectedException RuntimeException
*/
public function testAccessingRequestOutsideOfScopeShouldThrowRuntimeExceptionAfterHandling()
{
$app = new Application();
$app->get('/', function () {
return 'hello';
});
$app->handle(Request::create('/'), HttpKernelInterface::MASTER_REQUEST, false);
$request = $app['request'];
}
}
class FooController
......
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