Skip to content

Commit 16a4227

Browse files
author
catch
committed
Issue #3126566 by greg.1.anderson, jungle, tedbow, hussainweb, Kristen Pol, alexpott: Allow Drupal to work with Composer 2
1 parent e2c61c7 commit 16a4227

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

AllowedPackages.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Drupal\Composer\Plugin\Scaffold;
44

55
use Composer\Composer;
6+
use Composer\DependencyResolver\Operation\OperationInterface;
67
use Composer\Installer\PackageEvent;
78
use Composer\IO\IOInterface;
89
use Composer\Package\PackageInterface;
@@ -96,8 +97,12 @@ public function getAllowedPackages() {
9697
*/
9798
public function event(PackageEvent $event) {
9899
$operation = $event->getOperation();
99-
// Determine the package.
100-
$package = $operation->getJobType() == 'update' ? $operation->getTargetPackage() : $operation->getPackage();
100+
// Determine the package. Later, in evaluateNewPackages(), we will report
101+
// which of the newly-installed packages have scaffold operations, and
102+
// whether or not they are allowed to scaffold by the allowed-packages
103+
// option in the root-level composer.json file.
104+
$operationType = $this->getOperationType($operation);
105+
$package = $operationType === 'update' ? $operation->getTargetPackage() : $operation->getPackage();
101106
if (ScaffoldOptions::hasOptions($package->getExtra())) {
102107
$this->newPackages[$package->getName()] = $package;
103108
}
@@ -176,6 +181,26 @@ protected function evaluateNewPackages(array $allowed_packages) {
176181
return $allowed_packages;
177182
}
178183

184+
/**
185+
* Determine the type of the provided operation.
186+
*
187+
* Adjusts API used for Composer 1 or Composer 2.
188+
*
189+
* @param \Composer\DependencyResolver\Operation\OperationInterface $operation
190+
* The operation object.
191+
*
192+
* @return string
193+
* The operation type.
194+
*/
195+
protected function getOperationType(OperationInterface $operation) {
196+
// Use Composer 2 method.
197+
if (method_exists($operation, 'getOperationType')) {
198+
return $operation->getOperationType();
199+
}
200+
// Fallback to Composer 1 method.
201+
return $operation->getJobType();
202+
}
203+
179204
/**
180205
* Retrieves a package from the current composer process.
181206
*

Plugin.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ public function activate(Composer $composer, IOInterface $io) {
6060
$this->requireWasCalled = FALSE;
6161
}
6262

63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function deactivate(Composer $composer, IOInterface $io) {
67+
}
68+
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
public function uninstall(Composer $composer, IOInterface $io) {
73+
}
74+
6375
/**
6476
* {@inheritdoc}
6577
*/

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"homepage": "https://www.drupal.org/project/drupal",
77
"license": "GPL-2.0-or-later",
88
"require": {
9-
"composer-plugin-api": "^1.0.0",
9+
"composer-plugin-api": "^1 || ^2",
1010
"php": ">=7.0.8"
1111
},
1212
"conflict": {

0 commit comments

Comments
 (0)