/
XliffReader.php
71 lines (64 loc) · 1.86 KB
/
XliffReader.php
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
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Neos\Flow\I18n\Xliff\Service;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\Utility\LogEnvironment;
use Psr\Log\LoggerInterface;
/**
* An Xliff reader
*
* @Flow\Scope("singleton")
*/
class XliffReader
{
/**
* @Flow\Inject(name="Neos.Flow:I18nLogger")
* @var LoggerInterface
*/
protected $i18nLogger;
/**
* @param string $sourcePath
* @param callable $iterator
* @return void
*/
public function readFiles($sourcePath, callable $iterator)
{
$reader = new \XMLReader();
$reader->open($sourcePath);
$reader->read();
if ($reader->nodeType == \XMLReader::ELEMENT && $reader->name === 'xliff') {
$version = $reader->getAttribute('version');
$result = true;
while (!$this->isFileNode($reader) && $result) {
$result = $reader->read();
}
$offset = 0;
$iterator($reader, $offset, $version);
while ($reader->next()) {
if ($this->isFileNode($reader)) {
$iterator($reader, $offset, $version);
}
$offset++;
}
} else {
$this->i18nLogger->info('Given source "' . $sourcePath . '" is not a valid XLIFF file', LogEnvironment::fromMethodName(__METHOD__));
}
$reader->close();
}
/**
* @param \XMLReader $reader
* @return boolean
*/
protected function isFileNode(\XMLReader $reader)
{
return $reader->nodeType === \XMLReader::ELEMENT && $reader->name === 'file';
}
}