Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take default value for --sites-subdir in site install command #2982

Merged
merged 2 commits into from
Sep 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions src/Commands/core/SiteInstallCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}
}