Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function render($uri, Request $request, array $options = array())
$reference = null;
if ($uri instanceof ControllerReference) {
$reference = $uri;
$uri = $this->generateFragmentUri($uri, $request);
$uri = $this->generateFragmentUri($uri, $request, false);
}

$subRequest = $this->createSubRequest($uri, $request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ public function setFragmentPath($path)
* Generates a fragment URI for a given controller.
*
* @param ControllerReference $reference A ControllerReference instance
* @param Request $request A Request instance
* @param Request $request A Request instance
* @param Boolean $strict Whether to allow non-scalar attributes or not
*
* @return string A fragment URI
*/
protected function generateFragmentUri(ControllerReference $reference, Request $request)
protected function generateFragmentUri(ControllerReference $reference, Request $request, $strict = true)
{
if ($strict) {
$this->checkNonScalar($reference->attributes);
}

if (!isset($reference->attributes['_format'])) {
$reference->attributes['_format'] = $request->getRequestFormat();
}
Expand All @@ -56,4 +61,15 @@ protected function generateFragmentUri(ControllerReference $reference, Request $

return $request->getUriForPath($this->fragmentPath.'?'.http_build_query($reference->query, '', '&'));
}

private function checkNonScalar($values)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be typehinted as array ?

{
foreach ($values as $key => $value) {
if (is_array($value)) {
$this->checkNonScalar($value);
} elseif (!is_scalar($value)) {
throw new \LogicException(sprintf('Controller attributes cannot contain non-scalar values (value for key "%s" is not a scalar).', $key));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add gettype($value) to show the actual type passed

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ public function testRenderWithObjectsAsAttributes()
$strategy->render(new ControllerReference('main_controller', array('object' => $object), array()), Request::create('/'));
}

public function testRenderWithObjectsAsAttributesPassedAsObjectsInTheController()
{
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolver', array('getController'));
$resolver
->expects($this->once())
->method('getController')
->will($this->returnValue(function (\stdClass $object, Bar $object1) {
return new Response($object1->getBar());
}))
;

$kernel = new HttpKernel(new EventDispatcher(), $resolver);
$renderer = new InlineFragmentRenderer($kernel);

$response = $renderer->render(new ControllerReference('main_controller', array('object' => new \stdClass(), 'object1' => new Bar()), array()), Request::create('/'));
$this->assertEquals('bar', $response->getContent());
}

/**
* @expectedException \RuntimeException
*/
Expand Down Expand Up @@ -168,3 +186,12 @@ public function testESIHeaderIsKeptInSubrequest()
$strategy->render('/', $request);
}
}

class Bar {
public $bar = 'bar';

public function getBar()
{
return $this->bar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\Fragment\RoutableFragmentRenderer;

class RoutableFragmentRendererTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -22,7 +21,7 @@ class RoutableFragmentRendererTest extends \PHPUnit_Framework_TestCase
*/
public function testGenerateFragmentUri($uri, $controller)
{
$this->assertEquals($uri, $this->getRenderer()->doGenerateFragmentUri($controller, Request::create('/')));
$this->assertEquals($uri, $this->callGenerateFragmentUriMethod($controller, Request::create('/')));
}

public function getGenerateFragmentUriData()
Expand All @@ -33,6 +32,7 @@ public function getGenerateFragmentUriData()
array('http://localhost/_fragment?_path=foo%3Dfoo%26_format%3Djson%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_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))),
array('http://localhost/_fragment?foo=foo&_path=_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array(), array('foo' => 'foo'))),
array('http://localhost/_fragment?_path=foo%255B0%255D%3Dfoo%26foo%255B1%255D%3Dbar%26_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => array('foo', 'bar')), array())),
);
}

Expand All @@ -42,22 +42,43 @@ public function testGenerateFragmentUriWithARequest()
$request->attributes->set('_format', 'json');
$controller = new ControllerReference('controller', array(), array());

$this->assertEquals('http://localhost/_fragment?_path=_format%3Djson%26_controller%3Dcontroller', $this->getRenderer()->doGenerateFragmentUri($controller, $request));
$this->assertEquals('http://localhost/_fragment?_path=_format%3Djson%26_controller%3Dcontroller', $this->callGenerateFragmentUriMethod($controller, $request));
}

private function getRenderer()
/**
* @expectedException LogicException
* @dataProvider getGenerateFragmentUriDataWithNonScalar
*/
public function testGenerateFragmentUriWithNonScalar($controller)
{
$this->callGenerateFragmentUriMethod($controller, Request::create('/'));
}

public function getGenerateFragmentUriDataWithNonScalar()
{
return new Renderer();
return array(
array(new ControllerReference('controller', array('foo' => new Foo(), 'bar' => 'bar'), array())),
array(new ControllerReference('controller', array('foo' => array('foo' => 'foo'), 'bar' => array('bar' => new Foo())), array())),
);
}

private function callGenerateFragmentUriMethod(ControllerReference $reference, Request $request)
{
$renderer = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Fragment\RoutableFragmentRenderer');
$r = new \ReflectionObject($renderer);
$m = $r->getMethod('generateFragmentUri');
$m->setAccessible(true);

return $m->invoke($renderer, $reference, $request);
}
}

class Renderer extends RoutableFragmentRenderer
class Foo
{
public function render($uri, Request $request, array $options = array()) {}
public function getName() {}
public $foo;

public function doGenerateFragmentUri(ControllerReference $reference, Request $request)
public function getFoo()
{
return parent::generateFragmentUri($reference, $request);
return $this->foo;
}
}