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

Fix compatibility with twig 3.9 and yielding #344

Merged
merged 1 commit into from
Jun 2, 2024
Merged
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
42 changes: 31 additions & 11 deletions src/Twig/Node/CSPNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,60 @@
* file that was distributed with this source code.
*/

use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\CaptureNode;
use Twig\Node\Node;

#[YieldReady]
final class CSPNode extends Node
{
private ?string $sha;
private string $directive;

public function __construct(Node $body, int $lineno, string $tag, string $directive, ?string $sha = null)
{
if (class_exists(CaptureNode::class)) {
$body = new CaptureNode($body, $lineno, $tag);
$body->setAttribute('raw', true);
}

parent::__construct(['body' => $body], [], $lineno, $tag);
$this->sha = $sha;
$this->directive = $directive;
}

public function compile(Compiler $compiler): void
{
$body = $this->getNode('body');
if (class_exists(CaptureNode::class)) {
$compiler
->addDebugInfo($this)
->indent()
->write("\$content = ")
->subcompile($this->getNode('body'))
->raw("\n")
->outdent()
;
} else {
$compiler
->addDebugInfo($this)
->indent()
->write("ob_start();\n")
->subcompile($this->getNode('body'))
->outdent()
->write("\$content = ob_get_clean();\n")
;
}

if (null !== $this->sha) {
$output = "\$this->env->getRuntime('Nelmio\SecurityBundle\Twig\CSPRuntime')->getListener()->addSha('{$this->directive}', '{$this->sha}');\necho ob_get_clean();\n";
$compiler->write("\$this->env->getRuntime('Nelmio\SecurityBundle\Twig\CSPRuntime')->getListener()->addSha('{$this->directive}', '{$this->sha}');\n");
} elseif ('script-src' === $this->directive) {
$output = "\$script = ob_get_clean();\n\$this->env->getRuntime('Nelmio\SecurityBundle\Twig\CSPRuntime')->getListener()->addScript(\$script);\necho \$script;\n";
$compiler->write("\$this->env->getRuntime('Nelmio\SecurityBundle\Twig\CSPRuntime')->getListener()->addScript(\$content);\n");
} elseif ('style-src' === $this->directive) {
$output = "\$style = ob_get_clean();\n\$this->env->getRuntime('Nelmio\SecurityBundle\Twig\CSPRuntime')->getListener()->addStyle(\$style);\necho \$style;\n";
$compiler->write("\$this->env->getRuntime('Nelmio\SecurityBundle\Twig\CSPRuntime')->getListener()->addStyle(\$content);\n");
} else {
throw new \InvalidArgumentException(sprintf('Unable to compile for directive "%s"', $this->directive));
}

$compiler
->addDebugInfo($this)
->write("ob_start();\n")
->subcompile($body)
->write($output)
;
$compiler->write("echo \$content;\n");
Copy link
Member

Choose a reason for hiding this comment

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

I still get deprecation warnings in the test suite which I think are coming from this line?

4x: Since twig/twig 3.9: Using "echo" is deprecated, use "yield" instead in "Nelmio\SecurityBundle\Twig\Node\CSPNode", then flag the class with #[YieldReady].
2x in IntegrationTest::testItWorksDynamically from Nelmio\SecurityBundle\Tests\Twig
2x in IntegrationTest::testItWorksStatically from Nelmio\SecurityBundle\Tests\Twig

Should this not be a yield?

Copy link
Contributor Author

@jderusse jderusse Jul 1, 2024

Choose a reason for hiding this comment

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

indeed :(

fixed in #353

}
}