Completed
Push — master ( 3231e7...cf6a99 )
by Tobias
01:28
created

TwigFileExtractor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 60
c 0
b 0
f 0
ccs 19
cts 21
cp 0.9048
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A __construct() 0 5 1
A getSourceLocations() 0 13 3
A addVisitor() 0 4 1
A getNodeVisitors() 0 4 1
A supportsExtension() 0 4 1
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\FileExtractor;
13
14
use Symfony\Component\Finder\SplFileInfo;
15
use Translation\Extractor\Model\SourceCollection;
16
use Translation\Extractor\Visitor\Visitor;
17
use Twig\Environment;
18
use Twig\Extension\AbstractExtension;
19
use Twig\NodeVisitor\NodeVisitorInterface;
20
use Twig\Source;
21
22
/**
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
final class TwigFileExtractor extends AbstractExtension implements FileExtractor
26
{
27
    /**
28
     * @var NodeVisitorInterface[]
29
     */
30
    private $visitors = [];
31
    private $twig;
32
33 5
    public function __construct(Environment $twig)
34
    {
35 5
        $this->twig = $twig;
36 5
        $twig->addExtension($this);
37 5
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 5
    public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void
43
    {
44 5
        foreach ($this->visitors as $v) {
45 5
            if ($v instanceof Visitor) {
46 5
                $v->init($collection, $file);
47
            }
48
        }
49
50 5
        $path = $file->getRelativePath();
51
52 5
        $stream = $this->twig->tokenize(new Source($file->getContents(), $file->getRelativePathname(), $path));
53 5
        $this->twig->parse($stream);
54 5
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 1
    public function supportsExtension(string $extension): bool
60
    {
61 1
        return 'twig' === $extension;
62
    }
63
64 5
    public function addVisitor(NodeVisitorInterface $visitor): void
65
    {
66 5
        $this->visitors[] = $visitor;
67 5
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 5
    public function getNodeVisitors(): array
73
    {
74 5
        return $this->visitors;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getName(): string
81
    {
82
        return 'php.translation';
83
    }
84
}
85