WP_CLI\Utils

find_file_upward()WP-CLI 1.0

Search for file by walking up the directory tree until the first file is found or until $stop_check($dir) returns true.

Хуков нет.

Возвращает

null|Строку. Null if the file was not found.

Использование

find_file_upward( $files, $dir, $stop_check );
$files(строка|массив) (обязательный)
The files (or file) to search for.
$dir(строка|null)
The directory to start searching from; defaults to CWD.
По умолчанию: null
$stop_check(callable)
Function which is passed the current dir each time a directory level is traversed.
По умолчанию: null

Код find_file_upward() WP-CLI 2.8.0-alpha

function find_file_upward( $files, $dir = null, $stop_check = null ) {
	$files = (array) $files;
	if ( is_null( $dir ) ) {
		$dir = getcwd();
	}
	while ( is_readable( $dir ) ) {
		// Stop walking up when the supplied callable returns true being passed the $dir
		if ( is_callable( $stop_check ) && call_user_func( $stop_check, $dir ) ) {
			return null;
		}

		foreach ( $files as $file ) {
			$path = $dir . DIRECTORY_SEPARATOR . $file;
			if ( file_exists( $path ) ) {
				return $path;
			}
		}

		$parent_dir = dirname( $dir );
		if ( empty( $parent_dir ) || $parent_dir === $dir ) {
			break;
		}
		$dir = $parent_dir;
	}
	return null;
}