Skip to content

Commit cafb1a5

Browse files
committed
Added Composer script to allow contrib modules to be installed on 11.x. Fixes #23.
1 parent 23cc900 commit cafb1a5

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
],
6767
"post-drupal-scaffold-cmd": [
6868
"DrupalGitCloneProject\\ComposerScripts::postDrupalScaffold"
69+
],
70+
"pre-pool-create": [
71+
"DrupalGitCloneProject\\ComposerCoreVersionsLeniency::prePoolCreate"
6972
]
7073
},
7174
"autoload": {

src/ComposerCoreVersionsLeniency.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

src/ComposerScripts.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use Composer\Script\Event;
66

7+
// Development: this makes symfony var-dumper work.
8+
// See https://github.com/composer/composer/issues/7911
9+
// include_once './vendor/symfony/var-dumper/Resources/functions/dump.php';
10+
711
/**
812
* Provides Composer scripts for setting up Drupal from a git clone.
913
*/

0 commit comments

Comments
 (0)