diff options
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/File/BulkFileProvider.php')
-rw-r--r-- | www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/File/BulkFileProvider.php | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/File/BulkFileProvider.php b/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/File/BulkFileProvider.php new file mode 100644 index 00000000..22dfded9 --- /dev/null +++ b/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/File/BulkFileProvider.php @@ -0,0 +1,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; + } + +} |