|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* @copyright 2011 - 2016, Stephan Gambke, mwjames |
|
5
|
|
|
* |
|
6
|
|
|
* @license GPL-2.0-or-later |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of the MediaWiki extension Lingo. |
|
9
|
|
|
* The Lingo extension is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by the Free |
|
11
|
|
|
* Software Foundation; either version 2 of the License, or (at your option) any |
|
12
|
|
|
* later version. |
|
13
|
|
|
* |
|
14
|
|
|
* The Lingo extension is distributed in the hope that it will be useful, but |
|
15
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
16
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
17
|
|
|
* details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License along |
|
20
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 2.0.1 |
|
23
|
|
|
* @file |
|
24
|
|
|
* @ingroup Lingo |
|
25
|
|
|
* @ingroup Test |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace Lingo\Tests\Util; |
|
29
|
|
|
|
|
30
|
|
|
use RuntimeException; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @since 2.0.1 |
|
34
|
|
|
* @author mwjames, Stephan Gambke |
|
35
|
|
|
* @ingroup Lingo |
|
36
|
|
|
* @ingroup Test |
|
37
|
|
|
*/ |
|
38
|
|
|
class XmlFileProvider { |
|
39
|
|
|
|
|
40
|
|
|
protected $path = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $path |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( $path ) { |
|
46
|
|
|
$this->path = $path; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return string[] |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getFiles() { |
|
53
|
|
|
return $this->loadXmlFiles( $this->readDirectory( $this->path ) ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param String $path |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function readDirectory( $path ) { |
|
61
|
|
|
$path = str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, $path ); |
|
62
|
|
|
|
|
63
|
|
|
if ( is_readable( $path ) ) { |
|
64
|
|
|
return $path; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
throw new RuntimeException( "Expected an accessible {$path} path" ); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param String $path |
|
72
|
|
|
* @return string[] |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function loadXmlFiles( $path ) { |
|
75
|
|
|
$directoryIterator = new \RecursiveDirectoryIterator( $path ); |
|
76
|
|
|
$iteratorIterator = new \RecursiveIteratorIterator( $directoryIterator ); |
|
77
|
|
|
$regexIterator = new \RegexIterator( $iteratorIterator, '/^.+\.xml$/i', \RecursiveRegexIterator::GET_MATCH ); |
|
78
|
|
|
|
|
79
|
|
|
$files = call_user_func_array( 'array_merge', iterator_to_array( $regexIterator ) ); |
|
80
|
|
|
|
|
81
|
|
|
return $files; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.