Skip to content

[multisite:new] Force sites.php file create/update. #3031

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

Merged
merged 1 commit into from
Dec 20, 2016
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
132 changes: 65 additions & 67 deletions src/Command/Multisite/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Style\DrupalStyle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -18,7 +19,7 @@
use Symfony\Component\Filesystem\Exception\FileNotFoundException;

/**
* Class MultisiteNewCommand
* Class NewCommand
* @package Drupal\Console\Command\Multisite
*/
class NewCommand extends Command
Expand All @@ -38,14 +39,14 @@ public function __construct($appRoot)
}

/**
* @var \Symfony\Component\Filesystem\Filesystem;
* @var Filesystem;
*/
protected $fs;

/**
* @var string
*/
protected $subdir = '';
protected $directory = '';

/**
* {@inheritdoc}
Expand All @@ -56,21 +57,20 @@ public function configure()
->setDescription($this->trans('commands.multisite.new.description'))
->setHelp($this->trans('commands.multisite.new.help'))
->addArgument(
'sites-subdir',
InputOption::VALUE_REQUIRED,
$this->trans('commands.multisite.new.arguments.sites-subdir')
'directory',
InputArgument::REQUIRED,
$this->trans('commands.multisite.new.arguments.directory')
)
->addOption(
'site-uri',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.multisite.new.options.site-uri')
->addArgument(
'uri',
InputArgument::REQUIRED,
$this->trans('commands.multisite.new.arguments.uri')
)
->addOption(
'copy-install',
'copy-default',
'',
InputOption::VALUE_NONE,
$this->trans('commands.multisite.new.options.copy-install')
$this->trans('commands.multisite.new.options.copy-default')
);
}

Expand All @@ -79,58 +79,56 @@ public function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new DrupalStyle($input, $output);
$this->subdir = $input->getArgument('sites-subdir');
$io = new DrupalStyle($input, $output);
$this->fs = new Filesystem();
$this->directory = $input->getArgument('directory');

if (!$this->directory) {
$io->error($this->trans('commands.multisite.new.errors.subdir-empty'));

if (empty($this->subdir)) {
$output->error($this->trans('commands.multisite.new.errors.subdir-empty'));
return 1;
}

$this->fs = new Filesystem();

if ($this->fs->exists($this->appRoot . '/sites/' . $this->subdir)) {
$output->error(
if ($this->fs->exists($this->appRoot . '/sites/' . $this->directory)) {
$io->error(
sprintf(
$this->trans('commands.multisite.new.errors.subdir-exists'),
$this->subdir
$this->directory
)
);

return 1;
}

if (!$this->fs->exists($this->appRoot . '/sites/default')) {
$output->error($this->trans('commands.multisite.new.errors.default-missing'));
$io->error($this->trans('commands.multisite.new.errors.default-missing'));

return 1;
}

try {
$this->fs->mkdir($this->appRoot . '/sites/' . $this->subdir, 0755);
$this->fs->mkdir($this->appRoot . '/sites/' . $this->directory, 0755);
} catch (IOExceptionInterface $e) {
$output->error(
$io->error(
sprintf(
$this->trans('commands.multisite.new.errors.mkdir-fail'),
$this->subdir
$this->directory
)
);

return 1;
}

if ($uri = $input->getOption('site-uri')) {
try {
$this->addToSitesFile($output, $uri);
} catch (\Exception $e) {
$output->error($e->getMessage());
return 1;
}
}
$uri = $input->getArgument('uri');
try {
$this->addToSitesFile($io, $uri);
} catch (\Exception $e) {
$io->error($e->getMessage());

if ($input->getOption('copy-install')) {
$this->copyExistingInstall($output);
return 1;
}

$this->createFreshSite($output);
$this->createFreshSite($io);

return 0;
}
Expand Down Expand Up @@ -159,7 +157,7 @@ protected function addToSitesFile(DrupalStyle $output, $uri)
throw new FileNotFoundException($this->trans('commands.multisite.new.errors.sites-missing'));
}

$sites_file_contents .= "\n\$sites['$uri'] = '$this->subdir';";
$sites_file_contents .= "\n\$sites['$uri'] = '$this->directory';";

try {
$this->fs->dumpFile($this->appRoot . '/sites/sites.php', $sites_file_contents);
Expand All @@ -172,12 +170,12 @@ protected function addToSitesFile(DrupalStyle $output, $uri)
/**
* Copies detected default install alters settings.php to fit the new directory.
*
* @param DrupalStyle $output
* @param DrupalStyle $io
*/
protected function copyExistingInstall(DrupalStyle $output)
protected function copyExistingInstall(DrupalStyle $io)
{
if (!$this->fs->exists($this->appRoot . '/sites/default/settings.php')) {
$output->error(
$io->error(
sprintf(
$this->trans('commands.multisite.new.errors.file-missing'),
'sites/default/settings.php'
Expand All @@ -190,75 +188,75 @@ protected function copyExistingInstall(DrupalStyle $output)
try {
$this->fs->mirror(
$this->appRoot . '/sites/default/files',
$this->appRoot . '/sites/' . $this->subdir . '/files'
$this->appRoot . '/sites/' . $this->directory . '/files'
);
} catch (IOExceptionInterface $e) {
$output->error(
$io->error(
sprintf(
$this->trans('commands.multisite.new.errors.copy-fail'),
'sites/default/files',
'sites/' . $this->subdir . '/files'
'sites/' . $this->directory . '/files'
)
);
return;
}
} else {
$output->warning($this->trans('commands.multisite.new.warnings.missing-files'));
$io->warning($this->trans('commands.multisite.new.warnings.missing-files'));
}

$settings = file_get_contents($this->appRoot . '/sites/default/settings.php');
$settings = str_replace('sites/default', 'sites/' . $this->subdir, $settings);
$settings = str_replace('sites/default', 'sites/' . $this->directory, $settings);

try {
$this->fs->dumpFile(
$this->appRoot . '/sites/' . $this->subdir . '/settings.php',
$this->appRoot . '/sites/' . $this->directory . '/settings.php',
$settings
);
} catch (IOExceptionInterface $e) {
$output->error(
$io->error(
sprintf(
$this->trans('commands.multisite.new.errors.write-fail'),
'sites/' . $this->subdir . '/settings.php'
'sites/' . $this->directory . '/settings.php'
)
);
return;
}

$this->chmodSettings($output);
$this->chmodSettings($io);

$output->success(
$io->success(
sprintf(
$this->trans('commands.multisite.new.messages.copy-install'),
$this->subdir
$this->trans('commands.multisite.new.messages.copy-default'),
$this->directory
)
);
}

/**
* Creates site folder with clean settings.php file.
*
* @param DrupalStyle $output
* @param DrupalStyle $io
*/
protected function createFreshSite(DrupalStyle $output)
protected function createFreshSite(DrupalStyle $io)
{
if ($this->fs->exists($this->appRoot . '/sites/default/default.settings.php')) {
try {
$this->fs->copy(
$this->appRoot . '/sites/default/default.settings.php',
$this->appRoot . '/sites/' . $this->subdir . '/settings.php'
$this->appRoot . '/sites/' . $this->directory . '/settings.php'
);
} catch (IOExceptionInterface $e) {
$output->error(
$io->error(
sprintf(
$this->trans('commands.multisite.new.errors.copy-fail'),
$this->appRoot . '/sites/default/default.settings.php',
$this->appRoot . '/sites/' . $this->subdir . '/settings.php'
$this->appRoot . '/sites/' . $this->directory . '/settings.php'
)
);
return;
}
} else {
$output->error(
$io->error(
sprintf(
$this->trans('commands.multisite.new.errors.file-missing'),
'sites/default/default.settings.php'
Expand All @@ -267,12 +265,12 @@ protected function createFreshSite(DrupalStyle $output)
return;
}

$this->chmodSettings($output);
$this->chmodSettings($io);

$output->success(
$io->success(
sprintf(
$this->trans('commands.multisite.new.messages.fresh-site'),
$this->subdir
$this->directory
)
);
}
Expand All @@ -284,17 +282,17 @@ protected function createFreshSite(DrupalStyle $output)
* anyone. Also, Drupal likes being able to write to it during, for example,
* a fresh install.
*
* @param DrupalStyle $output
* @param DrupalStyle $io
*/
protected function chmodSettings(DrupalStyle $output)
protected function chmodSettings(DrupalStyle $io)
{
try {
$this->fs->chmod($this->appRoot . '/sites/' . $this->subdir . '/settings.php', 0640);
$this->fs->chmod($this->appRoot . '/sites/' . $this->directory . '/settings.php', 0640);
} catch (IOExceptionInterface $e) {
$output->error(
$io->error(
sprintf(
$this->trans('commands.multisite.new.errors.chmod-fail'),
$this->appRoot . '/sites/' . $this->subdir . '/settings.php'
$this->appRoot . '/sites/' . $this->directory . '/settings.php'
)
);
}
Expand Down