8000 merged branch usefulthink/fix-interal-uri-generator (PR #1747) · yktd26/symfony@6108b8d · GitHub
[go: up one dir, main page]

Skip to content

Commit 6108b8d

Browse files
committed
merged branch usefulthink/fix-interal-uri-generator (PR symfony#1747)
Commits ------- 79e4ed6 simplified assignment 04302ff simplified ternary-operator 3a285c1 fixed handling of null-values in attribute- and query-arrays Discussion ---------- [FrameworkBundle] fixed handling of null-values in attribute- and query-arrays when esi is enabled and internal uris are generated for esi-tags, an attribute-array consisting entirely of null-values isn't handled correctly. The reason is that php's `http_build_query()`-method outputs an empty string for such arrays: http_build_query(array('foo' => '')) == 'foo=' http_build_query(array('foo' => null)) == '' In the latter case, the generation of an URI in [`HttpKernel::generateInternalUri()`][code1] generates an URI that could not be matched by the corresponding route (ex. `_internal/Controller/.html` opposed to `_internal/Controller/none.html` which should be expected). This commit adds a possible solution as well as a simple test for this issue. [code1]: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/HttpKernel.php#L179
2 parents e42a2de + 79e4ed6 commit 6108b8d

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/HttpKernel.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,15 @@ public function generateInternalUri($controller, array $attributes = array(), ar
174174
return $controller;
175175
}
176176

177+
$path = http_build_query($attributes);
177178
$uri = $this->container->get('router')->generate('_internal', array(
178179
'controller' => $controller,
179-
'path' => $attributes ? http_build_query($attributes) : 'none',
180+
'path' => $path ?: 'none',
180181
'_format' => $this->container->get('request')->getRequestFormat(),
181182
));
182183

183-
if ($query) {
184-
$uri = $uri.'?'.http_build_query($query);
184+
if ($queryString = http_build_query($query)) {
185+
$uri .= '?'.$queryString;
185186
}
186187

187188
return $uri;

src/Symfony/Bundle/FrameworkBundle/Tests/HttpKernelTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,47 @@ public function testHandleRestoresThePreviousRequestOnException($type)
119119
}
120120
}
121121

122+
public function testGenerateInternalUriHandlesNullValues()
123+
{
124+
$request = new Request();
125+
126+
$router = $this->getMock('Symfony\\Component\\Routing\\RouterInterface');
127+
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
128+
$container
129+
->expects($this->at(0))
130+
->method('get')
131+
->with($this->equalTo('router'))
132+
->will($this->returnValue($router))
133+
;
134+
$container
135+
->expects($this->at('1'))
136+
->method('get')
137+
->with($this->equalTo('request'))
138+
->will($this->returnValue($request))
139+
;
140+
141+
$controller = 'AController';
142+
$attributes = array('anAttribute' => null);
143+
$query = array('aQueryParam' => null);
144+
145+
$expectedPath = 'none';
146+
147+
$routeParameters = array('controller' => $controller, 'path' => $expectedPath, '_format' => 'html');
148+
$router
149+
->expects($this->once())
150+
->method('generate')
151+
->with($this->equalTo('_internal'), $this->equalTo($routeParameters))
152+
->will($this->returnValue('GENERATED_URI'))
153+
;
154+
155+
$dispatcher = new EventDispatcher();
156+
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
157+
$kernel = new HttpKernel($dispatcher, $container, $resolver);
158+
159+
$uri = $kernel->generateInternalUri($controller, $attributes, $query);
160+
$this->assertEquals('GENERATED_URI', $uri);
161+
}
162+
122163
public function getProviderTypes()
123164
{
124165
return array(

0 commit comments

Comments
 (0)
0