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

[#2593] Register multisite commands. #2647

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions config/services/multisite.yml
@@ -1,9 +1,11 @@
#services:
# multisite_debug:
# class: Drupal\Console\Command\Multisite\DebugCommand
# tags:
# - { name: console.command }
# multisite_new:
# class: Drupal\Console\Command\Multisite\NewCommand
# tags:
# - { name: console.command }
services:
console.multisite_debug:
class: Drupal\Console\Command\Multisite\DebugCommand
arguments: ['@app.root']
tags:
- { name: console.command }
console.multisite_new:
class: Drupal\Console\Command\Multisite\NewCommand
arguments: ['@app.root']
tags:
- { name: console.command }
20 changes: 17 additions & 3 deletions src/Command/Multisite/DebugCommand.php
Expand Up @@ -20,8 +20,20 @@
class DebugCommand extends Command
{
use CommandTrait;

protected $appRoot;

/**
* DebugCommand constructor.
* @param $appRoot
*/
public function __construct($appRoot) {
$this->appRoot = $appRoot;
parent::__construct();
}

/**
* @{@inheritdoc}
* {@inheritdoc}
*/
public function configure()
{
Expand All @@ -43,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$multiSiteFile = sprintf(
'%s/sites/sites.php',
$this->get('site')->getRoot()
$this->appRoot
);

if (file_exists($multiSiteFile)) {
Expand Down Expand Up @@ -71,10 +83,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($sites as $site => $directory) {
$tableRows[] = [
$site,
$this->get('site')->getRoot() . '/' . $directory
$this->appRoot . '/' . $directory
];
}

$io->table($tableHeader, $tableRows);

return 0;
}
}
76 changes: 41 additions & 35 deletions src/Command/Multisite/NewCommand.php
Expand Up @@ -25,22 +25,27 @@ class NewCommand extends Command
{
use CommandTrait;

protected $appRoot;

/**
* @var \Symfony\Component\Filesystem\Filesystem;
* DebugCommand constructor.
* @param $appRoot
*/
protected $fs;
public function __construct($appRoot) {
$this->appRoot = $appRoot;
parent::__construct();
}

/**
* @var string
* @var \Symfony\Component\Filesystem\Filesystem;
*/
protected $root = '';
protected $fs;

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


/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -78,54 +83,55 @@ protected function execute(InputInterface $input, OutputInterface $output)

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

$this->fs = new Filesystem();
$this->root = $this->get('site')->getRoot();

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

return 1;
}

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

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

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

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

$this->createFreshSite($output);

return 0;
}

/**
Expand All @@ -138,15 +144,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function addToSitesFile(DrupalStyle $output, $uri)
{
if ($this->fs->exists($this->root . '/sites/sites.php')) {
$sites_is_dir = is_dir($this->root . '/sites/sites.php');
$sites_readable = is_readable($this->root . '/sites/sites.php');
if ($this->fs->exists($this->appRoot . '/sites/sites.php')) {
$sites_is_dir = is_dir($this->appRoot . '/sites/sites.php');
$sites_readable = is_readable($this->appRoot . '/sites/sites.php');
if ($sites_is_dir || !$sites_readable) {
throw new FileNotFoundException($this->trans('commands.multisite.new.errors.sites-invalid'));
}
$sites_file_contents = file_get_contents($this->root . '/sites/sites.php');
$sites_file_contents = file_get_contents($this->appRoot . '/sites/sites.php');
} elseif ($this->fs->exists($this->root . '/sites/example.sites.php')) {
$sites_file_contents = file_get_contents($this->root . '/sites/example.sites.php');
$sites_file_contents = file_get_contents($this->appRoot . '/sites/example.sites.php');
$sites_file_contents .= "\n\$sites = [];";
} else {
throw new FileNotFoundException($this->trans('commands.multisite.new.errors.sites-missing'));
Expand All @@ -155,8 +161,8 @@ protected function addToSitesFile(DrupalStyle $output, $uri)
$sites_file_contents .= "\n\$sites['$uri'] = '$this->subdir';";

try {
$this->fs->dumpFile($this->root . '/sites/sites.php', $sites_file_contents);
$this->fs->chmod($this->root . '/sites/sites.php', 0640);
$this->fs->dumpFile($this->appRoot . '/sites/sites.php', $sites_file_contents);
$this->fs->chmod($this->appRoot . '/sites/sites.php', 0640);
} catch (IOExceptionInterface $e) {
$output->error('commands.multisite.new.errors.sites-other');
}
Expand All @@ -169,7 +175,7 @@ protected function addToSitesFile(DrupalStyle $output, $uri)
*/
protected function copyExistingInstall(DrupalStyle $output)
{
if (!$this->fs->exists($this->root . '/sites/default/settings.php')) {
if (!$this->fs->exists($this->appRoot . '/sites/default/settings.php')) {
$output->error(
sprintf(
$this->trans('commands.multisite.new.errors.file-missing'),
Expand All @@ -179,11 +185,11 @@ protected function copyExistingInstall(DrupalStyle $output)
return;
}

if ($this->fs->exists($this->root . '/sites/default/files')) {
if ($this->fs->exists($this->appRoot . '/sites/default/files')) {
try {
$this->fs->mirror(
$this->root . '/sites/default/files',
$this->root . '/sites/' . $this->subdir . '/files'
$this->appRoot . '/sites/default/files',
$this->appRoot . '/sites/' . $this->subdir . '/files'
);
} catch (IOExceptionInterface $e) {
$output->error(
Expand All @@ -199,12 +205,12 @@ protected function copyExistingInstall(DrupalStyle $output)
$output->warning($this->trans('commands.multisite.new.warnings.missing-files'));
}

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

try {
$this->fs->dumpFile(
$this->root . '/sites/' . $this->subdir . '/settings.php',
$this->appRoot . '/sites/' . $this->subdir . '/settings.php',
$settings
);
} catch (IOExceptionInterface $e) {
Expand Down Expand Up @@ -234,18 +240,18 @@ protected function copyExistingInstall(DrupalStyle $output)
*/
protected function createFreshSite(DrupalStyle $output)
{
if ($this->fs->exists($this->root . '/sites/default/default.settings.php')) {
if ($this->fs->exists($this->appRoot . '/sites/default/default.settings.php')) {
try {
$this->fs->copy(
$this->root . '/sites/default/default.settings.php',
$this->root . '/sites/' . $this->subdir . '/settings.php'
$this->appRoot . '/sites/default/default.settings.php',
$this->appRoot . '/sites/' . $this->subdir . '/settings.php'
);
} catch (IOExceptionInterface $e) {
$output->error(
sprintf(
$this->trans('commands.multisite.new.errors.copy-fail'),
$this->root . '/sites/default/default.settings.php',
$this->root . '/sites/' . $this->subdir . '/settings.php'
$this->appRoot . '/sites/default/default.settings.php',
$this->appRoot . '/sites/' . $this->subdir . '/settings.php'
)
);
return;
Expand Down Expand Up @@ -282,12 +288,12 @@ protected function createFreshSite(DrupalStyle $output)
protected function chmodSettings(DrupalStyle $output)
{
try {
$this->fs->chmod($this->root . '/sites/' . $this->subdir . '/settings.php', 0640);
$this->fs->chmod($this->appRoot . '/sites/' . $this->subdir . '/settings.php', 0640);
} catch (IOExceptionInterface $e) {
$output->error(
sprintf(
$this->trans('commands.multisite.new.errors.chmod-fail'),
$this->root . '/sites/' . $this->subdir . '/settings.php'
$this->appRoot . '/sites/' . $this->subdir . '/settings.php'
)
);
}
Expand Down