1 | <?php |
||
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 |
||
84 | } |
||
85 |