forked from php-translation/extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHPFileExtractor.php
More file actions
60 lines (52 loc) · 1.68 KB
/
PHPFileExtractor.php
File metadata and controls
60 lines (52 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Translation\Extractor\FileExtractor;
use PhpParser\Error;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\ParserFactory;
use Symfony\Component\Finder\SplFileInfo;
use Translation\Extractor\Model\SourceCollection;
use Translation\Extractor\Visitor\Visitor;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class PHPFileExtractor implements FileExtractor
{
/**
* @var Visitor[]|NodeVisitor[]
*/
private array $visitors = [];
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void
{
$path = $file->getRelativePath();
$parser = (new ParserFactory())->createForHostVersion();
$traverser = new NodeTraverser();
$traverser->addVisitor(new NodeVisitor\NameResolver());
foreach ($this->visitors as $v) {
$v->init($collection, $file);
$traverser->addVisitor($v);
}
try {
$tokens = $parser->parse($file->getContents());
$traverser->traverse($tokens);
} catch (Error $e) {
trigger_error(\sprintf('Skipping file "%s" because of parse Error: %s. ', $path, $e->getMessage()));
}
}
public function supportsExtension(string $extension): bool
{
return \in_array($extension, ['php', 'php5', 'phtml']);
}
public function addVisitor(NodeVisitor $visitor): void
{
$this->visitors[] = $visitor;
}
}