Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters to exclude directories and files before import #208

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
84 changes: 79 additions & 5 deletions lib/runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,31 @@
*
* @return array|\WP_Error
*/
function get_wp_files( $directory ) {
$iterableFiles = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator( $directory )
);
$files = array();
function get_wp_files( $root ) {
$dirIterator = new \RecursiveDirectoryIterator( $root );
$filterIterator = filter_wp_directories( $root, $dirIterator );

if ( $filterIterator instanceof \RecursiveCallbackFilterIterator ) {
$iterableFiles = new \RecursiveIteratorIterator( $filterIterator );
} else {
$iterableFiles = new \RecursiveIteratorIterator( $dirIterator );
}

$files = array();

try {
foreach ( $iterableFiles as $file ) {
if ( 'php' !== $file->getExtension() ) {
continue;
}

/**
* Whether to exclude a file for parsing.
*/
if ( ! apply_filters( 'wp_parser_pre_get_wp_file', true, $file->getPathname(), $root ) ) {
continue;
}

$files[] = $file->getPathname();
}
} catch ( \UnexpectedValueException $exc ) {
Expand All @@ -38,6 +51,67 @@ function get_wp_files( $directory ) {
return $files;
}

/**
* Fiter the directories to parse.
*
* @param string $root Root dir.
* @param object $dirIterator RecursiveDirectoryIterator.
* @return object|false RecursiveCallbackFilterIterator or false.
*/
function filter_wp_directories( $root, $dirIterator ) {
$root = trailingslashit( $root );

/**
* Filter directories found in the root directory.
*
* For example: 'vendor', 'tests', 'specific/directory'.
*
* @param unknown $exclude Array with directories to skip parsing. Default empty array().
* @param unknown $root Root directory to parse.
*/
$exclude = apply_filters( 'wp_parser_exclude_directories', array(), $root );

/**
* Whether to exlude directories if found in a subdirectory.
*
* @param unknown $strict. Exclude subdirectories. Default false.
* @param unknown $root Root directory to parse.
*/
$strict = apply_filters( 'wp_parser_exclude_directories_strict', false, $root );

if ( ! $exclude ) {
return false;
}

$filter = new \RecursiveCallbackFilterIterator( $dirIterator, function ( $current ) use ( $root, $exclude, $strict ) {
if ( $current->isFile() && ( 'php' !== $current->getExtension() ) ) {
return false;
}

if ( ! $current->isDir() ) {
return true;
}

// Exclude directories strict.
$dir_name = $current->getFilename();
if ( $strict && in_array( $dir_name, $exclude ) ) {
return false;
}

// Exclude directories in the root directory.
$current_path = $current->getPathname();
foreach ( $exclude as $dir ) {
if ( ( $root . untrailingslashit( $dir ) ) === $current_path ) {
return false;
}
}

return true;
} );

return $filter;
}

/**
* @param array $files
* @param string $root
Expand Down