From 5f64fba4ee2dc662cdbc85143e1eeed48ab9ee44 Mon Sep 17 00:00:00 2001 From: Harold Date: Wed, 23 Oct 2019 12:09:21 -0600 Subject: [PATCH] Create a function to get the multisite name --- config/services/multisite.yml | 2 +- src/Command/Generate/ModuleCommand.php | 4 ++-- src/Command/Generate/ThemeCommand.php | 4 ++-- src/Command/Generate/ThemeSettingCommand.php | 4 ++-- src/Command/Multisite/UpdateCommand.php | 14 +++++++++++--- src/Command/Site/InstallCommand.php | 4 ++-- src/Utils/Site.php | 15 +++++++++++++++ 7 files changed, 35 insertions(+), 12 deletions(-) diff --git a/config/services/multisite.yml b/config/services/multisite.yml index f28d6abd1..364f6d4a1 100644 --- a/config/services/multisite.yml +++ b/config/services/multisite.yml @@ -6,6 +6,6 @@ services: - { name: drupal.command } console.multisite_update: class: Drupal\Console\Command\Multisite\UpdateCommand - arguments: ['@app.root'] + arguments: ['@app.root', '@console.site'] tags: - { name: drupal.command } diff --git a/src/Command/Generate/ModuleCommand.php b/src/Command/Generate/ModuleCommand.php index b41ab97a8..d53ef8d30 100644 --- a/src/Command/Generate/ModuleCommand.php +++ b/src/Command/Generate/ModuleCommand.php @@ -199,7 +199,7 @@ protected function execute(InputInterface $input, OutputInterface $output) // Check that it is an absolute path or otherwise create an absolute path using appRoot $modulePath = $input->getOption('module-path'); if(is_null($modulePath)) { - $uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST); + $uri = $this->site->getMultisiteName($input); $defaultModulePath = 'modules/custom'; $modulePath = $this->site->multisiteMode($uri)? 'sites/'.$this->site->getMultisiteDir($uri).'/'.$defaultModulePath : $defaultModulePath; } @@ -315,7 +315,7 @@ function ($machine_name) use ($validator) { $modulePath = $input->getOption('module-path'); if (!$modulePath) { - $uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST); + $uri = $this->site->getMultisiteName($input); $defaultModulePath = 'modules/custom'; $modulePath = $this->getIo()->ask( $this->trans('commands.generate.module.questions.module-path'), diff --git a/src/Command/Generate/ThemeCommand.php b/src/Command/Generate/ThemeCommand.php index fa1814a89..3d003550d 100644 --- a/src/Command/Generate/ThemeCommand.php +++ b/src/Command/Generate/ThemeCommand.php @@ -195,7 +195,7 @@ protected function execute(InputInterface $input, OutputInterface $output) // Check that it is an absolute path or otherwise create an absolute path using appRoot $theme_path = $input->getOption('theme-path'); if(is_null($theme_path)) { - $uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST); + $uri = $this->site->getMultisiteName($input); $defaultThemePath = 'themes/custom'; $theme_path = $this->site->multisiteMode($uri)? 'sites/'.$this->site->getMultisiteDir($uri).'/'.$defaultThemePath : $defaultThemePath; } @@ -291,7 +291,7 @@ function ($machine_name) { $theme_path = $input->getOption('theme-path'); if (!$theme_path) { - $uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST); + $uri = $this->site->getMultisiteName($input); $defaultThemePath = 'themes/custom'; $theme_path = $this->getIo()->ask( $this->trans('commands.generate.theme.questions.theme-path'), diff --git a/src/Command/Generate/ThemeSettingCommand.php b/src/Command/Generate/ThemeSettingCommand.php index 303844d66..0cd4957cc 100644 --- a/src/Command/Generate/ThemeSettingCommand.php +++ b/src/Command/Generate/ThemeSettingCommand.php @@ -170,13 +170,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $theme = $this->validator->validateModuleName($input->getOption('theme')); $theme_path = $input->getOption('theme-path'); if (is_null($theme_path)) { - $uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST); + $uri = $this->site->getMultisiteName($input); $defaultThemePath = 'themes/custom'; $theme_path = $this->site->multisiteMode($uri)? 'sites/'.$this->site->getMultisiteDir($uri).'/'.$defaultThemePath : $defaultThemePath; } $theme_path = Path::isAbsolute($theme_path) ? $theme_path : Path::makeAbsolute($theme_path, $this->appRoot); $theme_path = $this->validator->validateModulePath($theme_path, true); - + $favicon = $input->getOption('favicon'); $commentUserPicture = $input->getOption('comment-user-picture'); $commentUserVerification = $input->getOption('comment-user-verification'); diff --git a/src/Command/Multisite/UpdateCommand.php b/src/Command/Multisite/UpdateCommand.php index 44b4af025..e7313d7ae 100644 --- a/src/Command/Multisite/UpdateCommand.php +++ b/src/Command/Multisite/UpdateCommand.php @@ -8,6 +8,7 @@ namespace Drupal\Console\Command\Multisite; use Drupal\Console\Core\Command\Command; +use Drupal\Console\Utils\Site; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -23,14 +24,21 @@ class UpdateCommand extends Command { protected $appRoot; + /** + * @var Site + */ + protected $site; + /** * DebugCommand constructor. * * @param $appRoot + * @param Site $site */ - public function __construct($appRoot) + public function __construct($appRoot, Site $site) { $this->appRoot = $appRoot; + $this->site = $site; parent::__construct(); } @@ -81,7 +89,7 @@ public function configure() */ protected function interact(InputInterface $input, OutputInterface $output) { - $this->uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST); + $this->uri = $this->site->getMultisiteName($input); $sites = $this->getMultisite($this->uri); if ($this->uri == "default") { @@ -113,7 +121,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->fs = new Filesystem(); if (empty($this->uri)) { - $uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST); + $uri = $this->site->getMultisiteName($input); $sites = $this->getMultisite($uri); $this->uri = $sites[$uri]; } diff --git a/src/Command/Site/InstallCommand.php b/src/Command/Site/InstallCommand.php index 98a5d8483..7d3f67619 100644 --- a/src/Command/Site/InstallCommand.php +++ b/src/Command/Site/InstallCommand.php @@ -287,7 +287,7 @@ function ($profile) { if ($dbType === 'sqlite') { // --db-file option if (!$input->getOption('db-file')) { - $uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST); + $uri = $this->site->getMultisiteName($input); $uriPath = $this->site->multisiteMode($uri) ? $this->site->getMultisiteDir($uri) : 'default'; $dbFile = $this->getIo()->ask( $this->trans('commands.migrate.execute.questions.db-file'), @@ -383,7 +383,7 @@ function ($profile) { */ protected function execute(InputInterface $input, OutputInterface $output) { - $uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST); + $uri = $this->site->getMultisiteName($input); if ($this->site->multisiteMode($uri)) { if (!$this->site->validMultisite($uri)) { diff --git a/src/Utils/Site.php b/src/Utils/Site.php index e473ae652..2bb0b222e 100644 --- a/src/Utils/Site.php +++ b/src/Utils/Site.php @@ -179,6 +179,21 @@ public function getAutoload() return include $autoLoadFile; } + /** + * @param InputInterface $input + * @return string + */ + public function getMultisiteName($input) + { + $uri = $input->getParameterOption(['--uri', '-l'], 'default'); + + if ($uri && !preg_match('/^(http|https):\/\//', $uri)) { + $uri = sprintf('http://%s', $uri); + } + + return parse_url($uri, PHP_URL_HOST); + } + /** * @return boolean */