-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ACMS-4234: Add config action to import core site studio packages.
- Loading branch information
1 parent
ca42150
commit c8ac4e3
Showing
1 changed file
with
144 additions
and
0 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
modules/acquia_cms_site_studio/src/Plugin/ConfigAction/BasePackageImport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\acquia_cms_site_studio\Plugin\ConfigAction; | ||
|
||
use Drupal\cohesion\Controller\AdministrationController; | ||
use Drupal\cohesion_sync\Services\PackageImportHandler; | ||
use Drupal\Core\Config\Action\Attribute\ConfigAction; | ||
use Drupal\Core\Config\Action\ConfigActionPluginInterface; | ||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Extension\ModuleHandler; | ||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | ||
use Drupal\Core\Site\Settings; | ||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* The action to import base Site Studio packages. | ||
*/ | ||
#[ConfigAction( | ||
id: 'basePackageImport', | ||
admin_label: new TranslatableMarkup('Import base Site Studio packages.'), | ||
)] | ||
final class BasePackageImport implements ConfigActionPluginInterface, ContainerFactoryPluginInterface { | ||
|
||
/** | ||
* Constructs a SimpleConfigUpdate object. | ||
* | ||
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | ||
* The config factory. | ||
* @param \Drupal\Core\Site\Settings $settings | ||
* The settings. | ||
* @param \Drupal\cohesion_sync\Services\PackageImportHandler $packageImportHandler | ||
* The package import handler. | ||
* @param \Drupal\Core\Extension\ModuleHandler $moduleHandler | ||
* The module handler. | ||
*/ | ||
public function __construct( | ||
protected readonly ConfigFactoryInterface $configFactory, | ||
protected readonly Settings $settings, | ||
protected readonly PackageImportHandler $packageImportHandler, | ||
protected readonly ModuleHandler $moduleHandler, | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { | ||
return new static( | ||
$container->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(); | ||
} | ||
} | ||
|
||
} |