TQ\Vcs\FileSystem::bubble PHP Méthode

bubble() public static méthode

Bubbles up a path until $comparator returns true
public static bubble ( string $path, Closure $comparator ) : string | null
$path string The path
$comparator Closure The callback used inside when bubbling to determine a finding
Résultat string | null The path that is found or NULL otherwise
    public static function bubble($path, \Closure $comparator)
    {
        $found = null;
        $path = self::normalizeDirectorySeparator($path);
        $drive = null;
        if (preg_match('~^(\\w:)(.+)~', $path, $parts)) {
            $drive = $parts[1];
            $path = $parts[2];
        }
        $pathParts = explode('/', $path);
        while (count($pathParts) > 0 && $found === null) {
            $path = implode('/', $pathParts);
            if ($comparator($path)) {
                $found = $path;
            }
            array_pop($pathParts);
        }
        if ($drive && $found) {
            $found = $drive . $found;
        }
        return $found;
    }

Usage Example

 /**
  * Tries to find the root directory for a given repository path
  *
  * @param   string      $path       The file system path
  * @return  string|null             NULL if the root cannot be found, the root path otherwise
  */
 public static function findRepositoryRoot($path)
 {
     return FileSystem::bubble($path, function ($p) {
         $gitDir = $p . '/' . '.git';
         return file_exists($gitDir) && is_dir($gitDir);
     });
 }
All Usage Examples Of TQ\Vcs\FileSystem::bubble