<?php

declare(strict_types=1);

namespace apexl\Utils\Paths;

final readonly class Globstar
{
    public static function globstar($pattern, $flags = 0): array
    {
        if (stripos($pattern, '**') === false) {
            $files = glob($pattern, $flags);
        } else {
            $position = stripos($pattern, '**');
            $rootPattern = substr($pattern, 0, $position - 1);
            $restPattern = substr($pattern, $position + 2);
            $patterns = array ($rootPattern . $restPattern);
            $rootPattern .= '/*';
            while ($dirs = glob($rootPattern, GLOB_ONLYDIR)) {
                $rootPattern .= '/*';
                foreach ($dirs as $dir) {
                    $patterns[] = $dir . $restPattern;
                }
            }
            $files = array ();
            foreach ($patterns as $pat) {
                $files = array_merge($files, self::globstar($pat, $flags));
            }
        }
        $files = array_unique($files);
        sort($files);
        return $files;
    }
}