Skip to content
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
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## UNRELEASED

### Added

- Support for Twig2.0

## 1.0.0

## Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"php": "^5.5 || ^7.0",
"nikic/php-parser": "^3.0",
"symfony/finder": "^2.7 || ^3.0",
"twig/twig": "^1.21"
"twig/twig": "^1.27 || ^2.0"
},
"require-dev": {
"phpunit/phpunit": "^4.5 || ^5.4",
Expand Down
12 changes: 9 additions & 3 deletions src/FileExtractor/TwigFileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ final class TwigFileExtractor implements FileExtractor
/**
* @param \Twig_Environment $twig
*/
public function __construct(\Twig_Environment $twig = null)
public function __construct(\Twig_Environment $twig)
{
$this->twig = $twig ?: new \Twig_Environment();
$this->twig = $twig;
}

public function getSourceLocations(SplFileInfo $file, SourceCollection $collection)
Expand All @@ -46,7 +46,13 @@ public function getSourceLocations(SplFileInfo $file, SourceCollection $collecti

$path = $file->getRelativePath();

$tokens = $this->twig->parse($this->twig->tokenize($file->getContents(), $path));
if (class_exists('Twig_Source')) {
// Twig 2.0
$stream = $this->twig->tokenize(new \Twig_Source($file->getContents(), $file->getRelativePathname(), $path));
} else {
$stream = $this->twig->tokenize($file->getContents(), $path);
}
$tokens = $this->twig->parse($stream);
$traverser = new \Twig_NodeTraverser($this->twig, $this->visitors);
$traverser->traverse($tokens);
}
Expand Down
27 changes: 13 additions & 14 deletions src/Visitor/Twig/TranslationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Translation\Extractor\Visitor\Twig;

use Symfony\Bridge\Twig\Node\TransNode;
use Translation\Extractor\Model\SourceLocation;
use Translation\Extractor\Visitor\BaseVisitor;
use Twig_Environment;
use Twig_NodeInterface;
Expand All @@ -22,20 +20,21 @@
*/
final class TranslationBlock extends BaseVisitor implements \Twig_NodeVisitorInterface
{
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
{
if ($node instanceof TransNode) {
$id = $node->getNode('body')->getAttribute('data');
$domain = 'messages';
if ($node->hasNode('domain')) {
$domain = $node->getNode('domain')->getAttribute('value');
}
/**
* @var WorkerTranslationBlock
*/
private $worker;

$source = new SourceLocation($id, $this->getAbsoluteFilePath(), $node->getLine(), ['domain' => $domain]);
$this->collection->addLocation($source);
}
public function __construct()
{
$this->worker = new WorkerTranslationBlock();
}

return $node;
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
{
return $this->worker->work($node, $this->collection, function () {
return $this->getAbsoluteFilePath();
});
}

public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
Expand Down
46 changes: 13 additions & 33 deletions src/Visitor/Twig/TranslationFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Translation\Extractor\Visitor\Twig;

use Translation\Extractor\Model\SourceLocation;
use Translation\Extractor\Visitor\BaseVisitor;
use Twig_Environment;
use Twig_NodeInterface;
Expand All @@ -21,40 +20,21 @@
*/
final class TranslationFilter extends BaseVisitor implements \Twig_NodeVisitorInterface
{
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
{
if (!$node instanceof \Twig_Node_Expression_Filter) {
return $node;
}

$name = $node->getNode('filter')->getAttribute('value');
if ('trans' !== $name && 'transchoice' !== $name) {
return $node;
}

$idNode = $node->getNode('node');
if (!$idNode instanceof \Twig_Node_Expression_Constant) {
// We can only extract constants
return $node;
}

$id = $idNode->getAttribute('value');
$index = 'trans' === $name ? 1 : 2;
$domain = 'messages';
$arguments = $node->getNode('arguments');
if ($arguments->hasNode($index)) {
$argument = $arguments->getNode($index);
if (!$argument instanceof \Twig_Node_Expression_Constant) {
return $node;
}

$domain = $argument->getAttribute('value');
}
/**
* @var WorkerTranslationFilter
*/
private $worker;

$source = new SourceLocation($id, $this->getAbsoluteFilePath(), $node->getLine(), ['domain' => $domain]);
$this->collection->addLocation($source);
public function __construct()
{
$this->worker = new WorkerTranslationFilter();
}

return $node;
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
{
return $this->worker->work($node, $this->collection, function () {
return $this->getAbsoluteFilePath();
});
}

public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
Expand Down
49 changes: 49 additions & 0 deletions src/Visitor/Twig/Twig2TranslationBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Extractor\Visitor\Twig;

use Translation\Extractor\Visitor\BaseVisitor;
use Twig_Environment;
use Twig_Node;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class Twig2TranslationBlock extends BaseVisitor implements \Twig_NodeVisitorInterface
{
/**
* @var WorkerTranslationBlock
*/
private $worker;

public function __construct()
{
$this->worker = new WorkerTranslationBlock();
}

public function enterNode(Twig_Node $node, Twig_Environment $env)
{
return $this->worker->work($node, $this->collection, function () {
return $this->getAbsoluteFilePath();
});
}

public function leaveNode(Twig_Node $node, Twig_Environment $env)
{
return $node;
}

public function getPriority()
{
return 0;
}
}
49 changes: 49 additions & 0 deletions src/Visitor/Twig/Twig2TranslationFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Extractor\Visitor\Twig;

use Translation\Extractor\Visitor\BaseVisitor;
use Twig_Environment;
use Twig_Node;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class Twig2TranslationFilter extends BaseVisitor implements \Twig_NodeVisitorInterface
{
/**
* @var WorkerTranslationFilter
*/
private $worker;

public function __construct()
{
$this->worker = new WorkerTranslationFilter();
}

public function enterNode(Twig_Node $node, Twig_Environment $env)
{
return $this->worker->work($node, $this->collection, function () {
return $this->getAbsoluteFilePath();
});
}

public function leaveNode(Twig_Node $node, Twig_Environment $env)
{
return $node;
}

public function getPriority()
{
return 0;
}
}
47 changes: 47 additions & 0 deletions src/Visitor/Twig/WorkerTranslationBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Extractor\Visitor\Twig;

use Symfony\Bridge\Twig\Node\TransNode;
use Translation\Extractor\Model\SourceCollection;
use Translation\Extractor\Model\SourceLocation;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class WorkerTranslationBlock
{
/**
* @param \Twig_Node|\Twig_NodeInterface $node
* @param SourceCollection $collection
* @param callable $getAbsoluteFilePath
*
* @return \Twig_Node|\Twig_NodeInterface
*/
public function work($node, SourceCollection $collection, callable $getAbsoluteFilePath)
{
if ($node instanceof TransNode) {
$id = $node->getNode('body')->getAttribute('data');
$domain = 'messages';
if ($node->hasNode('domain')) {
if (null !== $domainNode = $node->getNode('domain')) {
$domain = $domainNode->getAttribute('value');
}
}

$source = new SourceLocation($id, $getAbsoluteFilePath(), $node->getTemplateLine(), ['domain' => $domain]);
$collection->addLocation($source);
}

return $node;
}
}
64 changes: 64 additions & 0 deletions src/Visitor/Twig/WorkerTranslationFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Extractor\Visitor\Twig;

use Translation\Extractor\Model\SourceCollection;
use Translation\Extractor\Model\SourceLocation;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class WorkerTranslationFilter
{
/**
* @param \Twig_Node|\Twig_NodeInterface $node
* @param SourceCollection $collection
* @param callable $getAbsoluteFilePath
*
* @return \Twig_Node|\Twig_NodeInterface
*/
public function work($node, SourceCollection $collection, callable $getAbsoluteFilePath)
{
if (!$node instanceof \Twig_Node_Expression_Filter) {
return $node;
}

$name = $node->getNode('filter')->getAttribute('value');
if ('trans' !== $name && 'transchoice' !== $name) {
return $node;
}

$idNode = $node->getNode('node');
if (!$idNode instanceof \Twig_Node_Expression_Constant) {
// We can only extract constants
return $node;
}

$id = $idNode->getAttribute('value');
$index = 'trans' === $name ? 1 : 2;
$domain = 'messages';
$arguments = $node->getNode('arguments');
if ($arguments->hasNode($index)) {
$argument = $arguments->getNode($index);
if (!$argument instanceof \Twig_Node_Expression_Constant) {
return $node;
}

$domain = $argument->getAttribute('value');
}

$source = new SourceLocation($id, $getAbsoluteFilePath(), $node->getTemplateLine(), ['domain' => $domain]);
$collection->addLocation($source);

return $node;
}
}
Loading