for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Shoot\Shoot\Twig\Node;
use Shoot\Shoot\SuppressedException;
use Twig\Compiler;
use Twig\Error\RuntimeError;
use Twig\Node\Node;
/**
* Represents the optional tag used to suppress runtime exceptions in templates.
*
* @internal
*/
final class OptionalNode extends Node
{
* @param Node $body
* @param int $lineNumber
* @param string $tag
public function __construct(Node $body, int $lineNumber, string $tag)
parent::__construct(['body' => $body], [], $lineNumber, $tag);
}
* @param Compiler $compiler
* @return void
public function compile(Compiler $compiler): void
$runtimeErrorClass = RuntimeError::class;
$suppressedExceptionClass = SuppressedException::class;
$compiler
->write("try {\n")
->indent()
->write("ob_start();\n\n")
->subcompile($this->getNode('body'))
->raw("\n")
->write("echo ob_get_clean();\n")
->outdent()
->write("} catch ({$runtimeErrorClass} \$exception) {\n")
->write("ob_end_clean();\n\n")
->write("if (\$exception->getPrevious() === null) {\n")
->write("throw \$exception;\n")
->write("}\n\n")
->write("\$suppressedException = new {$suppressedExceptionClass}(\$exception->getPrevious());\n")
->write("}\n\n");