Skip to content

Commit 0fdb965

Browse files
authored
Prepare 1.1 (#37)
* Removed some code that was not associated with a release * mnor * Added factor for twig * cs * Bugfix * removed debug code
1 parent a423a52 commit 0fdb965

14 files changed

+128
-305
lines changed

Changelog.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
44

55
## UNRELEASED
66

7+
## 1.1.0
8+
79
### Added
810

9-
- Support for Twig2.0
11+
- Support for Twig 2.0.
12+
- Support for reporting errors and silence errors with `@Ignore`.
13+
14+
### Deprecated
15+
16+
- `Twig\TranslationBlock` and `Twig\TranslationFilter`. Use `Twig\Twig1Visitor` instead.
1017

1118
## 1.0.0
1219

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"extra": {
3838
"branch-alias": {
39-
"dev-master": "1.1-dev"
39+
"dev-master": "1.2-dev"
4040
}
4141
}
4242
}

src/Visitor/Twig/TranslationBlock.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,24 @@
1818
/**
1919
* @author Tobias Nyholm <[email protected]>
2020
*
21-
* @deprecated Use Twig1Visitor. Will be removed in 2.0.
21+
* @deprecated Use Twig1Visitor. Will be removed in 2.0
2222
*/
2323
final class TranslationBlock extends BaseVisitor implements \Twig_NodeVisitorInterface
2424
{
25-
/**
26-
* @var WorkerTranslationBlock
27-
*/
28-
private $worker;
29-
30-
public function __construct()
31-
{
32-
$this->worker = new WorkerTranslationBlock();
33-
}
34-
3525
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
3626
{
37-
return $this->worker->work($node, $this->collection, function () {
38-
return $this->getAbsoluteFilePath();
39-
});
27+
if ($node instanceof TransNode) {
28+
$id = $node->getNode('body')->getAttribute('data');
29+
$domain = 'messages';
30+
if ($node->hasNode('domain')) {
31+
$domain = $node->getNode('domain')->getAttribute('value');
32+
}
33+
34+
$source = new SourceLocation($id, $this->getAbsoluteFilePath(), $node->getLine(), ['domain' => $domain]);
35+
$this->collection->addLocation($source);
36+
}
37+
38+
return $node;
4039
}
4140

4241
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)

src/Visitor/Twig/TranslationFilter.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,40 @@
2222
*/
2323
final class TranslationFilter extends BaseVisitor implements \Twig_NodeVisitorInterface
2424
{
25-
/**
26-
* @var WorkerTranslationFilter
27-
*/
28-
private $worker;
29-
30-
public function __construct()
31-
{
32-
$this->worker = new WorkerTranslationFilter();
33-
}
34-
3525
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
3626
{
37-
return $this->worker->work($node, $this->collection, function () {
38-
return $this->getAbsoluteFilePath();
39-
});
27+
if (!$node instanceof \Twig_Node_Expression_Filter) {
28+
return $node;
29+
}
30+
31+
$name = $node->getNode('filter')->getAttribute('value');
32+
if ('trans' !== $name && 'transchoice' !== $name) {
33+
return $node;
34+
}
35+
36+
$idNode = $node->getNode('node');
37+
if (!$idNode instanceof \Twig_Node_Expression_Constant) {
38+
// We can only extract constants
39+
return $node;
40+
}
41+
42+
$id = $idNode->getAttribute('value');
43+
$index = 'trans' === $name ? 1 : 2;
44+
$domain = 'messages';
45+
$arguments = $node->getNode('arguments');
46+
if ($arguments->hasNode($index)) {
47+
$argument = $arguments->getNode($index);
48+
if (!$argument instanceof \Twig_Node_Expression_Constant) {
49+
return $node;
50+
}
51+
52+
$domain = $argument->getAttribute('value');
53+
}
54+
55+
$source = new SourceLocation($id, $this->getAbsoluteFilePath(), $node->getLine(), ['domain' => $domain]);
56+
$this->collection->addLocation($source);
57+
58+
return $node;
4059
}
4160

4261
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)

src/Visitor/Twig/Twig1Visitor.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,17 @@
1111

1212
namespace Translation\Extractor\Visitor\Twig;
1313

14-
use Translation\Extractor\Visitor\BaseVisitor;
1514
use Twig_Environment;
1615
use Twig_NodeInterface;
1716

1817
/**
1918
* @author Tobias Nyholm <[email protected]>
2019
*/
21-
final class Twig1Visitor extends BaseVisitor implements \Twig_NodeVisitorInterface
20+
final class Twig1Visitor extends TwigVisitor implements \Twig_NodeVisitorInterface
2221
{
23-
/**
24-
* @var WorkerTranslationFilter
25-
*/
26-
private $worker;
27-
28-
public function __construct()
29-
{
30-
$this->worker = new Worker();
31-
}
32-
3322
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
3423
{
35-
return $this->worker->work($node, $this->collection, function () {
36-
return $this->getAbsoluteFilePath();
37-
});
24+
return $this->doEnterNode($node);
3825
}
3926

4027
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)

src/Visitor/Twig/Twig2TranslationBlock.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/Visitor/Twig/Twig2TranslationFilter.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/Visitor/Twig/Twig2Visitor.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,17 @@
1111

1212
namespace Translation\Extractor\Visitor\Twig;
1313

14-
use Translation\Extractor\Visitor\BaseVisitor;
1514
use Twig_Environment;
1615
use Twig_Node;
1716

1817
/**
1918
* @author Tobias Nyholm <[email protected]>
2019
*/
21-
final class Twig2Visitor extends BaseVisitor implements \Twig_NodeVisitorInterface
20+
final class Twig2Visitor extends TwigVisitor implements \Twig_NodeVisitorInterface
2221
{
23-
/**
24-
* @var WorkerTranslationFilter
25-
*/
26-
private $worker;
27-
28-
public function __construct()
29-
{
30-
$this->worker = new Worker();
31-
}
32-
3322
public function enterNode(Twig_Node $node, Twig_Environment $env)
3423
{
35-
return $this->worker->work($node, $this->collection, function () {
36-
return $this->getAbsoluteFilePath();
37-
});
24+
return $this->doEnterNode($node);
3825
}
3926

4027
public function leaveNode(Twig_Node $node, Twig_Environment $env)

src/Visitor/Twig/TwigVisitor.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Extractor\Visitor\Twig;
13+
14+
use Translation\Extractor\Visitor\BaseVisitor;
15+
16+
/**
17+
* Factory class and base class for TwigVisitor.
18+
*
19+
* @author Tobias Nyholm <[email protected]>
20+
*/
21+
abstract class TwigVisitor extends BaseVisitor
22+
{
23+
/**
24+
* @var Worker
25+
*/
26+
private $worker;
27+
28+
/**
29+
* @param Worker|null $worker
30+
*/
31+
public function __construct(Worker $worker = null)
32+
{
33+
if (null === $worker) {
34+
$worker = new Worker();
35+
}
36+
37+
$this->worker = $worker;
38+
}
39+
40+
/**
41+
* @return Twig1Visitor|Twig2Visitor
42+
*/
43+
public static function create()
44+
{
45+
if (\Twig_Environment::MAJOR_VERSION === 1) {
46+
return new Twig1Visitor();
47+
}
48+
49+
return new Twig2Visitor();
50+
}
51+
52+
/**
53+
* @param \Twig_Node|\Twig_NodeInterface $node
54+
*
55+
* @return \Twig_Node|\Twig_NodeInterface
56+
*/
57+
protected function doEnterNode($node)
58+
{
59+
return $this->worker->work($node, $this->collection, function () {
60+
return $this->getAbsoluteFilePath();
61+
});
62+
}
63+
}

0 commit comments

Comments
 (0)