From 6292e13087bab667373911c00986d9f3ee85d45d Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Fri, 29 Dec 2017 14:55:00 -0500 Subject: [PATCH] Use URI parsing logic from DrupalKernel to support more complex URIs. --- src/Config/ConfigLocator.php | 29 ++---------------------- src/Utils/StringUtils.php | 44 ++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/src/Config/ConfigLocator.php b/src/Config/ConfigLocator.php index 88bc9b1612..aae723922f 100644 --- a/src/Config/ConfigLocator.php +++ b/src/Config/ConfigLocator.php @@ -295,13 +295,9 @@ public function addSiteConfig($alias) { $uri = $alias->uri() ?: 'default'; - // Convert a fqdn to a hostname and look for matching entry in - // sites/sites.php. + // Parse a fqdn and look for matching entry in sites/sites.php. if (filter_var($uri, FILTER_VALIDATE_URL)) { - $hostname = StringUtils::convertUriToHostname($uri); - - // If $hostname matches a sites.php mappings, use dir from mapping. - if ($dir_name = $this->lookupSiteDirFromHostname($hostname, $alias->root())) { + if ($dir_name = StringUtils::lookupSiteDirFromUri($uri, $alias->root())) { $uri = $dir_name; } } @@ -324,27 +320,6 @@ public function addSiteConfig($alias) return $this; } - /** - * Lookup a site's directory via the sites.php file given a hostname. - * - * @param $hostname - * The hostname of a site. May be converted from URI. - * - * @return $drupalRoot - * The directory associated with that hostname. - */ - public function lookupSiteDirFromHostname($hostname, $drupalRoot) { - if (file_exists($drupalRoot . '/sites/sites.php')) { - $sites = array(); - // This will overwrite $sites with the desired mappings. - include ($drupalRoot . '/sites/sites.php'); - return isset($sites[$hostname]) ? $sites[$hostname] : FALSE; - } - else { - return FALSE; - } - } - /** * Add any configuration file found at any of the provided paths. Both the * provided location, and the directory `config` inside each provided location diff --git a/src/Utils/StringUtils.php b/src/Utils/StringUtils.php index 5665bc80f6..1d4cc1ecbf 100644 --- a/src/Utils/StringUtils.php +++ b/src/Utils/StringUtils.php @@ -134,23 +134,39 @@ public static function generatePassword($length = 10) } /** - * Convert from a URI to a site directory. + * Lookup a site's directory via the sites.php file given a uri. * * @param string $uri - * A uri, such as http://domain.com:8080/drupal - * - * @return string - * The hostname. + * The site URI. + * @return string $drupalRoot + * The directory associated with that URI. + * @param bool $require_settings + * Only directories with an existing settings.php file will be recognized. + * Defaults to TRUE. + * @see \Drupal\Core\DrupalKernel::findSitePath() */ - public static function convertUriToHostname($uri) { - $uri = str_replace('http://', '', $uri); - $uri = str_replace('https://', '', $uri); - if (drush_is_windows()) { - // Handle absolute paths on windows - $uri = str_replace(array(':/', ':\\'), array('.', '.'), $uri); + public static function lookupSiteDirFromUri($uri, $drupalRoot, $require_settings = TRUE) { + if (file_exists($drupalRoot . '/sites/sites.php')) { + $sites = []; + // This will overwrite $sites with the desired mappings. + include ($drupalRoot . '/sites/sites.php'); + + // This code is adapted from + // \Drupal\Core\DrupalKernel::findSitePath(). + $path = explode('/', parse_url($uri, PHP_URL_PATH)); + $server = explode('.', implode('.', array_reverse(explode(':', rtrim(parse_url($uri, PHP_URL_HOST), '.'))))); + for ($i = count($path); $i > 0; $i--) { + for ($j = count($server); $j > 0; $j--) { + $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($path, 0, $i)); + if (isset($sites[$dir]) && file_exists($drupalRoot . '/sites/' . $sites[$dir])) { + $dir = $sites[$dir]; + } + if (file_exists($drupalRoot . '/sites/' . $dir . '/settings.php') || (!$require_settings && file_exists($drupalRoot . '/sites/' . $dir))) { + return "$dir"; + } + } + } } - $hostname = str_replace(array('/', ':', '\\'), array('.', '.', '.'), $uri); - - return $hostname; + return FALSE; } }