Skip to content

Commit

Permalink
Makes the TemplateBoxNode yield ready (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanStorm authored Sep 9, 2024
1 parent 6bba2eb commit cdc475c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/FlashMessage/FlashManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class FlashManager implements FlashManagerInterface
public function __construct(
private RequestStack $requestStack,
private array $types,
private array $cssClasses
private array $cssClasses,
) {
}

Expand Down Expand Up @@ -127,7 +127,7 @@ private function getSession(): SessionInterface

$session = $request->getSession();
if (!$session instanceof Session) {
throw new \UnexpectedValueException(sprintf(
throw new \UnexpectedValueException(\sprintf(
'The flash manager only works with a "%s" session.',
Session::class
));
Expand Down
7 changes: 5 additions & 2 deletions src/Node/TemplateBoxNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

namespace Sonata\Twig\Node;

use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Node;

#[YieldReady]
final class TemplateBoxNode extends Node
{
/**
Expand All @@ -29,7 +31,7 @@ public function __construct(
AbstractExpression $message,
private bool $enabled,
?int $lineno = null,
?string $tag = null
?string $tag = null,
) {
parent::__construct(['message' => $message], [], $lineno ?? 0, $tag);
}
Expand All @@ -54,7 +56,8 @@ public function compile(Compiler $compiler): void
</div>"
CODE;

$display = class_exists(YieldReady::class) ? 'yield' : 'echo';
$compiler
->write("echo $message;");
->write("$display $message;");
}
}
2 changes: 1 addition & 1 deletion tests/App/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getProjectDir(): string

protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->import(sprintf('%s/config/routes.yaml', $this->getProjectDir()));
$routes->import(\sprintf('%s/config/routes.yaml', $this->getProjectDir()));
}

protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader): void
Expand Down
16 changes: 10 additions & 6 deletions tests/Node/TemplateBoxNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\Twig\Tests\Node;

use Sonata\Twig\Node\TemplateBoxNode;
use Twig\Attribute\YieldReady;
use Twig\Environment;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Node;
Expand Down Expand Up @@ -51,19 +52,22 @@ public function getTests(): iterable
1,
'sonata_template_box'
);
yield [$nodeEn, <<<'EOF'

$display = class_exists(YieldReady::class) ? 'yield' : 'echo';

yield [$nodeEn, <<<EOF
// line 1
echo "<div class='alert alert-default alert-info'>
$display "<div class='alert alert-default alert-info'>
<strong>This is the default message</strong>
<div>This file can be found in <code>{$this->getTemplateName()}</code>.</div>
<div>This file can be found in <code>{\$this->getTemplateName()}</code>.</div>
</div>";
EOF, null, false,
];
yield [$nodeFr, <<<'EOF'
yield [$nodeFr, <<<EOF
// line 1
echo "<div class='alert alert-default alert-info'>
$display "<div class='alert alert-default alert-info'>
<strong>Ceci est le message par défaut</strong>
<div>This file can be found in <code>{$this->getTemplateName()}</code>.</div>
<div>This file can be found in <code>{\$this->getTemplateName()}</code>.</div>
</div>";
EOF, null, false,
];
Expand Down

0 comments on commit cdc475c

Please sign in to comment.