[HttpKernel] changed fragment URLs to be relative by default (closes #8458)

This commit is contained in:
Fabien Potencier 2013-08-29 07:19:56 +02:00
parent 0009deb055
commit 91234cd89a
4 changed files with 28 additions and 13 deletions

View File

@ -40,11 +40,12 @@ abstract class RoutableFragmentRenderer implements FragmentRendererInterface
* Generates a fragment URI for a given controller. * Generates a fragment URI for a given controller.
* *
* @param ControllerReference $reference A ControllerReference instance * @param ControllerReference $reference A ControllerReference instance
* @param Request $request A Request instance * @param Request $request A Request instance
* @param Boolean $absolute Whether to generate an absolute URL or not
* *
* @return string A fragment URI * @return string A fragment URI
*/ */
protected function generateFragmentUri(ControllerReference $reference, Request $request) protected function generateFragmentUri(ControllerReference $reference, Request $request, $absolute = false)
{ {
// We need to forward the current _format and _locale values as we don't have // We need to forward the current _format and _locale values as we don't have
// a proper routing pattern to do the job for us. // a proper routing pattern to do the job for us.
@ -62,6 +63,12 @@ abstract class RoutableFragmentRenderer implements FragmentRendererInterface
$reference->query['_path'] = http_build_query($reference->attributes, '', '&'); $reference->query['_path'] = http_build_query($reference->attributes, '', '&');
return $request->getUriForPath($this->fragmentPath.'?'.http_build_query($reference->query, '', '&')); $path = $this->fragmentPath.'?'.http_build_query($reference->query, '', '&');
if ($absolute) {
return $request->getUriForPath($path);
}
return $request->getBaseUrl().$path;
} }
} }

View File

@ -48,7 +48,7 @@ class EsiFragmentRendererTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent()); $this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
$this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent()); $this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent());
$this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo'))->getContent()); $this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo'))->getContent());
$this->assertEquals('<esi:include src="http://localhost/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" alt="http://localhost/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dalt_controller" />', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent()); $this->assertEquals('<esi:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" alt="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dalt_controller" />', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent());
} }
private function getInlineStrategy($called = false) private function getInlineStrategy($called = false)

View File

@ -38,7 +38,7 @@ class HIncludeFragmentRendererTest extends \PHPUnit_Framework_TestCase
{ {
$strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo')); $strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));
$this->assertEquals('<hx:include src="http://localhost/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller&amp;_hash=g4b3vtCnhkZBFKrciEFwG7fucVo%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); $this->assertEquals('<hx:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller&amp;_hash=5RZ1IkwF487EaXt6buHka73CCtQ%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
} }
public function testRenderWithUri() public function testRenderWithUri()

View File

@ -25,14 +25,22 @@ class RoutableFragmentRendererTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($uri, $this->getRenderer()->doGenerateFragmentUri($controller, Request::create('/'))); $this->assertEquals($uri, $this->getRenderer()->doGenerateFragmentUri($controller, Request::create('/')));
} }
/**
* @dataProvider getGenerateFragmentUriData
*/
public function testGenerateAbsoluteFragmentUri($uri, $controller)
{
$this->assertEquals('http://localhost'.$uri, $this->getRenderer()->doGenerateFragmentUri($controller, Request::create('/'), true));
}
public function getGenerateFragmentUriData() public function getGenerateFragmentUriData()
{ {
return array( return array(
array('http://localhost/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array(), array())), array('/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array(), array())),
array('http://localhost/_fragment?_path=_format%3Dxml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('_format' => 'xml'), array())), array('/_fragment?_path=_format%3Dxml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('_format' => 'xml'), array())),
array('http://localhost/_fragment?_path=foo%3Dfoo%26_format%3Djson%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo', '_format' => 'json'), array())), array('/_fragment?_path=foo%3Dfoo%26_format%3Djson%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo', '_format' => 'json'), array())),
array('http://localhost/_fragment?bar=bar&_path=foo%3Dfoo%26_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))), array('/_fragment?bar=bar&_path=foo%3Dfoo%26_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))),
array('http://localhost/_fragment?foo=foo&_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array(), array('foo' => 'foo'))), array('/_fragment?foo=foo&_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array(), array('foo' => 'foo'))),
); );
} }
@ -43,7 +51,7 @@ class RoutableFragmentRendererTest extends \PHPUnit_Framework_TestCase
$request->setLocale('fr'); $request->setLocale('fr');
$controller = new ControllerReference('controller', array(), array()); $controller = new ControllerReference('controller', array(), array());
$this->assertEquals('http://localhost/_fragment?_path=_format%3Djson%26_locale%3Dfr%26_controller%3Dcontroller', $this->getRenderer()->doGenerateFragmentUri($controller, $request)); $this->assertEquals('/_fragment?_path=_format%3Djson%26_locale%3Dfr%26_controller%3Dcontroller', $this->getRenderer()->doGenerateFragmentUri($controller, $request));
} }
private function getRenderer() private function getRenderer()
@ -57,8 +65,8 @@ class Renderer extends RoutableFragmentRenderer
public function render($uri, Request $request, array $options = array()) {} public function render($uri, Request $request, array $options = array()) {}
public function getName() {} public function getName() {}
public function doGenerateFragmentUri(ControllerReference $reference, Request $request) public function doGenerateFragmentUri(ControllerReference $reference, Request $request, $absolute = false)
{ {
return parent::generateFragmentUri($reference, $request); return parent::generateFragmentUri($reference, $request, $absolute);
} }
} }