|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drupal\acquia_cms_site_studio\Plugin\ConfigAction; |
| 6 | + |
| 7 | +use Drupal\cohesion\Controller\AdministrationController; |
| 8 | +use Drupal\cohesion_sync\Services\PackageImportHandler; |
| 9 | +use Drupal\Core\Config\Action\Attribute\ConfigAction; |
| 10 | +use Drupal\Core\Config\Action\ConfigActionPluginInterface; |
| 11 | +use Drupal\Core\Config\ConfigFactoryInterface; |
| 12 | +use Drupal\Core\Extension\ModuleHandler; |
| 13 | +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
| 14 | +use Drupal\Core\Site\Settings; |
| 15 | +use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * The action to import base Site Studio packages. |
| 20 | + */ |
| 21 | +#[ConfigAction( |
| 22 | + id: 'basePackageImport', |
| 23 | + admin_label: new TranslatableMarkup('Import base Site Studio packages.'), |
| 24 | +)] |
| 25 | +final class BasePackageImport implements ConfigActionPluginInterface, ContainerFactoryPluginInterface { |
| 26 | + |
| 27 | + /** |
| 28 | + * Constructs a SimpleConfigUpdate object. |
| 29 | + * |
| 30 | + * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory |
| 31 | + * The config factory. |
| 32 | + * @param \Drupal\Core\Site\Settings $settings |
| 33 | + * The settings. |
| 34 | + * @param \Drupal\cohesion_sync\Services\PackageImportHandler $packageImportHandler |
| 35 | + * The package import handler. |
| 36 | + * @param \Drupal\Core\Extension\ModuleHandler $moduleHandler |
| 37 | + * The module handler. |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + protected readonly ConfigFactoryInterface $configFactory, |
| 41 | + protected readonly Settings $settings, |
| 42 | + protected readonly PackageImportHandler $packageImportHandler, |
| 43 | + protected readonly ModuleHandler $moduleHandler, |
| 44 | + ) { |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + */ |
| 50 | + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { |
| 51 | + return new static( |
| 52 | + $container->get('config.factory'), |
| 53 | + $container->get('settings'), |
| 54 | + $container->get('cohesion_sync.package_import_handler'), |
| 55 | + $container->get('module_handler'), |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + */ |
| 62 | + public function apply(string $configName, mixed $value): void { |
| 63 | + if ($configName === 'cohesion.settings' && $value) { |
| 64 | + // Update the configuration with the API and organization keys. |
| 65 | + $this->updateConfig($configName); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Updates the configuration with API and organization keys. |
| 71 | + * |
| 72 | + * @param string $configName |
| 73 | + * The name of the configuration. |
| 74 | + */ |
| 75 | + private function updateConfig(string $configName): void { |
| 76 | + $config = $this->configFactory->get($configName); |
| 77 | + $apiKey = $config->get('api_key'); |
| 78 | + $orgKey = $config->get('organization_key'); |
| 79 | + |
| 80 | + if (!($apiKey && $orgKey)) { |
| 81 | + $apiKey = getenv('SITESTUDIO_API_KEY') ?? $this->settings->get('cohesion.settings')->get('api_key'); |
| 82 | + $orgKey = getenv('SITESTUDIO_ORG_KEY') ?? $this->settings->get('cohesion.settings')->get('organization_key'); |
| 83 | + if (!($apiKey && $orgKey)) { |
| 84 | + return; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + $this->configFactory->getEditable($configName) |
| 89 | + ->set('api_key', $apiKey) |
| 90 | + ->set('organization_key', $orgKey) |
| 91 | + ->save(TRUE); |
| 92 | + |
| 93 | + // Import the base packages. |
| 94 | + $this->importBasePackages(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Imports the base Site Studio packages. |
| 99 | + */ |
| 100 | + private function importBasePackages(): void { |
| 101 | + $this->initializeBatch(); |
| 102 | + $package_list_path = $this->getPackageListPath(); |
| 103 | + $this->importPackages($package_list_path); |
| 104 | + $this->processBatchIfCli(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Initializes the batch process for importing packages. |
| 109 | + */ |
| 110 | + private function initializeBatch(): void { |
| 111 | + batch_set(AdministrationController::batchAction(TRUE)); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Gets the path to the package list file. |
| 116 | + * |
| 117 | + * @return string |
| 118 | + * The path to the package list file. |
| 119 | + */ |
| 120 | + private function getPackageListPath(): string { |
| 121 | + $module_path = $this->moduleHandler->getModule('acquia_cms_site_studio')->getPath(); |
| 122 | + return $module_path . '/config/site_studio/site_studio.packages.yml'; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Imports packages from the specified path. |
| 127 | + * |
| 128 | + * @param string $package_list_path |
| 129 | + * The path to the package list file. |
| 130 | + */ |
| 131 | + private function importPackages(string $package_list_path): void { |
| 132 | + $this->packageImportHandler->importPackagesFromPath($package_list_path); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Processes the batch if running in CLI mode. |
| 137 | + */ |
| 138 | + private function processBatchIfCli(): void { |
| 139 | + if (PHP_SAPI === 'cli') { |
| 140 | + drush_backend_batch_process(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments