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

Makes the TemplateBoxNode yield ready #412

Merged
merged 2 commits into from
Sep 9, 2024
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
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