From 41da31cbb11f7b0c71a4443c6219a7522a1fdc53 Mon Sep 17 00:00:00 2001 From: enzo - Eduardo Garcia Date: Sat, 17 Dec 2016 17:13:36 -0600 Subject: [PATCH] Add logic to enable multisite installation (#3022) * Add logic to enable multisite installation * Enable multisite command if Drupal is not installed --- config/services/drupal-console/multisite.yml | 11 ---- services-drupal-install.yml | 10 ++++ src/Command/Site/InstallCommand.php | 56 ++++++++++++++------ src/Utils/Site.php | 35 ++++++++++++ 4 files changed, 86 insertions(+), 26 deletions(-) delete mode 100644 config/services/drupal-console/multisite.yml diff --git a/config/services/drupal-console/multisite.yml b/config/services/drupal-console/multisite.yml deleted file mode 100644 index 314d7a2db..000000000 --- a/config/services/drupal-console/multisite.yml +++ /dev/null @@ -1,11 +0,0 @@ -services: - console.multisite_debug: - class: Drupal\Console\Command\Multisite\DebugCommand - arguments: ['@app.root'] - tags: - - { name: drupal.command } - console.multisite_new: - class: Drupal\Console\Command\Multisite\NewCommand - arguments: ['@app.root'] - tags: - - { name: drupal.command } diff --git a/services-drupal-install.yml b/services-drupal-install.yml index 6d8034052..b338720a5 100644 --- a/services-drupal-install.yml +++ b/services-drupal-install.yml @@ -16,3 +16,13 @@ services: arguments: ['@console.extension_manager', '@console.site', '@console.configuration_manager', '@app.root'] tags: - { name: drupal.command } + console.multisite_debug: + class: Drupal\Console\Command\Multisite\DebugCommand + arguments: ['@app.root'] + tags: + - { name: drupal.command } + console.multisite_new: + class: Drupal\Console\Command\Multisite\NewCommand + arguments: ['@app.root'] + tags: + - { name: drupal.command } diff --git a/src/Command/Site/InstallCommand.php b/src/Command/Site/InstallCommand.php index 3990d268c..5907664bd 100644 --- a/src/Command/Site/InstallCommand.php +++ b/src/Command/Site/InstallCommand.php @@ -9,6 +9,7 @@ use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Config\Definition\Exception\Exception; +use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; @@ -25,6 +26,7 @@ use Drupal\Console\Utils\Site; use DrupalFinder\DrupalFinder; + class InstallCommand extends Command { use ContainerAwareCommandTrait; @@ -370,6 +372,19 @@ function ($profile) { protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); + $uri = parse_url($input->getParameterOption(['--uri', '-l']) ?: 'default', PHP_URL_HOST); + + if($this->site->multisiteMode($uri)) { + if(!$this->site->validMultisite($uri)) { + $io->error( + sprintf($this->trans('commands.site.install.messages.invalid-multisite'), $uri, $uri) + ); + exit(1); + } + + // Modify $_SERVER environment information to enable the Drupal installer use multisite configuration. + $_SERVER['HTTP_HOST'] = $uri; + } // Database options $dbType = $input->getOption('db-type')?:'mysql'; @@ -417,15 +432,15 @@ protected function execute(InputInterface $input, OutputInterface $output) } } - $this->backupSitesFile($io); - try { - $this->runInstaller($io, $input, $database); $drupalFinder = new DrupalFinder(); $drupalFinder->locateRoot(getcwd()); $composerRoot = $drupalFinder->getComposerRoot(); $drupalRoot = $drupalFinder->getDrupalRoot(); + + $this->runInstaller($io, $input, $database, $uri); + $autoload = $this->container->get('class_loader'); $drupal = new Drupal($autoload, $composerRoot, $drupalRoot); $container = $drupal->boot(); @@ -478,15 +493,18 @@ protected function restoreSitesFile(DrupalStyle $output) protected function runInstaller( DrupalStyle $io, InputInterface $input, - $database - ) { + $database, + $uri + ) + { $this->site->loadLegacyFile('/core/includes/install.core.inc'); - $driver = (string) $database['driver']; + $driver = (string)$database['driver']; + $settings = [ 'parameters' => [ - 'profile' => $input->getArgument('profile')?:'standard', - 'langcode' => $input->getOption('langcode')?:'en', + 'profile' => $input->getArgument('profile') ?: 'standard', + 'langcode' => $input->getOption('langcode') ?: 'en', ], 'forms' => [ 'install_settings_form' => [ @@ -495,26 +513,30 @@ protected function runInstaller( 'op' => 'Save and continue', ], 'install_configure_form' => [ - 'site_name' => $input->getOption('site-name')?:'Drupal 8', - 'site_mail' => $input->getOption('site-mail')?:'admin@example.org', + 'site_name' => $input->getOption('site-name') ?: 'Drupal 8', + 'site_mail' => $input->getOption('site-mail') ?: 'admin@example.org', 'account' => [ - 'name' => $input->getOption('account-name')?:'admin', - 'mail' => $input->getOption('account-mail')?:'admin@example.org', + 'name' => $input->getOption('account-name') ?: 'admin', + 'mail' => $input->getOption('account-mail') ?: 'admin@example.org', 'pass' => [ - 'pass1' => $input->getOption('account-pass')?:'admin', - 'pass2' => $input->getOption('account-pass')?:'admin' + 'pass1' => $input->getOption('account-pass') ?: 'admin', + 'pass2' => $input->getOption('account-pass') ?: 'admin' ], ], 'update_status_module' => [ 1 => true, 2 => true, ], - 'clean_url' => true, + 'clean_url' => true, 'op' => 'Save and continue', ], ] ]; + if (!$this->site->multisiteMode($uri)) { + $this->backupSitesFile($io); + } + $io->newLine(); $io->info($this->trans('commands.site.install.messages.installing')); @@ -529,6 +551,10 @@ protected function runInstaller( return; } + if (!$this->site->multisiteMode($uri)) { + $this->restoreSitesFile($io); + } + $io->success($this->trans('commands.site.install.messages.installed')); } } diff --git a/src/Utils/Site.php b/src/Utils/Site.php index a3bc8da70..c561f4cb7 100644 --- a/src/Utils/Site.php +++ b/src/Utils/Site.php @@ -135,4 +135,39 @@ public function getAutoload() return include $autoLoadFile; } + + /** + * @return boolean + */ + public function multisiteMode($uri) + { + if($uri != 'default') { + return true; + } + + return false; + } + + /** + * @return boolean + */ + public function validMultisite($uri) + { + $multiSiteFile = sprintf( + '%s/sites/sites.php', + $this->appRoot + ); + + if (file_exists($multiSiteFile)) { + include $multiSiteFile; + } else { + return false; + } + + if(isset($sites[$uri]) && is_dir($this->appRoot . "/sites/" . $sites[$uri])) { + return true; + } + + return false; + } }