Skip to content

Commit 667a141

Browse files
author
Benoît Burnichon
committed
Add a way to avoid throwing Exceptions
1 parent f09499f commit 667a141

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

Diff for: Translation/Extractor/File/TwigFileExtractor.php

+49-10
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,30 @@
1919
namespace JMS\TranslationBundle\Translation\Extractor\File;
2020

2121
use JMS\TranslationBundle\Exception\RuntimeException;
22-
use Symfony\Bridge\Twig\Node\TransNode;
23-
22+
use JMS\TranslationBundle\Logger\LoggerAwareInterface;
2423
use JMS\TranslationBundle\Model\FileSource;
2524
use JMS\TranslationBundle\Model\Message;
2625
use JMS\TranslationBundle\Model\MessageCatalogue;
2726
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
27+
use Psr\Log\LoggerInterface;
28+
use Symfony\Bridge\Twig\Node\TransNode;
2829

29-
class TwigFileExtractor implements FileVisitorInterface, \Twig_NodeVisitorInterface
30+
class TwigFileExtractor implements FileVisitorInterface, \Twig_NodeVisitorInterface, LoggerAwareInterface
3031
{
32+
/**
33+
* @var \SplFileInfo
34+
*/
3135
private $file;
3236
private $catalogue;
3337
private $traverser;
3438
private $stack = array();
3539
private $stackCount = 0;
3640

41+
/**
42+
* @var LoggerInterface
43+
*/
44+
private $logger;
45+
3746
public function __construct(\Twig_Environment $env)
3847
{
3948
$this->traverser = new \Twig_NodeTraverser($env, array($this));
@@ -47,6 +56,9 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
4756
$id = $node->getNode('body')->getAttribute('data');
4857
$domain = 'messages';
4958
if (null !== $domainNode = $node->getNode('domain')) {
59+
if (!$this->checkNodeIsConstant($domainNode)) {
60+
return $node;
61+
}
5062
$domain = $domainNode->getAttribute('value');
5163
}
5264

@@ -58,10 +70,8 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
5870

5971
if ('trans' === $name || 'transchoice' === $name) {
6072
$idNode = $node->getNode('node');
61-
if (!$idNode instanceof \Twig_Node_Expression_Constant) {
73+
if (!$this->checkNodeIsConstant($idNode)) {
6274
return $node;
63-
// FIXME: see below
64-
// throw new \RuntimeException(sprintf('Cannot infer translation id from node "%s". Please refactor to only translate constants.', get_class($idNode)));
6575
}
6676
$id = $idNode->getAttribute('value');
6777

@@ -70,10 +80,8 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
7080
$arguments = $node->getNode('arguments');
7181
if ($arguments->hasNode($index)) {
7282
$argument = $arguments->getNode($index);
73-
if (!$argument instanceof \Twig_Node_Expression_Constant) {
83+
if (!$this->checkNodeIsConstant($argument)) {
7484
return $node;
75-
// FIXME: Throw exception if there is some way for the user to turn this off
76-
// on a case-by-case basis, similar to @Ignore in PHP
7785
}
7886

7987
$domain = $argument->getAttribute('value');
@@ -153,4 +161,35 @@ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
153161

154162
public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue) { }
155163
public function visitPhpFile(\SplFileInfo $file, MessageCatalogue $catalogue, array $ast) { }
156-
}
164+
165+
public function setLogger(LoggerInterface $logger)
166+
{
167+
$this->logger = $logger;
168+
}
169+
170+
/**
171+
* @param \Twig_Node $node
172+
* @return bool
173+
*/
174+
private function checkNodeIsConstant(\Twig_Node $node)
175+
{
176+
if ($node instanceof \Twig_Node_Expression_Constant) {
177+
return true;
178+
}
179+
180+
$message = sprintf(
181+
'Cannot infer translation id from node "%s". Please refactor to only translate constants in file "%s" on line %d',
182+
get_class($node),
183+
$this->file,
184+
$node->getLine()
185+
);
186+
187+
if (!$this->logger) {
188+
throw new RuntimeException($message);
189+
}
190+
191+
$this->logger->error($message);
192+
193+
return false;
194+
}
195+
}

0 commit comments

Comments
 (0)