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 filter for skipping files earlier in the pipeline than 'wp_parser_pre_import_file' #198

Open
pbiron opened this issue May 26, 2018 · 3 comments

Comments

@pbiron
Copy link

pbiron commented May 26, 2018

\\WP_Parser\Importer::import_file() provides the wp_parser_pre_import_file filter to skip importing a particular file.

It would improve efficiency if there were a similar filter earlier in the pipeline, e.g., in \\WP_Parser\get_wp_files(). For example,

function get_wp_files( $directory ) {
	$iterableFiles = new \RecursiveIteratorIterator(
		new \RecursiveDirectoryIterator( $directory )
	);
	$files         = array();

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

			if ( ! apply_filters( 'wp_parser_pre_get_wp_file', true, $file->getPathname(), $directory ) ) {
				continue;
			}

			$files[] = $file->getPathname();
		}
	} catch ( \UnexpectedValueException $exc ) {
		return new \WP_Error(
			'unexpected_value_exception',
			sprintf( 'Directory [%s] contained a directory we can not recurse into', $directory )
		);
	}

	return $files;
}

I use this plugin to produce developer documentation for plugins/themes I create for clients, and currently hook into wp_parser_pre_import_file to skip importing anything in tests/ or vendor/ sub-dirs of a plugin/theme. Having the proposed wp_parser_pre_get_wp_file hook would allow avoiding the overhead of parsing all those files, only to skip them later in the pipeline.

Here is an example of a func I would hook into the proposed filter, to show why the proposed filter takes the additional $directory param that wp_parser_pre_import_file does not:

add_filter( 'wp_parser_pre_get_wp_file', 'shc_wp_parser_skip_files', 10, 3 );

function shc_wp_parser_skip_files( $default, $file, $directory ) {
	$file = str_replace( $directory . DIRECTORY_SEPARATOR, '', $file );
	$paths = explode( DIRECTORY_SEPARATOR, $file );
	if ( in_array( $paths[1], array( 'tests', 'vendor' ) ) ) {
		return false;
	}

	return $default;
}
@pbiron
Copy link
Author

pbiron commented May 26, 2018

I'd also recommend adding something like the following to \\WP_Parser\Importer::import_file():

if ( ! apply_filters( 'wp_parser_pre_import_file', true, $file ) ) {
	$this->logger->info( sprintf( 'Skipping  file %1$s', $file['path'] ) );

	return;
}

whether this proposal is accepted or not.

@keesiemeijer
Copy link
Contributor

keesiemeijer commented May 26, 2018

+1 for this feature. I also have a plugin parsing plugins where I exclude directories. For filtering directories we should use a RecursiveCallbackFilterIterator so the directories (and its files) are not traversed recursively. This saves a lot of computing power.

Here is how I did it for my plugin
https://github.com/keesiemeijer/wp-plugin-parser/blob/master/includes/class-file-parser.php#L109

@sebastienserre
Copy link

Hi All,
The @keesiemeijer solution is working great, what about merging it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants