diff --git a/src/Drush/Commands/HooksDrushCommands.php b/src/Drush/Commands/HooksDrushCommands.php new file mode 100644 index 0000000..87e8123 --- /dev/null +++ b/src/Drush/Commands/HooksDrushCommands.php @@ -0,0 +1,36 @@ +get('bootstrap.manager') + ); + } + + /** + * Execute code before pre-validate site:install. + */ + #[CLI\Hook(type: HookManager::PRE_ARGUMENT_VALIDATOR, target: 'site-install')] + public function preValidateSiteInstall(CommandData $commandData): void { + if ($this->invokeHook(self::VALIDATE_GENERATE_SETTINGS)) { + $uri = $commandData->input()->getOption('uri') ?? 'default'; + $sitesSubdir = $this->getSitesSubdirFromUri(DRUPAL_ROOT, $uri); + $commandData->input()->setOption('sites-subdir', $sitesSubdir); + $options = $commandData->options(); + $this->bootstrapManager->setUri('http://' . $sitesSubdir); + + // Try to get any already configured database information. + $this->bootstrapManager->bootstrapMax(DrupalBootLevels::CONFIGURATION, $commandData->annotationData()); + + // By default, bootstrap manager boot site from default/setting.php + // hence remove the database connection if site is other than default. + if (($sitesSubdir && "sites/$sitesSubdir" !== $this->bootstrapManager->bootstrap()->confpath())) { + Database::removeConnection('default'); + $db = [ + 'database' => 'drupal', + 'username' => 'drupal', + 'password' => 'drupal', + 'host' => 'localhost', + 'port' => '3306', + ]; + $dbSpec = [ + "drupal" => ["db" => $db] + ]; + if (!($options['db-url'])) { + if (EnvironmentDetector::isLocalEnv()) { + $db = $this->askDbCredentials($sitesSubdir, $db); + $dbSpec["drupal"]["db"] = $db; + } + $commandData->input()->setOption("db-url", + "mysql://" . $db['username'] . ":" . $db['password'] . "@" . $db['host'] . ":" . $db['port'] . "/" . $db['database'] + ); + } + $settings = new Settings(DRUPAL_ROOT, $sitesSubdir); + try { + $settings->generate($dbSpec); + $this->invokeHook(self::POST_GENERATE_SETTINGS); + } + catch (SettingsException $e) { + $this->io()->warning($e->getMessage()); + } + } + } + } + + + /** + * Function to check if multisite should be setup or not. + * + * @return bool + */ + protected function invokeHook(string $eventName): bool { + $handlers = $this->getCustomEventHandlers($eventName); + $status = TRUE; + foreach ($handlers as $handler) { + $status = $handler(); + if (!$status) { + return FALSE; + } + } + return $status; + } + + /** + * Get local database specs. + * + * @param string $site_name + * The site name. + * + * @return array + * The database specs. + */ + private function askDbCredentials(string $site_name, array $defaultCredentials): array { + $shouldAsk = $this->io()->confirm(dt("Would you like to configure the local database credentials?")); + $credentials = $defaultCredentials; + if ($shouldAsk) { + $credentials['database'] = $this->io()->ask("Local database name", $site_name); + $credentials['username'] = $this->io()->ask("Local database user", $credentials['username']); + $credentials['password'] = $this->io()->ask("Local database password", $credentials['password']); + $credentials['host'] = $this->io()->ask("Local database host", $credentials['host']); + $credentials['port'] = $this->io()->ask("Local database port", $credentials['port']); + } + return $credentials; + } + +} diff --git a/src/Drush/Traits/SiteUriTrait.php b/src/Drush/Traits/SiteUriTrait.php new file mode 100644 index 0000000..f9f26a6 --- /dev/null +++ b/src/Drush/Traits/SiteUriTrait.php @@ -0,0 +1,51 @@ +