Skip to content

Commit ddf3331

Browse files
committed
[FrameworkBundle] fixed output buffering when an error occurs in a sub-request
1 parent 8ee4f64 commit ddf3331

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

HttpKernel.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function render($controller, array $options = array())
135135
$subRequest = $request->duplicate($options['query'], null, $options['attributes']);
136136
}
137137

138+
$level = ob_get_level();
138139
try {
139140
$response = $this->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
140141

@@ -156,6 +157,11 @@ public function render($controller, array $options = array())
156157
if (!$options['ignore_errors']) {
157158
throw $e;
158159
}
160+
161+
// let's clean up the output buffers that were created by the sub-request
162+
while (ob_get_level() > $level) {
163+
ob_get_clean();
164+
}
159165
}
160166
}
161167

Tests/HttpKernelTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,53 @@ public function getProviderTypes()
166166
array(HttpKernelInterface::SUB_REQUEST),
167167
);
168168
}
169+
170+
public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
171+
{
172+
$request = new Request();
173+
174+
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
175+
$container
176+
->expects($this->at(0))
177+
->method('getParameter')
178+
->with($this->equalTo('kernel.debug'))
179+
->will($this->returnValue(false))
180+
;
181+
$container
182+
->expects($this->at(1))
183+
->method('has')
184+
->with($this->equalTo('esi'))
185+
->will($this->returnValue(false))
186+
;
187+
$container
188+
->expects($this->at(2))
189+
->method('get')
190+
->with($this->equalTo('request'))
191+
->will($this->returnValue($request))
192+
;
193+
194+
$dispatcher = new EventDispatcher();
195+
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
196+
$resolver->expects($this->once())
197+
->method('getController')
198+
->will($this->returnValue(function () {
199+
ob_start();
200+
echo 'bar';
201+
throw new \RuntimeException();
202+
}));
203+
$resolver->expects($this->once())
204+
->method('getArguments')
205+
->will($this->returnValue(array()));
206+
207+
$kernel = new HttpKernel($dispatcher, $container, $resolver);
208+
209+
// simulate a main request with output buffering
210+
ob_start();
211+
echo 'Foo';
212+
213+
// simulate a sub-request with output buffering and an exception
214+
$kernel->render('/');
215+
216+
$this->assertEquals('Foo', ob_get_clean());
217+
}
169218
}

0 commit comments

Comments
 (0)