Skip to content

Commit

Permalink
Use URI parsing logic from DrupalKernel to support more complex URIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
grasmash committed Dec 29, 2017
1 parent 7567410 commit 6292e13
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
29 changes: 2 additions & 27 deletions src/Config/ConfigLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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
Expand Down
44 changes: 30 additions & 14 deletions src/Utils/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 6292e13

Please sign in to comment.