Skip to content

Commit 68ba253

Browse files
committed
Update atom node body handling
1 parent 2fa2857 commit 68ba253

6 files changed

Lines changed: 37 additions & 16 deletions

File tree

config/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ services:
1717
$kernelInterface: "@kernel"
1818
$parameterBag: "@parameter_bag"
1919
$cache: "@twig.runtime.cache"
20+
$requestStack: "@request_stack"
2021

2122
tom_atom.service.atom_runtime:
2223
class: TomAtom\AtomBundle\Services\AtomRuntime
2324
tags: [ 'twig.runtime' ]
2425
arguments:
2526
$requestStack: '@request_stack'
27+
$nodeHelper: '@tom_atom.service.node_helper'
2628

2729
tom_atom.service.deepl:
2830
class: TomAtom\AtomBundle\Services\DeepLService

src/Services/AtomRuntime.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class AtomRuntime
88
{
99
private RequestStack $requestStack;
1010

11-
public function __construct(RequestStack $requestStack)
11+
public function __construct(RequestStack $requestStack, private readonly NodeHelper $nodeHelper)
1212
{
1313
$this->requestStack = $requestStack;
1414
}
@@ -18,4 +18,8 @@ public function getRequestStack(): RequestStack
1818
return $this->requestStack;
1919
}
2020

21+
public function getAtomBody(string $name, string $defaultBody): string
22+
{
23+
return $this->nodeHelper->getAtomBody($name, $defaultBody);
24+
}
2125
}

src/Services/NodeHelper.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\ORM\EntityManagerInterface;
66
use Psr\Cache\InvalidArgumentException;
77
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
8+
use Symfony\Component\HttpFoundation\RequestStack;
89
use Symfony\Component\HttpKernel\KernelInterface;
910
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1011
use Symfony\Contracts\Cache\ItemInterface;
@@ -19,18 +20,21 @@ class NodeHelper
1920
protected KernelInterface $kernel;
2021
protected ParameterBagInterface $parameterBag;
2122
protected CacheRuntime $cache;
23+
protected RequestStack $requestStack;
2224

2325
public function __construct(EntityManagerInterface $entityManager,
2426
AuthorizationCheckerInterface $authorizationChecker,
2527
KernelInterface $kernelInterface,
2628
ParameterBagInterface $parameterBag,
27-
CacheRuntime $cache)
29+
CacheRuntime $cache,
30+
RequestStack $requestStack)
2831
{
2932
$this->entityManager = $entityManager;
3033
$this->authorizationChecker = $authorizationChecker;
3134
$this->kernel = $kernelInterface;
3235
$this->parameterBag = $parameterBag;
3336
$this->cache = $cache;
37+
$this->requestStack = $requestStack;
3438
}
3539

3640
/**
@@ -155,6 +159,23 @@ public function checkAtomEntity($name, $method, $id, $body, $isAdmin = null)
155159
return $result;
156160
}
157161

162+
public function getAtomBody(string $name, string $defaultBody): string
163+
{
164+
$env = $this->kernel->getEnvironment();
165+
166+
if ($env === 'prod') {
167+
$atom = $this->entityManager->getRepository(Atom::class)->findOneBy(['name' => $name]);
168+
if (!$atom) {
169+
return $defaultBody;
170+
}
171+
172+
$currentLocale = $this->requestStack->getCurrentRequest()?->getLocale() ?? $this->getDefaultLocale();
173+
return $atom->translate($currentLocale, false)->getBody() ?? '';
174+
}
175+
176+
return $defaultBody;
177+
}
178+
158179
public function getDefaultLocale(): mixed
159180
{
160181
return $this->parameterBag->get('kernel.default_locale');

src/Twig/AtomNodeVisitor.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use Psr\Cache\InvalidArgumentException;
66
use TomAtom\AtomBundle\Services\NodeHelper;
77
use Twig\Environment;
8-
use Twig\Node\BodyNode;
98
use Twig\Node\Node;
10-
use Twig\Node\TextNode;
119
use Twig\NodeVisitor\NodeVisitorInterface;
1210

1311
class AtomNodeVisitor implements NodeVisitorInterface
@@ -35,10 +33,7 @@ public function enterNode(Node $node, Environment $env): Node
3533
if ($node->getNode('body')->hasAttribute('data')) {
3634
$defaultBody = $node->getNode('body')->getAttribute('data');
3735
}
38-
$body = $this->nodeHelper->checkAtom($atomName, $defaultBody, 'atom', false);
39-
$node->setNode('body', new BodyNode([
40-
new TextNode(!is_null($body) ? $body : '', 1),
41-
]));
36+
$this->nodeHelper->checkAtom($atomName, $defaultBody, 'atom', false);
4237
$node->setAttribute('default_locale', $this->nodeHelper->getDefaultLocale());
4338
}
4439

@@ -48,10 +43,7 @@ public function enterNode(Node $node, Environment $env): Node
4843
if ($node->getNode('body')->hasAttribute('data')) {
4944
$defaultBody = $node->getNode('body')->getAttribute('data');
5045
}
51-
$body = $this->nodeHelper->checkAtomLine($atomName, $defaultBody, false);
52-
$node->setNode('body', new BodyNode([
53-
new TextNode(!is_null($body) ? $body : '', 1),
54-
]));
46+
$this->nodeHelper->checkAtomLine($atomName, $defaultBody, false);
5547
}
5648

5749
return $node;

src/Twig/NodeAtom.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ public function compile(Compiler $compiler): void
2828
->raw('$cacheKey = "' . $this->getAttribute('name') . '_" . $this->env->getRuntime(\'TomAtom\AtomBundle\Services\AtomRuntime\')->getRequestStack()->getCurrentRequest()->getLocale();' . "\n")
2929
->raw("\$cached = \$this->env->getRuntime('Twig\Extra\Cache\CacheRuntime')->getCache()->get(\$cacheKey, function (\Symfony\Contracts\Cache\ItemInterface \$item) use (\$context, \$macros) {\n")
3030
->indent()
31-
->raw("return implode('', iterator_to_array((function () use (\$context, \$macros) {\n")
31+
->raw('$defaultBody = implode(\'\', iterator_to_array((function () use ($context, $macros) {' . "\n")
3232
->indent()
3333
->subcompile($this->getNode('body'))
3434
->raw("return; yield '';\n")
3535
->outdent()
36-
->raw('})(), false));')
36+
->raw('})(), false));' . "\n")
37+
->raw('return $this->env->getRuntime(\'TomAtom\AtomBundle\Services\AtomRuntime\')->getAtomBody(\'' . $this->getAttribute('name') . '\', $defaultBody);' . "\n")
3738
->outdent()
3839
->raw("});\n")
3940
->raw("if (\$isAdmin) {\n")

src/Twig/NodeAtomLine.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ public function compile(Compiler $compiler): void
2828
->raw('$cacheKey = "' . $this->getAttribute('name') . '_" . $this->env->getRuntime(\'TomAtom\AtomBundle\Services\AtomRuntime\')->getRequestStack()->getCurrentRequest()->getLocale();' . "\n")
2929
->raw("\$cached = \$this->env->getRuntime('Twig\Extra\Cache\CacheRuntime')->getCache()->get(\$cacheKey, function (\Symfony\Contracts\Cache\ItemInterface \$item) use (\$context, \$macros) {\n")
3030
->indent()
31-
->raw("return implode('', iterator_to_array((function () use (\$context, \$macros) {\n")
31+
->raw('$defaultBody = implode(\'\', iterator_to_array((function () use ($context, $macros) {' . "\n")
3232
->indent()
3333
->subcompile($this->getNode('body'))
3434
->raw("return; yield '';\n")
3535
->outdent()
36-
->raw('})(), false));')
36+
->raw('})(), false));' . "\n")
37+
->raw('return $this->env->getRuntime(\'TomAtom\AtomBundle\Services\AtomRuntime\')->getAtomBody(\'' . $this->getAttribute('name') . '\', $defaultBody);' . "\n")
3738
->outdent()
3839
->raw("});\n")
3940
->raw("\$cached = strip_tags(\$cached);\n")

0 commit comments

Comments
 (0)