Skip to content

Commit

Permalink
Address namespace change in Twig
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Feb 6, 2020
1 parent 0f8dec9 commit 829bba1
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 101 deletions.
6 changes: 4 additions & 2 deletions Tests/Translation/Extractor/File/TwigFileExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Translation\MessageSelector;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Source;

class TwigFileExtractorTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -145,7 +147,7 @@ private function extract($file, TwigFileExtractor $extractor = null)
throw new RuntimeException(sprintf('The file "%s" does not exist.', $file));
}

$env = new \Twig_Environment(new \Twig_Loader_Array(array()));
$env = new Environment(new ArrayLoader(array()));
$env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector())));
$env->addExtension(new TranslationExtension($translator, true));
$env->addExtension(new RoutingExtension(new UrlGenerator(new RouteCollection(), new RequestContext())));
Expand All @@ -168,7 +170,7 @@ class_exists('Symfony\Bridge\Twig\Form\TwigRenderer') ?
$extractor = new TwigFileExtractor($env, new FileSourceFactory('faux'));
}

$ast = $env->parse($env->tokenize(new \Twig_Source(file_get_contents($file), $file)));
$ast = $env->parse($env->tokenize(new Source(file_get_contents($file), $file)));

$catalogue = new MessageCatalogue();
$extractor->visitTwigFile(new \SplFileInfo($file), $catalogue, $ast);
Expand Down
7 changes: 5 additions & 2 deletions Tests/Translation/Extractor/FileExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension;
use JMS\TranslationBundle\Translation\Extractor\FileExtractor;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;

class FileExtractorTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -95,10 +98,10 @@ public function testExtractWithSimpleTestFixtures()

private function extract($directory)
{
$twig = new \Twig_Environment(new \Twig_Loader_Array(array()));
$twig = new Environment(new ArrayLoader(array()));
$twig->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector())));
$twig->addExtension(new TranslationExtension($translator));
$loader=new \Twig_Loader_Filesystem(realpath(__DIR__."/Fixture/SimpleTest/Resources/views/"));
$loader=new FilesystemLoader(realpath(__DIR__."/Fixture/SimpleTest/Resources/views/"));
$twig->setLoader($loader);

$docParser = new DocParser();
Expand Down
6 changes: 4 additions & 2 deletions Tests/Translation/ExtractorManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Psr\Log\NullLogger;
use JMS\TranslationBundle\Translation\Extractor\FileExtractor;
use JMS\TranslationBundle\Translation\ExtractorManager;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

class ExtractorManagerTest extends BaseTestCase
{
Expand Down Expand Up @@ -68,7 +70,7 @@ public function testReset()
$foo = $this->createMock('JMS\TranslationBundle\Translation\ExtractorInterface');
$logger = new NullLogger();

$extractor = new FileExtractor(new \Twig_Environment(new \Twig_Loader_Array(array())), $logger, array());
$extractor = new FileExtractor(new Environment(new ArrayLoader(array())), $logger, array());
$extractor->setExcludedNames(array('foo', 'bar'));
$extractor->setExcludedDirs(array('baz'));

Expand Down Expand Up @@ -111,7 +113,7 @@ private function getManager(FileExtractor $extractor = null, array $extractors =
$logger = new NullLogger();

if (null === $extractor) {
$extractor = new FileExtractor(new \Twig_Environment(new \Twig_Loader_Array(array())), $logger, array());
$extractor = new FileExtractor(new Environment(new ArrayLoader(array())), $logger, array());
}

return new ExtractorManager($extractor, $logger, $extractors);
Expand Down
7 changes: 5 additions & 2 deletions Tests/Twig/BaseTwigTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension;
use JMS\TranslationBundle\Twig\TranslationExtension;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Source;

abstract class BaseTwigTestCase extends \PHPUnit_Framework_TestCase
{
final protected function parse($file, $debug = false)
{
$content = file_get_contents(__DIR__.'/Fixture/'.$file);

$env = new \Twig_Environment(new \Twig_Loader_Array(array()));
$env = new Environment(new ArrayLoader(array()));
$env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector())));
$env->addExtension(new TranslationExtension($translator, $debug));

return $env->compile($env->parse($env->tokenize(new \Twig_Source($content, 'whatever')))->getNode('body'));
return $env->compile($env->parse($env->tokenize(new Source($content, 'whatever')))->getNode('body'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use Psr\Log\LoggerInterface;
use Twig\Node\Node as TwigNode;

class AuthenticationMessagesExtractor implements LoggerAwareInterface, FileVisitorInterface, NodeVisitor
{
Expand Down Expand Up @@ -266,9 +267,9 @@ public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue)
/**
* @param \SplFileInfo $file
* @param MessageCatalogue $catalogue
* @param \Twig_Node $ast
* @param TwigNode $ast
*/
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $ast)
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, TwigNode $ast)
{
}
}
4 changes: 2 additions & 2 deletions Translation/Extractor/File/DefaultPhpFileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PhpParser\NodeVisitor;
use PhpParser\Node\Scalar\String_;
use Psr\Log\LoggerInterface;
use Twig\Node\Node as TwigNode;

/**
* This parser can extract translation information from PHP files.
Expand Down Expand Up @@ -236,9 +237,8 @@ public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue)
/**
* @param \SplFileInfo $file
* @param MessageCatalogue $catalogue
* @param \Twig_Node $ast
*/
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $ast)
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, TwigNode $ast)
{
}

Expand Down
5 changes: 3 additions & 2 deletions Translation/Extractor/File/FormExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PhpParser\NodeVisitor;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Kernel;
use Twig\Node\Node as TwigNode;

class FormExtractor implements FileVisitorInterface, LoggerAwareInterface, NodeVisitor
{
Expand Down Expand Up @@ -491,9 +492,9 @@ public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue)
/**
* @param \SplFileInfo $file
* @param MessageCatalogue $catalogue
* @param \Twig_Node $ast
* @param TwigNode $ast
*/
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $ast)
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, TwigNode $ast)
{
}

Expand Down
4 changes: 2 additions & 2 deletions Translation/Extractor/File/TranslationContainerExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use Twig\Node\Node as TwigNode;

/**
* Extracts translations from designated translation containers.
Expand Down Expand Up @@ -174,9 +175,8 @@ public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue)
/**
* @param \SplFileInfo $file
* @param MessageCatalogue $catalogue
* @param \Twig_Node $ast
*/
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $ast)
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, TwigNode $ast)
{
}
}
47 changes: 23 additions & 24 deletions Translation/Extractor/File/TwigFileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Model\MessageCatalogue;
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;

class TwigFileExtractor extends \Twig_BaseNodeVisitor implements FileVisitorInterface
use Twig\Environment;
use Twig\NodeTraverser;
use Twig\NodeVisitor\AbstractNodeVisitor;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Node;

class TwigFileExtractor extends AbstractNodeVisitor implements FileVisitorInterface
{
/**
* @var FileSourceFactory
Expand All @@ -43,7 +49,7 @@ class TwigFileExtractor extends \Twig_BaseNodeVisitor implements FileVisitorInte
private $catalogue;

/**
* @var \Twig_NodeTraverser
* @var NodeTraverser
*/
private $traverser;

Expand All @@ -54,21 +60,19 @@ class TwigFileExtractor extends \Twig_BaseNodeVisitor implements FileVisitorInte

/**
* TwigFileExtractor constructor.
* @param \Twig_Environment $env
* @param Environment $env
* @param FileSourceFactory $fileSourceFactory
*/
public function __construct(\Twig_Environment $env, FileSourceFactory $fileSourceFactory)
public function __construct(Environment $env, FileSourceFactory $fileSourceFactory)
{
$this->fileSourceFactory = $fileSourceFactory;
$this->traverser = new \Twig_NodeTraverser($env, array($this));
$this->traverser = new NodeTraverser($env, array($this));
}

/**
* @param \Twig_Node $node
* @param \Twig_Environment $env
* @return \Twig_Node
* @return Node
*/
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
protected function doEnterNode(Node $node, Environment $env)
{
$this->stack[] = $node;

Expand All @@ -83,12 +87,12 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
$message = new Message($id, $domain);
$message->addSource($this->fileSourceFactory->create($this->file, $node->getTemplateLine()));
$this->catalogue->add($message);
} elseif ($node instanceof \Twig_Node_Expression_Filter) {
} elseif ($node instanceof FilterExpression) {
$name = $node->getNode('filter')->getAttribute('value');

if ('trans' === $name || 'transchoice' === $name) {
$idNode = $node->getNode('node');
if (!$idNode instanceof \Twig_Node_Expression_Constant) {
if (!$idNode instanceof ConstantExpression) {
return $node;
// FIXME: see below
// throw new \RuntimeException(sprintf('Cannot infer translation id from node "%s". Please refactor to only translate constants.', get_class($idNode)));
Expand All @@ -100,7 +104,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
$arguments = $node->getNode('arguments');
if ($arguments->hasNode($index)) {
$argument = $arguments->getNode($index);
if (!$argument instanceof \Twig_Node_Expression_Constant) {
if (!$argument instanceof ConstantExpression) {
return $node;
// FIXME: Throw exception if there is some way for the user to turn this off
// on a case-by-case basis, similar to @Ignore in PHP
Expand All @@ -113,7 +117,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
$message->addSource($this->fileSourceFactory->create($this->file, $node->getTemplateLine()));

for ($i=count($this->stack)-2; $i>=0; $i-=1) {
if (!$this->stack[$i] instanceof \Twig_Node_Expression_Filter) {
if (!$this->stack[$i] instanceof FilterExpression) {
break;
}

Expand All @@ -125,7 +129,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
}

$text = $arguments->getNode(0);
if (!$text instanceof \Twig_Node_Expression_Constant) {
if (!$text instanceof ConstantExpression) {
throw new RuntimeException(sprintf('The first argument of the "%s" filter must be a constant expression, such as a string.', $name));
}

Expand Down Expand Up @@ -153,9 +157,8 @@ public function getPriority()
/**
* @param \SplFileInfo $file
* @param MessageCatalogue $catalogue
* @param \Twig_Node $ast
*/
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $ast)
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, Node $ast)
{
$this->file = $file;
$this->catalogue = $catalogue;
Expand All @@ -167,10 +170,8 @@ public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \
* If the current Twig Node has embedded templates, we want to travese these templates
* in the same manner as we do the main twig template to ensure all translations are
* caught.
*
* @param \Twig_Node $node
*/
private function traverseEmbeddedTemplates(\Twig_Node $node)
private function traverseEmbeddedTemplates(Node $node)
{
$templates = $node->getAttribute('embedded_templates');

Expand All @@ -183,11 +184,9 @@ private function traverseEmbeddedTemplates(\Twig_Node $node)
}

/**
* @param \Twig_Node $node
* @param \Twig_Environment $env
* @return \Twig_Node
* @return Node
*/
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
protected function doLeaveNode(Node $node, Environment $env)
{
array_pop($this->stack);

Expand Down
4 changes: 2 additions & 2 deletions Translation/Extractor/File/ValidationExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Model\MessageCatalogue;
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
use Twig\Node\Node as TwigNode;

/**
* Extracts translations validation constraints.
Expand Down Expand Up @@ -164,9 +165,8 @@ public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue)
/**
* @param \SplFileInfo $file
* @param MessageCatalogue $catalogue
* @param \Twig_Node $ast
*/
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $ast)
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, TwigNode $ast)
{
}

Expand Down
18 changes: 11 additions & 7 deletions Translation/Extractor/FileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
use JMS\TranslationBundle\Translation\ExtractorInterface;
use JMS\TranslationBundle\Model\MessageCatalogue;
use Symfony\Component\Finder\Finder;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\NodeVisitor\NodeVisitorInterface;
use Twig\Source;

/**
* File-based extractor.
Expand All @@ -39,7 +43,7 @@
class FileExtractor implements ExtractorInterface, LoggerAwareInterface
{
/**
* @var \Twig_Environment
* @var Environment
*/
private $twig;

Expand All @@ -64,12 +68,12 @@ class FileExtractor implements ExtractorInterface, LoggerAwareInterface
private $directory;

/**
* @var RemovingNodeVisitor|\Twig_NodeVisitorInterface
* @var RemovingNodeVisitor|NodeVisitorInterface
*/
private $removingTwigVisitor;

/**
* @var DefaultApplyingNodeVisitor|RemovingNodeVisitor|\Twig_NodeVisitorInterface
* @var DefaultApplyingNodeVisitor|RemovingNodeVisitor|NodeVisitorInterface
*/
private $defaultApplyingTwigVisitor;

Expand All @@ -90,11 +94,11 @@ class FileExtractor implements ExtractorInterface, LoggerAwareInterface

/**
* FileExtractor constructor.
* @param \Twig_Environment $twig
* @param Environment $twig
* @param LoggerInterface $logger
* @param array $visitors
*/
public function __construct(\Twig_Environment $twig, LoggerInterface $logger, array $visitors)
public function __construct(Environment $twig, LoggerInterface $logger, array $visitors)
{
$this->twig = $twig;
$this->visitors = $visitors;
Expand Down Expand Up @@ -203,7 +207,7 @@ public function extract()
}

$curTwigLoader = $this->twig->getLoader();
$this->twig->setLoader(new \Twig_Loader_Array(array()));
$this->twig->setLoader(new ArrayLoader(array()));

try {
$catalogue = new MessageCatalogue();
Expand All @@ -227,7 +231,7 @@ public function extract()
$visitingArgs[] = $ast;
} elseif ('twig' === $extension) {
$visitingMethod = 'visitTwigFile';
$visitingArgs[] = $this->twig->parse($this->twig->tokenize(new \Twig_Source(file_get_contents($file), (string) $file)));
$visitingArgs[] = $this->twig->parse($this->twig->tokenize(new Source(file_get_contents($file), (string) $file)));
}
}

Expand Down
Loading

0 comments on commit 829bba1

Please sign in to comment.