|
| 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 Symfony\Bridge\Twig\Node\TransNode; |
| 15 | +use Translation\Extractor\Model\SourceCollection; |
| 16 | +use Translation\Extractor\Model\SourceLocation; |
| 17 | + |
| 18 | +/** |
| 19 | + * The Worker tha actually extract the translations. |
| 20 | + * |
| 21 | + * @author Tobias Nyholm <[email protected]> |
| 22 | + * @author Fabien Potencier <[email protected]> |
| 23 | + */ |
| 24 | +final class Worker |
| 25 | +{ |
| 26 | + const UNDEFINED_DOMAIN = 'messages'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param \Twig_Node|\Twig_NodeInterface $node |
| 30 | + * @param SourceCollection $collection |
| 31 | + * @param callable $getAbsoluteFilePath |
| 32 | + * |
| 33 | + * @return \Twig_Node|\Twig_NodeInterface |
| 34 | + */ |
| 35 | + public function work($node, SourceCollection $collection, callable $getAbsoluteFilePath) |
| 36 | + { |
| 37 | + if ( |
| 38 | + $node instanceof \Twig_Node_Expression_Filter && |
| 39 | + 'trans' === $node->getNode('filter')->getAttribute('value') && |
| 40 | + $node->getNode('node') instanceof \Twig_Node_Expression_Constant |
| 41 | + ) { |
| 42 | + // extract constant nodes with a trans filter |
| 43 | + $collection->addLocation(new SourceLocation( |
| 44 | + $node->getNode('node')->getAttribute('value'), |
| 45 | + $getAbsoluteFilePath(), |
| 46 | + $node->getTemplateLine(), |
| 47 | + ['domain' => $this->getReadDomainFromArguments($node->getNode('arguments'), 1)] |
| 48 | + )); |
| 49 | + } elseif ( |
| 50 | + $node instanceof \Twig_Node_Expression_Filter && |
| 51 | + 'transchoice' === $node->getNode('filter')->getAttribute('value') && |
| 52 | + $node->getNode('node') instanceof \Twig_Node_Expression_Constant |
| 53 | + ) { |
| 54 | + // extract constant nodes with a trans filter |
| 55 | + $collection->addLocation(new SourceLocation( |
| 56 | + $node->getNode('node')->getAttribute('value'), |
| 57 | + $getAbsoluteFilePath(), |
| 58 | + $node->getTemplateLine(), |
| 59 | + ['domain' => $this->getReadDomainFromArguments($node->getNode('arguments'), 2)] |
| 60 | + )); |
| 61 | + } elseif ($node instanceof TransNode) { |
| 62 | + // extract trans nodes |
| 63 | + $collection->addLocation(new SourceLocation( |
| 64 | + $node->getNode('body')->getAttribute('data'), |
| 65 | + $getAbsoluteFilePath(), |
| 66 | + $node->getTemplateLine(), |
| 67 | + ['domain' => $node->hasNode('domain') ? $this->getReadDomainFromNode($node->getNode('domain')) : self::UNDEFINED_DOMAIN] |
| 68 | + )); |
| 69 | + } |
| 70 | + |
| 71 | + return $node; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param \Twig_Node $arguments |
| 76 | + * @param int $index |
| 77 | + * |
| 78 | + * @return string|null |
| 79 | + */ |
| 80 | + private function getReadDomainFromArguments(\Twig_Node $arguments, $index) |
| 81 | + { |
| 82 | + if ($arguments->hasNode('domain')) { |
| 83 | + $argument = $arguments->getNode('domain'); |
| 84 | + } elseif ($arguments->hasNode($index)) { |
| 85 | + $argument = $arguments->getNode($index); |
| 86 | + } else { |
| 87 | + return self::UNDEFINED_DOMAIN; |
| 88 | + } |
| 89 | + |
| 90 | + return $this->getReadDomainFromNode($argument); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param \Twig_Node $node |
| 95 | + * |
| 96 | + * @return string|null |
| 97 | + */ |
| 98 | + private function getReadDomainFromNode(\Twig_Node $node) |
| 99 | + { |
| 100 | + if ($node instanceof \Twig_Node_Expression_Constant) { |
| 101 | + return $node->getAttribute('value'); |
| 102 | + } |
| 103 | + |
| 104 | + return self::UNDEFINED_DOMAIN; |
| 105 | + } |
| 106 | +} |
0 commit comments