Skip to content

Commit d80563b

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

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

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

+44-10
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,24 @@
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 Symfony\Bridge\Twig\Node\TransNode;
28+
use Symfony\Component\HttpKernel\Log\LoggerInterface;
2829

29-
class TwigFileExtractor implements FileVisitorInterface, \Twig_NodeVisitorInterface
30+
class TwigFileExtractor implements FileVisitorInterface, \Twig_NodeVisitorInterface, LoggerAwareInterface
3031
{
32+
/** @var \SplFileInfo */
3133
private $file;
3234
private $catalogue;
3335
private $traverser;
3436
private $stack = array();
3537
private $stackCount = 0;
38+
/** @var LoggerInterface */
39+
private $logger;
3640

3741
public function __construct(\Twig_Environment $env)
3842
{
@@ -47,6 +51,9 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
4751
$id = $node->getNode('body')->getAttribute('data');
4852
$domain = 'messages';
4953
if (null !== $domainNode = $node->getNode('domain')) {
54+
if (!$this->checkNodeIsConstant($domainNode)) {
55+
return $node;
56+
}
5057
$domain = $domainNode->getAttribute('value');
5158
}
5259

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

5966
if ('trans' === $name || 'transchoice' === $name) {
6067
$idNode = $node->getNode('node');
61-
if (!$idNode instanceof \Twig_Node_Expression_Constant) {
68+
if (!$this->checkNodeIsConstant($idNode)) {
6269
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)));
6570
}
6671
$id = $idNode->getAttribute('value');
6772

@@ -70,10 +75,8 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
7075
$arguments = $node->getNode('arguments');
7176
if ($arguments->hasNode($index)) {
7277
$argument = $arguments->getNode($index);
73-
if (!$argument instanceof \Twig_Node_Expression_Constant) {
78+
if (!$this->checkNodeIsConstant($argument)) {
7479
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
7780
}
7881

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

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

0 commit comments

Comments
 (0)