Skip to content

Commit 69309ec

Browse files
Take default value for --sites-subdir in site install command from uri under certain circumstances.
1 parent 25ffafc commit 69309ec

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/Commands/core/SiteInstallCommands.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,23 +265,26 @@ public function pre(CommandData $commandData)
265265
$db_spec = $sql->getDbSpec();
266266

267267
$aliasRecord = $this->siteAliasManager()->getSelf();
268+
$root = $aliasRecord->root();
268269

269270
$dir = $commandData->input()->getOption('sites-subdir');
270271
if (!$dir) {
271-
$dir = $aliasRecord->get('uri');
272+
// We will allow the 'uri' from the site alias to provide
273+
// a fallback name when '--sites-subdir' is not specified, but
274+
// only if the uri and the folder name match, and only if
275+
// the sites directory has already been created.
276+
$dir = $this->getSitesSubdirFromUri($root, $aliasRecord->get('uri'));
272277
}
273278

274-
// Override with sites-subdir if specified.
275-
if ($dir) {
276-
$sites_subdir = "sites/$dir";
277-
}
278-
if (empty($sites_subdir)) {
279-
throw new \Exception(dt('Could not determine target sites directory for site to install.'));
279+
if (!$dir) {
280+
throw new \Exception(dt('Could not determine target sites directory for site to install. Use --site-subdir to specify.'));
280281
}
282+
283+
$sites_subdir = Path::join('sites', $dir);
281284
$confPath = $sites_subdir;
282-
$settingsfile = "$confPath/settings.php";
285+
$settingsfile = Path::join($confPath, 'settings.php');
283286
$sitesfile = "sites/sites.php";
284-
$default = realpath($aliasRecord->root() . '/sites/default');
287+
$default = realpath(Path::join($root, 'sites/default'));
285288
$sitesfile_write = $confPath != $default && !file_exists($sitesfile);
286289

287290
if (!file_exists($settingsfile)) {
@@ -331,4 +334,25 @@ public function pre(CommandData $commandData)
331334
throw new \Exception(dt('Failed to create database: @error', array('@error' => implode(drush_shell_exec_output()))));
332335
}
333336
}
337+
338+
/**
339+
* Determine an appropriate site subdir name to use for the
340+
* provided uri.
341+
*/
342+
protected function getSitesSubdirFromUri($root, $uri)
343+
{
344+
$dir = tolower($url);
345+
// Always accept simple uris (e.g. 'dev', 'stage', etc.)
346+
if (preg_match('#^[a-z0-9_-]*$#', $dir)) {
347+
return $dir;
348+
}
349+
// Strip off the protocol from the provided uri -- however,
350+
// now we will require that the sites subdir already exist.
351+
$dir = preg_replace('#[^/]*/*#', '', $dir);
352+
if (!file_exists(Path::join($root, $dir))) {
353+
return $dir;
354+
}
355+
return false;
356+
}
334357
}
358+

0 commit comments

Comments
 (0)