yf_glob(): file patterns on Alpine and in a phar

yf_glob($pattern, $flags = 0) is the framework's glob(). It takes the same patterns (*, ?, [...], {a,b}) and the same GLOB_* flags, and works where the native function does not:

  • Alpine Linux (musl libc) has no GLOB_BRACE: the constant is not defined there, so glob($p, GLOB_BRACE) stops with an error. Pass YF_GLOB_BRACE instead. Where the libc has brace support (glibc, macOS, BSD) it is GLOB_BRACE and glob() does the work; on musl it is a private flag and yf_glob() expands the braces in PHP, then runs glob() once per expanded pattern.
  • phar archives and other stream wrappers (phar://, file://): glob() cannot list them and finds nothing. yf_glob() lists only the path segments that have a wildcard (scandir() and fnmatch()) and checks the other segments with file_exists(), so the framework can run from a phar.
  • It always returns an array: [] when nothing matches or on an error, never false. (array) glob() gave [false] on an error; glob() ?: [] is no longer needed.
foreach (yf_glob($dir . '*.php') as $file) { ... }
$dirs = yf_glob(YF_PATH . 'plugins/*', GLOB_ONLYDIR);
$files = yf_glob($root . '{classes,functions}/{,*/}*.php', YF_GLOB_BRACE);
$inside = yf_glob('phar:///opt/app.phar/plugins/*/classes/*.php');

Results

  • Sorted in byte order (as glob() in the C locale), unless GLOB_NOSORT.
  • Braces work like glibc: the alternatives of the first group from left to right, nested and later groups expanded the same way ({a,b}.{txt,php} gives a.txt, a.php, b.txt, b.php); each expanded pattern is sorted on its own and the lists follow in that order. A path that two alternatives match is listed once (glibc lists it twice). A pattern with an unclosed brace (t/{a) is matched literally, as glibc does.
  • GLOB_ONLYDIR, GLOB_MARK (a / after folders), GLOB_NOCHECK (the pattern itself when nothing matches), GLOB_NOESCAPE and GLOB_NOSORT work on all paths, the phar ones included. A pattern ending in / lists folders only, each with its slash. * and ? do not match a leading dot.

Speed

A local pattern without braces (and a brace pattern where the libc has brace support) goes straight to glob(); the wrapper adds two string checks. Measured on PHP 8.3, glibc, a folder of 100 files (microseconds per call, mean of 20,000 calls):

Callµs
glob($d . '*.php')26.0 - 27.6
yf_glob($d . '*.php')26.7 - 27.4
glob($d . 'zz*') (no match)16.7 - 17.1
yf_glob($d . 'zz*') (no match)16.5 - 17.0
glob($d . '{f1,f2}*', GLOB_BRACE)42 - 47
yf_glob($d . '{f1,f2}*', YF_GLOB_BRACE), libc braces41 - 50
the same, braces expanded in PHP (the Alpine path)45 - 48
yf_glob($d . '*.php') through the PHP walker38 - 41
yf_glob('phar://app.phar/d/*.php'), 100 entries30 - 32

The difference on the native path is within the noise of the run; expanding braces in PHP costs about as much as libc braces.

Tests and the Alpine mode

$GLOBALS['YF_GLOB_EMULATE'] = 1 expands braces in PHP even where the libc could (it simulates musl on glibc); 2 also lists local paths in PHP. Leave it unset in production. The unit tests (function_yf_glob_test) compare all modes with glob() and build a small phar to check phar:// patterns.

Framework code calls yf_glob() only: make raw-glob (also in make hygiene and the pre-commit hook) flags a raw glob( or GLOB_BRACE in classes/, functions/, plugins/, console/, services/ and the installer. Site code may keep glob(); switch to yf_glob() and YF_GLOB_BRACE to run on Alpine.