From 47ba37c238c412a1fe77847f4e5519c5bea9a0c6 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Thu, 28 Sep 2017 22:22:37 +0200 Subject: [PATCH] Take default value for --sites-subdir in site install command from uri under certain circumstances. --- src/Commands/core/SiteInstallCommands.php | 42 ++++++++++++++++++----- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/Commands/core/SiteInstallCommands.php b/src/Commands/core/SiteInstallCommands.php index 6714e1f61a..6e4b09457e 100644 --- a/src/Commands/core/SiteInstallCommands.php +++ b/src/Commands/core/SiteInstallCommands.php @@ -265,23 +265,26 @@ public function pre(CommandData $commandData) $db_spec = $sql->getDbSpec(); $aliasRecord = $this->siteAliasManager()->getSelf(); + $root = $aliasRecord->root(); $dir = $commandData->input()->getOption('sites-subdir'); if (!$dir) { - $dir = $aliasRecord->get('uri'); + // We will allow the 'uri' from the site alias to provide + // a fallback name when '--sites-subdir' is not specified, but + // only if the uri and the folder name match, and only if + // the sites directory has already been created. + $dir = $this->getSitesSubdirFromUri($root, $aliasRecord->get('uri')); } - // Override with sites-subdir if specified. - if ($dir) { - $sites_subdir = "sites/$dir"; - } - if (empty($sites_subdir)) { - throw new \Exception(dt('Could not determine target sites directory for site to install.')); + if (!$dir) { + throw new \Exception(dt('Could not determine target sites directory for site to install. Use --site-subdir to specify.')); } + + $sites_subdir = Path::join('sites', $dir); $confPath = $sites_subdir; - $settingsfile = "$confPath/settings.php"; + $settingsfile = Path::join($confPath, 'settings.php'); $sitesfile = "sites/sites.php"; - $default = realpath($aliasRecord->root() . '/sites/default'); + $default = realpath(Path::join($root, 'sites/default')); $sitesfile_write = $confPath != $default && !file_exists($sitesfile); if (!file_exists($settingsfile)) { @@ -331,4 +334,25 @@ public function pre(CommandData $commandData) throw new \Exception(dt('Failed to create database: @error', array('@error' => implode(drush_shell_exec_output())))); } } + + /** + * Determine an appropriate site subdir name to use for the + * provided uri. + */ + protected function getSitesSubdirFromUri($root, $uri) + { + $dir = strtolower($uri); + // Always accept simple uris (e.g. 'dev', 'stage', etc.) + if (preg_match('#^[a-z0-9_-]*$#', $dir)) { + return $dir; + } + // Strip off the protocol from the provided uri -- however, + // now we will require that the sites subdir already exist. + $dir = preg_replace('#[^/]*/*#', '', $dir); + if (file_exists(Path::join($root, $dir))) { + return $dir; + } + return false; + } } +