WP_CLI\Utils
find_file_upward()
Search for file by walking up the directory tree until the first file is found or until $stop_check($dir) returns true.
No Hooks.
Returns
null|String. Null if the file was not found.
Usage
find_file_upward( $files, $dir, $stop_check );
- $files(string|array
) (required) - The files (or file) to search for.
- $dir(string|null)
- The directory to start searching from; defaults to CWD.
Default:null - $stop_check(callable)
- Function which is passed the current dir each time a directory level is traversed.
Default:null
find_file_upward() find file upward code WP-CLI 2.13.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;
}