|
| 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\Tui; |
| 9 | + |
| 10 | +class CustomModules extends AbstractHandler { |
| 11 | + |
| 12 | + const BASE = 'base'; |
| 13 | + |
| 14 | + const DEMO = 'demo'; |
| 15 | + |
| 16 | + const SEARCH = 'search'; |
| 17 | + |
| 18 | + /** |
| 19 | + * {@inheritdoc} |
| 20 | + */ |
| 21 | + public function label(): string { |
| 22 | + return 'Custom modules'; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * {@inheritdoc} |
| 27 | + */ |
| 28 | + public static function description(array $responses): ?string { |
| 29 | + $base = Tui::bold('Base'); |
| 30 | + $demo = Tui::bold('Demo'); |
| 31 | + $search = Tui::bold('Search'); |
| 32 | + |
| 33 | + return <<<DOC |
| 34 | +Select which custom modules to include in your project: |
| 35 | +
|
| 36 | + ○ {$base} |
| 37 | + Starter module with common site utilities (mail handling, |
| 38 | + deploy hooks) and test scaffolding for Unit, Kernel, |
| 39 | + Functional, and FunctionalJavascript tests. |
| 40 | +
|
| 41 | + ○ {$search} |
| 42 | + Custom Solr search integration module. Requires the Solr |
| 43 | + service to be selected. |
| 44 | +
|
| 45 | + ○ {$demo} |
| 46 | + Demonstrates how Vortex tooling works: includes a counter |
| 47 | + block with CSS/JS, PHPUnit example tests across all test |
| 48 | + types, and a Behat feature. Safe to remove on real projects. |
| 49 | +DOC; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + public function hint(array $responses): ?string { |
| 56 | + return 'Use ⬆, ⬇ and Space bar to select one or more modules.'; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + */ |
| 62 | + public function options(array $responses): ?array { |
| 63 | + return [ |
| 64 | + self::BASE => 'Base - starter module with utilities and test scaffolding', |
| 65 | + self::SEARCH => 'Search - custom Solr search integration', |
| 66 | + self::DEMO => 'Demo - counter block and example tests to demonstrate tooling', |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritdoc} |
| 72 | + */ |
| 73 | + public function default(array $responses): null|string|bool|array { |
| 74 | + return [self::BASE, self::SEARCH, self::DEMO]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * {@inheritdoc} |
| 79 | + */ |
| 80 | + public function discover(): null|string|bool|array { |
| 81 | + if (!$this->isInstalled()) { |
| 82 | + return NULL; |
| 83 | + } |
| 84 | + |
| 85 | + // Discover the module prefix from the existing codebase. |
| 86 | + $prefix = $this->discoverModulePrefix(); |
| 87 | + |
| 88 | + if ($prefix === NULL) { |
| 89 | + return NULL; |
| 90 | + } |
| 91 | + |
| 92 | + $modules = []; |
| 93 | + |
| 94 | + $module_dir = $this->dstDir . '/' . $this->webroot . '/modules/custom'; |
| 95 | + |
| 96 | + if (is_dir($module_dir . '/' . $prefix . '_base')) { |
| 97 | + $modules[] = self::BASE; |
| 98 | + } |
| 99 | + |
| 100 | + if (is_dir($module_dir . '/' . $prefix . '_demo')) { |
| 101 | + $modules[] = self::DEMO; |
| 102 | + } |
| 103 | + |
| 104 | + if (is_dir($module_dir . '/' . $prefix . '_search')) { |
| 105 | + $modules[] = self::SEARCH; |
| 106 | + } |
| 107 | + |
| 108 | + sort($modules); |
| 109 | + |
| 110 | + return $modules; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * {@inheritdoc} |
| 115 | + */ |
| 116 | + public function process(): void { |
| 117 | + $selected = $this->getResponseAsArray(); |
| 118 | + $t = $this->tmpDir; |
| 119 | + $w = $this->webroot; |
| 120 | + |
| 121 | + // Safety net: if search was selected but Solr service was not, force-remove |
| 122 | + // search module since it cannot function without Solr. |
| 123 | + if (in_array(self::SEARCH, $selected) && isset($this->responses[Services::id()])) { |
| 124 | + $services = $this->responses[Services::id()]; |
| 125 | + if (is_array($services) && !in_array(Services::SOLR, $services)) { |
| 126 | + $selected = array_values(array_diff($selected, [self::SEARCH])); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (!in_array(self::BASE, $selected)) { |
| 131 | + File::removeTokenAsync('CUSTOM_MODULE_BASE'); |
| 132 | + |
| 133 | + $locations = [ |
| 134 | + $t . sprintf('/%s/modules/custom/*_base', $w), |
| 135 | + $t . sprintf('/%s/sites/all/modules/custom/*_base', $w), |
| 136 | + $t . sprintf('/%s/profiles/*/modules/*_base', $w), |
| 137 | + $t . sprintf('/%s/profiles/*/modules/custom/*_base', $w), |
| 138 | + $t . sprintf('/%s/profiles/custom/*/modules/*_base', $w), |
| 139 | + $t . sprintf('/%s/profiles/custom/*/modules/custom/*_base', $w), |
| 140 | + ]; |
| 141 | + |
| 142 | + $path = File::findMatchingPath($locations); |
| 143 | + if ($path) { |
| 144 | + File::remove($path); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (!in_array(self::DEMO, $selected)) { |
| 149 | + File::removeTokenAsync('CUSTOM_MODULE_DEMO'); |
| 150 | + |
| 151 | + $locations = [ |
| 152 | + $t . sprintf('/%s/modules/custom/*_demo', $w), |
| 153 | + $t . sprintf('/%s/sites/all/modules/custom/*_demo', $w), |
| 154 | + $t . sprintf('/%s/profiles/*/modules/*_demo', $w), |
| 155 | + $t . sprintf('/%s/profiles/*/modules/custom/*_demo', $w), |
| 156 | + $t . sprintf('/%s/profiles/custom/*/modules/*_demo', $w), |
| 157 | + $t . sprintf('/%s/profiles/custom/*/modules/custom/*_demo', $w), |
| 158 | + ]; |
| 159 | + |
| 160 | + $path = File::findMatchingPath($locations); |
| 161 | + if ($path) { |
| 162 | + File::remove($path); |
| 163 | + } |
| 164 | + |
| 165 | + File::remove($t . '/tests/behat/features/counter.feature'); |
| 166 | + } |
| 167 | + |
| 168 | + if (!in_array(self::SEARCH, $selected)) { |
| 169 | + File::removeTokenAsync('CUSTOM_MODULE_SEARCH'); |
| 170 | + |
| 171 | + $locations = [ |
| 172 | + $t . sprintf('/%s/modules/custom/*_search', $w), |
| 173 | + $t . sprintf('/%s/sites/all/modules/custom/*_search', $w), |
| 174 | + $t . sprintf('/%s/profiles/*/modules/*_search', $w), |
| 175 | + $t . sprintf('/%s/profiles/*/modules/custom/*_search', $w), |
| 176 | + $t . sprintf('/%s/profiles/custom/*/modules/*_search', $w), |
| 177 | + $t . sprintf('/%s/profiles/custom/*/modules/custom/*_search', $w), |
| 178 | + ]; |
| 179 | + |
| 180 | + $path = File::findMatchingPath($locations); |
| 181 | + if ($path) { |
| 182 | + File::remove($path); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Discover the module prefix from the existing codebase. |
| 189 | + * |
| 190 | + * @return string|null |
| 191 | + * The discovered module prefix, or NULL if not found. |
| 192 | + */ |
| 193 | + protected function discoverModulePrefix(): ?string { |
| 194 | + $locations = [ |
| 195 | + $this->dstDir . sprintf('/%s/modules/custom/*_base', $this->webroot), |
| 196 | + $this->dstDir . sprintf('/%s/modules/custom/*_core', $this->webroot), |
| 197 | + $this->dstDir . sprintf('/%s/sites/all/modules/custom/*_base', $this->webroot), |
| 198 | + $this->dstDir . sprintf('/%s/sites/all/modules/custom/*_core', $this->webroot), |
| 199 | + ]; |
| 200 | + |
| 201 | + $path = File::findMatchingPath($locations); |
| 202 | + |
| 203 | + return empty($path) ? NULL : str_replace(['_base', '_core'], '', basename($path)); |
| 204 | + } |
| 205 | + |
| 206 | +} |
| 207 | + |
0 commit comments