|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ComposerDrupalLenient\Drupal; |
| 6 | + |
| 7 | +use Composer\InstalledVersions; |
| 8 | +use Drupal\Core\Extension\Extension; |
| 9 | + |
| 10 | +/** |
| 11 | + * Implements hook_system_info_alter() to bypass core_version_requirement checks. |
| 12 | + * |
| 13 | + * For any extension that appears in the project's drupal-lenient allowed list |
| 14 | + * (or when allow-all is set), core_incompatible is set to FALSE so that the |
| 15 | + * Drupal UI and Drush can install the extension regardless of its declared |
| 16 | + * core_version_requirement. |
| 17 | + */ |
| 18 | +class LenientHooks |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Cached lenient configuration read from the root composer.json. |
| 22 | + * |
| 23 | + * NULL means the config has not been read yet. FALSE means no config was |
| 24 | + * found. An array contains the parsed 'drupal-lenient' extra config. |
| 25 | + * |
| 26 | + * @var array<string, mixed>|false|null |
| 27 | + */ |
| 28 | + private static array|false|null $config = null; |
| 29 | + |
| 30 | + /** |
| 31 | + * Implements hook_system_info_alter(). |
| 32 | + * |
| 33 | + * @param array<string, mixed> $info |
| 34 | + * The extension info array from its .info.yml file. |
| 35 | + * @param \Drupal\Core\Extension\Extension $file |
| 36 | + * The extension object. |
| 37 | + * @param string $type |
| 38 | + * The type of extension ('module', 'theme', etc.). |
| 39 | + */ |
| 40 | + public function systemInfoAlter(array &$info, Extension $file, string $type): void |
| 41 | + { |
| 42 | + // Already marked compatible, nothing to do. |
| 43 | + if (($info['core_incompatible'] ?? true) === false) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + $config = self::getLenientConfig(); |
| 48 | + if ($config === false) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if ((bool) ($config['allow-all'] ?? false)) { |
| 53 | + $info['core_incompatible'] = false; |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + $allowedList = $config['allowed-list'] ?? []; |
| 58 | + if (!is_array($allowedList)) { |
| 59 | + return; |
| 60 | + } |
| 61 | + $packageName = 'drupal/' . $file->getName(); |
| 62 | + if (in_array($packageName, $allowedList, true)) { |
| 63 | + $info['core_incompatible'] = false; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the drupal-lenient config from the root composer.json. |
| 69 | + * |
| 70 | + * @return array<string, mixed>|false |
| 71 | + * The config array or FALSE if not found. |
| 72 | + */ |
| 73 | + private static function getLenientConfig(): array|false |
| 74 | + { |
| 75 | + if (self::$config !== null) { |
| 76 | + return self::$config; |
| 77 | + } |
| 78 | + |
| 79 | + $rootDir = self::findRootDir(); |
| 80 | + if ($rootDir === null) { |
| 81 | + self::$config = false; |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + $composerJsonPath = $rootDir . '/composer.json'; |
| 86 | + if (!file_exists($composerJsonPath)) { |
| 87 | + self::$config = false; |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + $contents = file_get_contents($composerJsonPath); |
| 92 | + if ($contents === false) { |
| 93 | + self::$config = false; |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + $data = json_decode($contents, true, 512, JSON_THROW_ON_ERROR); |
| 99 | + } catch (\JsonException) { |
| 100 | + self::$config = false; |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + if (!is_array($data)) { |
| 105 | + self::$config = false; |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + /** @var array<string, mixed> $data */ |
| 110 | + $extra = $data['extra'] ?? null; |
| 111 | + if (!is_array($extra)) { |
| 112 | + self::$config = false; |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + $lenient = $extra['drupal-lenient'] ?? false; |
| 117 | + /** @var array<string, mixed>|false $lenientConfig */ |
| 118 | + $lenientConfig = is_array($lenient) ? $lenient : false; |
| 119 | + self::$config = $lenientConfig; |
| 120 | + return self::$config; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Locates the Composer project root directory. |
| 125 | + * |
| 126 | + * Uses InstalledVersions to find this package's install path, then walks |
| 127 | + * up three levels ({vendor-dir}/{vendor}/{package} → root). This handles |
| 128 | + * custom vendor-dir settings correctly, unlike a fixed dirname() count |
| 129 | + * from __DIR__. |
| 130 | + */ |
| 131 | + private static function findRootDir(): ?string |
| 132 | + { |
| 133 | + $installPath = InstalledVersions::getInstallPath('mglaman/composer-drupal-lenient'); |
| 134 | + if ($installPath !== null) { |
| 135 | + return dirname($installPath, 3); |
| 136 | + } |
| 137 | + |
| 138 | + return null; |
| 139 | + } |
| 140 | +} |
0 commit comments