|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DrevOps\VortexInstaller\Prompts\Handlers; |
| 6 | + |
| 7 | +use DrevOps\VortexInstaller\Utils\File; |
| 8 | +use DrevOps\VortexInstaller\Utils\JsonManipulator; |
| 9 | + |
| 10 | +class Modules extends AbstractHandler { |
| 11 | + |
| 12 | + /** |
| 13 | + * {@inheritdoc} |
| 14 | + */ |
| 15 | + public function label(): string { |
| 16 | + return 'Modules'; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * {@inheritdoc} |
| 21 | + */ |
| 22 | + public function hint(array $responses): ?string { |
| 23 | + return 'Use ⬆, ⬇ and Space bar to select one or more modules.'; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + public function options(array $responses): ?array { |
| 30 | + return static::getAvailableModules(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * {@inheritdoc} |
| 35 | + */ |
| 36 | + public function default(array $responses): null|string|bool|array { |
| 37 | + // Default to all modules selected (meaning none will be removed). |
| 38 | + return array_keys(static::getAvailableModules()); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + public function discover(): null|string|bool|array { |
| 45 | + if (!$this->isInstalled()) { |
| 46 | + return NULL; |
| 47 | + } |
| 48 | + |
| 49 | + $composer_file = $this->dstDir . '/composer.json'; |
| 50 | + $discovered_modules = $this->getModulesFromComposerFile($composer_file); |
| 51 | + |
| 52 | + if ($discovered_modules === NULL) { |
| 53 | + return NULL; |
| 54 | + } |
| 55 | + |
| 56 | + // Filter discovered modules to only include those in our available list. |
| 57 | + $available_modules = array_keys(static::getAvailableModules()); |
| 58 | + $modules = array_intersect($discovered_modules, $available_modules); |
| 59 | + |
| 60 | + sort($modules); |
| 61 | + |
| 62 | + return $modules; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritdoc} |
| 67 | + */ |
| 68 | + public function process(): void { |
| 69 | + $selected_modules = $this->getResponseAsArray(); |
| 70 | + $all_modules = static::getAvailableModules(); |
| 71 | + |
| 72 | + $t = $this->tmpDir; |
| 73 | + $w = $this->webroot; |
| 74 | + |
| 75 | + // Process each module that was NOT selected (remove them). |
| 76 | + foreach (array_keys($all_modules) as $module_name) { |
| 77 | + if (!in_array($module_name, $selected_modules)) { |
| 78 | + // Remove from composer.json. |
| 79 | + $pattern = '/\s*"drupal\/' . preg_quote($module_name, '/') . '":\s*"[^\"]+",?\n/'; |
| 80 | + File::replaceContentInFile($t . '/composer.json', $pattern, "\n"); |
| 81 | + |
| 82 | + // Remove module from settings file. |
| 83 | + File::remove($t . '/' . $w . '/sites/default/includes/modules/settings.' . $module_name . '.php'); |
| 84 | + |
| 85 | + // Remove module from the provision example file. |
| 86 | + File::replaceContentCallbackInFile($t . '/scripts/custom/provision-10-example.sh', function (string $content) use ($module_name): string { |
| 87 | + $pattern = '/^(\s*)(drush\s+pm:install.*\b' . preg_quote($module_name, '/') . '\b.*)$/m'; |
| 88 | + $content = preg_replace_callback($pattern, function (array $matches) use ($module_name): string { |
| 89 | + $indent = $matches[1]; |
| 90 | + $line = $matches[2]; |
| 91 | + $line = preg_replace('/\s+\b' . preg_quote($module_name, '/') . '\b/', '', $line); |
| 92 | + $line = preg_replace('/\s{2,}/', ' ', $line) ?? $line; |
| 93 | + return $indent . $line; |
| 94 | + }, $content); |
| 95 | + return $content ?? ''; |
| 96 | + }); |
| 97 | + |
| 98 | + // Remove module from the Behat tests. |
| 99 | + File::remove($t . '/tests/behat/features/' . $module_name . '.feature'); |
| 100 | + |
| 101 | + // Remove module from the config tests. |
| 102 | + $pattern = '/\s*\$config\[\'' . preg_quote($module_name, '/') . '\..*;(\r?\n)?/'; |
| 103 | + File::removeLine($t . '/tests/phpunit/Drupal/EnvironmentSettingsTest.php', $pattern); |
| 104 | + |
| 105 | + // Remove module tokens. |
| 106 | + File::removeTokenAsync('MODULE_' . strtoupper($module_name)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (count($selected_modules) === 0) { |
| 111 | + File::removeTokenAsync('MODULE'); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get the full list of available Drupal contributed modules. |
| 117 | + * |
| 118 | + * This list excludes Drupal core modules and service modules. |
| 119 | + * |
| 120 | + * @return array<string, string> |
| 121 | + * Array of module machine names as keys and labels as values. |
| 122 | + */ |
| 123 | + public static function getAvailableModules(): array { |
| 124 | + return [ |
| 125 | + 'admin_toolbar' => 'Admin toolbar', |
| 126 | + 'coffee' => 'Coffee', |
| 127 | + 'config_split' => 'Config split', |
| 128 | + 'config_update' => 'Config update', |
| 129 | + 'environment_indicator' => 'Environment indicator', |
| 130 | + 'pathauto' => 'Pathauto', |
| 131 | + 'redirect' => 'Redirect', |
| 132 | + 'robotstxt' => 'Robots.txt', |
| 133 | + 'seckit' => 'Seckit', |
| 134 | + 'shield' => 'Shield', |
| 135 | + 'stage_file_proxy' => 'Stage file proxy', |
| 136 | + ]; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Extract Drupal contributed module names from a composer.json file. |
| 141 | + * |
| 142 | + * @param string $composer_file |
| 143 | + * Path to the composer.json file. |
| 144 | + * |
| 145 | + * @return array|null |
| 146 | + * Array of module machine names (without drupal/ prefix), or NULL on error. |
| 147 | + */ |
| 148 | + protected function getModulesFromComposerFile(string $composer_file): ?array { |
| 149 | + if (!file_exists($composer_file)) { |
| 150 | + return NULL; |
| 151 | + } |
| 152 | + |
| 153 | + $cj = JsonManipulator::fromFile($composer_file); |
| 154 | + |
| 155 | + if (!$cj instanceof JsonManipulator) { |
| 156 | + return NULL; |
| 157 | + } |
| 158 | + |
| 159 | + $require = $cj->getProperty('require'); |
| 160 | + |
| 161 | + if (!is_array($require)) { |
| 162 | + return NULL; |
| 163 | + } |
| 164 | + |
| 165 | + $modules = []; |
| 166 | + foreach (array_keys($require) as $package) { |
| 167 | + // Only include drupal/* packages, excluding core packages. |
| 168 | + if (str_starts_with((string) $package, 'drupal/') && !str_starts_with((string) $package, 'drupal/core-')) { |
| 169 | + // Extract module name (remove drupal/ prefix). |
| 170 | + $module_name = substr((string) $package, 7); |
| 171 | + $modules[] = $module_name; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return $modules; |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments