blob: 22dfded91ee4e3b7a732edefc79cda4b0c329bb8 (
plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
<?php
namespace SMW\Tests\Utils\File;
use RuntimeException;
/**
* @license GNU GPL v2+
* @since 2.1
*
* @author mwjames
*/
class BulkFileProvider {
/**
* @var string
*/
private $path = null;
/**
* @var string
*/
private $extension = 'json';
/**
* @since 2.1
*
* @param string $path
*/
public function __construct( $path ) {
$this->path = $path;
}
/**
* @since 2.1
*
* @param string $extension
*/
public function searchByFileExtension( $extension ) {
$this->extension = $extension;
}
/**
* @since 2.1
*
* @return array
*/
public function getFiles() {
$path = $this->checkPathReadability( $this->path );
return $this->iterateToFindFilesForExtension(
$path,
$this->extension
);
}
private function checkPathReadability( $path ) {
$path = str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, $path );
if ( is_readable( $path ) ) {
return $path;
}
throw new RuntimeException( "Expected an accessible {$path} path" );
}
private function iterateToFindFilesForExtension( $path, $extension ) {
$files = [];
$directoryIterator = new \RecursiveDirectoryIterator( $path );
foreach ( new \RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) {
if ( strtolower( substr( $fileInfo->getFilename(), -( strlen( $extension ) + 1 ) ) ) === ( '.' . $extension ) ) {
// Use a shortcut to be sortable while keep files with same name
// in different directories distinct
$files[$fileInfo->getFilename() . ' (' . substr( md5( $fileInfo->getPathname() ), 0, 5 ) .')'] = $fileInfo->getPathname();
}
}
asort( $files );
return $files;
}
}
|