From 2eb369b4557400030edf5b5c31ab0970d68cc152 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Mon, 14 Jun 2021 14:57:06 -0400 Subject: [PATCH] Avoid open file handles And added comments for future maintenance --- server.php | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/server.php b/server.php index 2934474e5..45ed63388 100644 --- a/server.php +++ b/server.php @@ -130,22 +130,27 @@ function get_valet_site_path($valetConfig, $siteName, $domain) $valetSitePath = null; foreach ($valetConfig['paths'] as $path) { if ($handle = opendir($path)) { + $dirs = []; while (false !== ($file = readdir($handle))) { if (! is_dir($path.'/'.$file)) continue; if (in_array($file, ['.', '..', '.DS_Store'])) continue; + $dirs[] = $file; + } + closedir($handle); - // match dir for lowercase, because Nginx only tells us lowercase names - if (strtolower($file) === $siteName) { - $valetSitePath = $path.'/'.$file; - break; + // Note: strtolower used below because Nginx only tells us lowercase names + foreach ($dirs as $dir) { + if (strtolower($dir) === $siteName) { + // early return when exact match for linked subdomain + return $path.'/'.$dir; } - if (strtolower($file) === $domain) { - $valetSitePath = $path.'/'.$file; + if (strtolower($dir) === $domain) { + // no early return here because the foreach may still have some subdomains to process with higher priority + $valetSitePath = $path.'/'.$dir; } } - closedir($handle); - + if ($valetSitePath) { return $valetSitePath; }