1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Coffreo project "coffreo/php-translation-js-extractor-bundle" |
5
|
|
|
* |
6
|
|
|
* (c) Coffreo SAS <[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 Coffreo\PHPTranslationJsExtractorBundle\Extractor; |
13
|
|
|
|
14
|
|
|
use Coffreo\JsTranslationExtractor\Extractor\JsTranslationExtractor; |
15
|
|
|
use Coffreo\JsTranslationExtractor\Extractor\JsTranslationExtractorInterface; |
16
|
|
|
use Coffreo\JsTranslationExtractor\Model\TranslationCollection; |
17
|
|
|
use Coffreo\JsTranslationExtractor\Model\TranslationString; |
18
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
19
|
|
|
use Translation\Extractor\FileExtractor\FileExtractor; |
20
|
|
|
use Translation\Extractor\Model\SourceCollection; |
21
|
|
|
use Translation\Extractor\Model\SourceLocation; |
22
|
|
|
|
23
|
|
|
final class JsFileExtractor implements FileExtractor |
24
|
|
|
{ |
25
|
|
|
/** @var JsTranslationExtractorInterface */ |
26
|
|
|
private $extractor; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $type; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* JsFileExtractor constructor. |
33
|
|
|
* |
34
|
|
|
* @param string $type the type of file handled by this extractor |
35
|
|
|
* @param JsTranslationExtractorInterface|null $extractor extractor instance to use |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct($type = 'js', JsTranslationExtractorInterface $extractor = null) |
38
|
|
|
{ |
39
|
1 |
|
$this->type = $type; |
40
|
1 |
|
$this->extractor = $extractor ?: new JsTranslationExtractor(); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
1 |
|
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void |
44
|
|
|
{ |
45
|
1 |
|
$realPath = $file->getRealPath(); |
46
|
1 |
|
$translations = $this->findTranslations($file); |
47
|
|
|
|
48
|
1 |
|
foreach ($translations as $translation) { |
49
|
|
|
/* @var $translation TranslationString */ |
50
|
1 |
|
$collection->addLocation(new SourceLocation($translation->getMessage(), $realPath, $translation->getLine(), $translation->getContext())); |
51
|
|
|
} |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
public function supportsExtension(string $extension): bool |
55
|
|
|
{ |
56
|
|
|
return $this->type === $extension; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
private function findTranslations(SplFileInfo $file): TranslationCollection |
60
|
|
|
{ |
61
|
|
|
return $this->extractor->extract($file->getContents(), new TranslationCollection()); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|