From c8ac4e39e1cd61c32c207797fe1f39fefff344aa Mon Sep 17 00:00:00 2001 From: rajeshreeputra Date: Thu, 9 Jan 2025 12:00:33 +0530 Subject: [PATCH] ACMS-4234: Add config action to import core site studio packages. --- .../Plugin/ConfigAction/BasePackageImport.php | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 modules/acquia_cms_site_studio/src/Plugin/ConfigAction/BasePackageImport.php diff --git a/modules/acquia_cms_site_studio/src/Plugin/ConfigAction/BasePackageImport.php b/modules/acquia_cms_site_studio/src/Plugin/ConfigAction/BasePackageImport.php new file mode 100644 index 000000000..1cf6525f1 --- /dev/null +++ b/modules/acquia_cms_site_studio/src/Plugin/ConfigAction/BasePackageImport.php @@ -0,0 +1,144 @@ +get('config.factory'), + $container->get('settings'), + $container->get('cohesion_sync.package_import_handler'), + $container->get('module_handler'), + ); + } + + /** + * {@inheritdoc} + */ + public function apply(string $configName, mixed $value): void { + if ($configName === 'cohesion.settings' && $value) { + // Update the configuration with the API and organization keys. + $this->updateConfig($configName); + } + } + + /** + * Updates the configuration with API and organization keys. + * + * @param string $configName + * The name of the configuration. + */ + private function updateConfig(string $configName): void { + $config = $this->configFactory->get($configName); + $apiKey = $config->get('api_key'); + $orgKey = $config->get('organization_key'); + + if (!($apiKey && $orgKey)) { + $apiKey = getenv('SITESTUDIO_API_KEY') ?? $this->settings->get('cohesion.settings')->get('api_key'); + $orgKey = getenv('SITESTUDIO_ORG_KEY') ?? $this->settings->get('cohesion.settings')->get('organization_key'); + if (!($apiKey && $orgKey)) { + return; + } + } + + $this->configFactory->getEditable($configName) + ->set('api_key', $apiKey) + ->set('organization_key', $orgKey) + ->save(TRUE); + + // Import the base packages. + $this->importBasePackages(); + } + + /** + * Imports the base Site Studio packages. + */ + private function importBasePackages(): void { + $this->initializeBatch(); + $package_list_path = $this->getPackageListPath(); + $this->importPackages($package_list_path); + $this->processBatchIfCli(); + } + + /** + * Initializes the batch process for importing packages. + */ + private function initializeBatch(): void { + batch_set(AdministrationController::batchAction(TRUE)); + } + + /** + * Gets the path to the package list file. + * + * @return string + * The path to the package list file. + */ + private function getPackageListPath(): string { + $module_path = $this->moduleHandler->getModule('acquia_cms_site_studio')->getPath(); + return $module_path . '/config/site_studio/site_studio.packages.yml'; + } + + /** + * Imports packages from the specified path. + * + * @param string $package_list_path + * The path to the package list file. + */ + private function importPackages(string $package_list_path): void { + $this->packageImportHandler->importPackagesFromPath($package_list_path); + } + + /** + * Processes the batch if running in CLI mode. + */ + private function processBatchIfCli(): void { + if (PHP_SAPI === 'cli') { + drush_backend_batch_process(); + } + } + +}