|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DrupalGitCloneProject; |
| 4 | + |
| 5 | +use Composer\Package\Link; |
| 6 | +use Composer\Package\CompletePackage; |
| 7 | +use Composer\Plugin\PrePoolCreateEvent; |
| 8 | +use Composer\Semver\VersionParser; |
| 9 | + |
| 10 | +// Development: this makes symfony var-dumper work. |
| 11 | +// See https://github.com/composer/composer/issues/7911 |
| 12 | +// include_once './vendor/symfony/var-dumper/Resources/functions/dump.php'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Provides Composer scripts to allow installation on core 11.x. |
| 16 | + * |
| 17 | + * Inspired by mglaman/composer-drupal-lenient. |
| 18 | + */ |
| 19 | +class ComposerCoreVersionsLeniency { |
| 20 | + |
| 21 | + /** |
| 22 | + * Act on pre-pool-create event. |
| 23 | + * |
| 24 | + * This tweaks the core requirement of contrib modules, so that Composer will |
| 25 | + * install them when the Drupal core git clone is checked out to the 11.x |
| 26 | + * branch. |
| 27 | + * |
| 28 | + * @param \Composer\Plugin\PrePoolCreateEvent $event |
| 29 | + * The event. |
| 30 | + */ |
| 31 | + public static function prePoolCreate(PrePoolCreateEvent $event) { |
| 32 | + // @todo Only act if the current branch of drupal/core is 11.x |
| 33 | + |
| 34 | + $version_parser = new VersionParser(); |
| 35 | + |
| 36 | + $packages = $event->getPackages(); |
| 37 | + foreach ($packages as $package) { |
| 38 | + $type = $package->getType(); |
| 39 | + if ($type === 'drupal-core') { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + if ($type != 'drupal-module') { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + $requires = $package->getRequires(); |
| 48 | + |
| 49 | + if (!isset($requires['drupal/core'])) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + $drupal_core_require = $requires['drupal/core']; |
| 54 | + |
| 55 | + $pretty_constraint = $drupal_core_require->getPrettyConstraint(); |
| 56 | + if (str_contains($pretty_constraint, '^11')) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + // Make a new constraint with the 11 requirement appended. |
| 61 | + $pretty_constraint_with_11 = $pretty_constraint . ' || ^11'; |
| 62 | + $constraint_with_11 = $version_parser->parseConstraints($pretty_constraint_with_11); |
| 63 | + |
| 64 | + // Replace the existing core requirement with the new one. |
| 65 | + $requires['drupal/core'] = new Link( |
| 66 | + $drupal_core_require->getSource(), |
| 67 | + $drupal_core_require->getTarget(), |
| 68 | + $constraint_with_11, |
| 69 | + $drupal_core_require->getDescription(), |
| 70 | + $constraint_with_11->getPrettyString(), |
| 71 | + ); |
| 72 | + |
| 73 | + // No idea why we need to check CompletePackage, just aping |
| 74 | + // mglaman/composer-drupal-lenient. |
| 75 | + if ($package instanceof CompletePackage) { |
| 76 | + // @note `setRequires` is on Package but not PackageInterface. |
| 77 | + $package->setRequires($requires); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments