Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't clear PHP buffer when an error occurs with debug=true #3059

Merged
merged 1 commit into from
Jun 10, 2019
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
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
* 1.42.2 (2019-XX-XX)

* n/a
* Display partial output (PHP buffer) when an error occurs in debug mode

* 1.42.1 (2019-06-04)

Expand Down
2 changes: 1 addition & 1 deletion src/Extension/DebugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function twig_var_dump(Environment $env, $context, array $vars = [])
return;
}

ob_start(function () { return ''; });
ob_start();
fabpot marked this conversation as resolved.
Show resolved Hide resolved

if (!$vars) {
$vars = [];
Expand Down
8 changes: 7 additions & 1 deletion src/Node/MacroNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ public function compile(Compiler $compiler)
->outdent()
->write("]);\n\n")
->write("\$blocks = [];\n\n")
->write("ob_start(function () { return ''; });\n")
;
if ($compiler->getEnvironment()->isDebug()) {
fabpot marked this conversation as resolved.
Show resolved Hide resolved
$compiler->write("ob_start();\n");
} else {
$compiler->write("ob_start(function () { return ''; });\n");
}
$compiler
->write("try {\n")
->indent()
->subcompile($this->getNode('body'))
Expand Down
6 changes: 5 additions & 1 deletion src/Node/SetNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ public function compile(Compiler $compiler)
$compiler->raw(')');
} else {
if ($this->getAttribute('capture')) {
if ($compiler->getEnvironment()->isDebug()) {
$compiler->write("ob_start();\n");
} else {
$compiler->write("ob_start(function () { return ''; });\n");
}
$compiler
->write("ob_start(function () { return ''; });\n")
->subcompile($this->getNode('values'))
;
}
Expand Down
8 changes: 7 additions & 1 deletion src/Node/SpacelessNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("ob_start(function () { return ''; });\n")
;
if ($compiler->getEnvironment()->isDebug()) {
$compiler->write("ob_start();\n");
} else {
$compiler->write("ob_start(function () { return ''; });\n");
}
$compiler
->subcompile($this->getNode('body'))
->write("echo trim(preg_replace('/>\s+</', '><', ob_get_clean()));\n")
;
Expand Down
18 changes: 15 additions & 3 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ public function displayBlock($name, array $context, array $blocks = [], $useBloc
*/
public function renderParentBlock($name, array $context, array $blocks = [])
{
ob_start(function () { return ''; });
if ($this->env->isDebug()) {
ob_start();
} else {
ob_start(function () { return ''; });
}
$this->displayParentBlock($name, $context, $blocks);

return ob_get_clean();
Expand All @@ -274,7 +278,11 @@ public function renderParentBlock($name, array $context, array $blocks = [])
*/
public function renderBlock($name, array $context, array $blocks = [], $useBlocks = true)
{
ob_start(function () { return ''; });
if ($this->env->isDebug()) {
ob_start();
} else {
ob_start(function () { return ''; });
}
$this->displayBlock($name, $context, $blocks, $useBlocks);

return ob_get_clean();
Expand Down Expand Up @@ -417,7 +425,11 @@ public function display(array $context, array $blocks = [])
public function render(array $context)
{
$level = ob_get_level();
ob_start(function () { return ''; });
if ($this->env->isDebug()) {
ob_start();
} else {
ob_start(function () { return ''; });
}
try {
$this->display($context);
} catch (\Exception $e) {
Expand Down
6 changes: 5 additions & 1 deletion src/TemplateWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ public function renderBlock($name, $context = [])
{
$context = $this->env->mergeGlobals($context);
$level = ob_get_level();
ob_start(function () { return ''; });
if ($this->env->isDebug()) {
ob_start();
} else {
ob_start(function () { return ''; });
}
try {
$this->template->displayBlock($name, $context);
} catch (\Exception $e) {
Expand Down
14 changes: 14 additions & 0 deletions test/Twig/Tests/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ public function getErroredTemplates()
],
];
}

public function testTwigLeakOutputInDebugMode()
{
$output = exec(sprintf('%s %s debug', \PHP_BINARY, __DIR__.'/Fixtures/errors/leak-output.php'));
Copy link
Member

Choose a reason for hiding this comment

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

you need to escape the argument for the path (__DIR__ might have spaces in it)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. #3062


$this->assertSame('Hello OOPS', $output);
}

public function testDoesNotTwigLeakOutput()
{
$output = exec(sprintf('%s %s', \PHP_BINARY, __DIR__.'/Fixtures/errors/leak-output.php'));

$this->assertSame('', $output);
}
}

class Twig_Tests_ErrorTest_Foo
Expand Down
31 changes: 31 additions & 0 deletions test/Twig/Tests/Fixtures/errors/leak-output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

require __DIR__.'/../../../../../vendor/autoload.php';

use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Loader\ArrayLoader;
use Twig\TwigFilter;

class BrokenExtension extends AbstractExtension
{
public function getFilters()
{
return [
new TwigFilter('broken', [$this, 'broken']),
];
}

public function broken()
{
die('OOPS');
}
}

$loader = new ArrayLoader([
'index.html.twig' => 'Hello {{ "world"|broken }}',
]);
$twig = new Environment($loader, ['debug' => isset($argv[1])]);
$twig->addExtension(new BrokenExtension());

echo $twig->render('index.html.twig');