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
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Tobias Nyholm <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class TwigFileExtractor implements FileExtractor |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Visitor[]|\Twig_NodeVisitorInterface[] |
25
|
|
|
*/ |
26
|
|
|
private $visitors = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Twig_Environment |
30
|
|
|
*/ |
31
|
|
|
private $twig; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \Twig_Environment $twig |
35
|
|
|
*/ |
36
|
4 |
|
public function __construct(\Twig_Environment $twig = null) |
37
|
|
|
{ |
38
|
4 |
|
$this->twig = $twig ?: new \Twig_Environment(); |
39
|
4 |
|
} |
40
|
|
|
|
41
|
4 |
|
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection) |
42
|
|
|
{ |
43
|
4 |
|
foreach ($this->visitors as $v) { |
44
|
4 |
|
$v->init($collection, $file); |
|
|
|
|
45
|
4 |
|
} |
46
|
|
|
|
47
|
4 |
|
$path = $file->getRelativePath(); |
48
|
|
|
|
49
|
4 |
|
$tokens = $this->twig->parse($this->twig->tokenize($file->getContents(), $path)); |
50
|
4 |
|
$traverser = new \Twig_NodeTraverser($this->twig, $this->visitors); |
|
|
|
|
51
|
4 |
|
$traverser->traverse($tokens); |
52
|
4 |
|
} |
53
|
|
|
|
54
|
1 |
|
public function getType() |
55
|
|
|
{ |
56
|
1 |
|
return 'twig'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \Twig_NodeVisitorInterface $visitor |
61
|
|
|
*/ |
62
|
4 |
|
public function addVisitor(\Twig_NodeVisitorInterface $visitor) |
63
|
|
|
{ |
64
|
4 |
|
$this->visitors[] = $visitor; |
65
|
4 |
|
} |
66
|
|
|
} |
67
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: