Skip to content

Commit 243946b

Browse files
COMOPS-1503: Updating to support Composer 2
1 parent 44fd2e8 commit 243946b

File tree

8 files changed

+171
-61
lines changed

8 files changed

+171
-61
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"AFL-3.0"
77
],
88
"require": {
9-
"composer/composer": "<=1.10.15",
10-
"composer-plugin-api": "^1.0"
9+
"composer/composer": "<=1.10.19 || >=2.0.0 <=2.0.8",
10+
"composer-plugin-api": "^1.0 || ^2.0"
1111
},
1212
"require-dev": {
1313
"phpunit/phpunit": "~6.5.0"

docs/class_descriptions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ This class manages the plugin's self-installation inside the `var` directory to
131131
- **`packageEvent()`**
132132
- When Composer installs or updates a required package, this method checks whether it was the plugin package that changed and calls `updateSetupWizardPlugin()` with the new version if so
133133
- Triggered by the events defined in [PluginDefinition::getSubscribedEvents()](#plugindefinition)
134+
- **`processEvent()`**
135+
- Helper method used by `packageEvent()` to run `updateSetupWizardPlugin()` when an appropriate [PackageEvent](https://getcomposer.org/apidoc/master/Composer/Installer/PackageEvent.html) is fired
134136
- **`doVarInstall()`**
135137
- Checks the `composer.lock` file the plugin and calls `updateSetupWizardPlugin()` with the version found there
136138
- Called by `composer magento-update-plugin install` and the Magento module setup classes ([InstallData](#installdatarecurringdataupgradedata), [RecurringData](#installdatarecurringdataupgradedata), [UpgradeData](#installdatarecurringdataupgradedata))

src/Magento/ComposerRootUpdatePlugin/Plugin/PluginDefinition.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ public function activate(Composer $composer, IOInterface $io)
3232
// Method must exist
3333
}
3434

35+
/**
36+
* @inheritdoc
37+
*/
38+
public function deactivate(Composer $composer, IOInterface $io)
39+
{
40+
// Method must exist
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function uninstall(Composer $composer, IOInterface $io)
47+
{
48+
// Method must exist
49+
}
50+
3551
/**
3652
* @inheritdoc
3753
*/

src/Magento/ComposerRootUpdatePlugin/README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44

55
The `magento/composer-root-update-plugin` Composer plugin resolves changes that need to be made to the root project `composer.json` file before updating to a new Magento product requirement.
66

7-
This is accomplished by comparing the root `composer.json` file for the Magento project corresponding to the Magento version and edition in the current installation with the Magento project `composer.json` file for the target Magento product package when the `composer require` command runs and applying any deltas found between the two files if they do not conflict with the existing `composer.json` file in the Magento root directory.
7+
This is accomplished by comparing the root `composer.json` file for the Magento project corresponding to the Magento version and edition in the current installation with the Magento project `composer.json` file for the target Magento product or cloud metapackage when the `composer require` command runs and applying any deltas found between the two files if they do not conflict with the existing `composer.json` file in the Magento root directory.
88

99
# Getting Started
1010

1111
## System requirements
1212

13-
The `magento/composer-root-update-plugin` package requires Composer version 1.8.0 or earlier. Compatibility with newer Composer versions will be tested and added in future plugin versions.
13+
The `magento/composer-root-update-plugin` package requires Composer version 1.10.19 or earlier, or version 2.0.0 - 2.0.8. Compatibility with newer Composer versions will be tested and added in future plugin versions.
1414

1515
## Installation
1616

1717
To install the plugin, run the following commands in the Magento root directory.
1818

19-
composer require magento/composer-root-update-plugin ~0.1 --no-update
19+
composer require magento/composer-root-update-plugin ~1.1 --no-update
2020
composer update
2121

2222
# Usage
2323

24-
The plugin adds functionality to the `composer require` command when a new Magento product package is required, and in most cases will not need additional options or commands run to function.
24+
The plugin adds functionality to the `composer require` command when a new Magento product or cloud metapackage is required, and in most cases will not need additional options or commands run to function.
2525

2626
If the `composer require` command for the target Magento package fails, one of the following may be necessary.
2727

@@ -33,9 +33,11 @@ In this case, run the following command with the appropriate values to correct t
3333

3434
composer require <current_Magento_package> <current_version> --base-magento-edition '<Open Source|Commerce>' --base-magento-version <original_Magento_version>
3535

36+
These options are not valid for Magento Cloud installations.
37+
3638
## Conflicting custom values
3739

38-
If the `composer.json` file has custom changes that do not match the values the plugin expects according to the installed Magento product, the entries may need to be corrected to values compatible with the target Magento package.
40+
If the `composer.json` file has custom changes that do not match the values the plugin expects according to the installed Magento metapackage, the entries may need to be corrected to values compatible with the target Magento version.
3941

4042
To resolve these conflicts interactively, re-run the `composer require` command with the `--interactive-magento-conflicts` option.
4143

@@ -128,23 +130,23 @@ For reference, these are the `"require"` and `"require-dev"` sections for defaul
128130

129131
### With `magento/composer-root-update-plugin`:
130132

131-
In the project directory for a Magento Open Source 2.2.8 installation, a user runs `composer require magento/composer-root-update-plugin ~0.1 --no-update` and `composer update` before the Magento Open Source 2.3.1 upgrade commands.
133+
In the project directory for a Magento Open Source 2.2.8 installation, a user runs `composer require magento/composer-root-update-plugin ~1.1 --no-update` and `composer update` before the Magento Open Source 2.3.1 upgrade commands.
132134

133135
```
134-
$ composer require magento/composer-root-update-plugin ~0.1 --no-update
136+
$ composer require magento/composer-root-update-plugin ~1.1 --no-update
135137
./composer.json has been updated
136138
$ composer update
137139
Loading composer repositories with package information
138140
Updating dependencies (including require-dev)
139141
Package operations: 1 install, 0 updates, 0 removals
140-
- Installing magento/composer-root-update-plugin (0.1.0): Downloading (100%)
141-
Installing "magento/composer-root-update-plugin: 0.1.0" for the Web Setup Wizard
142+
- Installing magento/composer-root-update-plugin (1.1.0): Downloading (100%)
143+
Installing "magento/composer-root-update-plugin: 1.1.0" for the Web Setup Wizard
142144
Loading composer repositories with package information
143145
Updating dependencies
144146
Package operations: 18 installs, 0 updates, 0 removals
145147
- Installing ...
146148
...
147-
- Installing magento/composer-root-update-plugin (0.1.0): Downloading (100%)
149+
- Installing magento/composer-root-update-plugin (1.1.0): Downloading (100%)
148150
Writing lock file
149151
Generating autoload files
150152
Writing lock file
@@ -191,7 +193,7 @@ For reference, these are the `"require"` and `"require-dev"` sections from the `
191193
```
192194
"require": {
193195
"magento/product-community-edition": "2.3.1",
194-
"magento/composer-root-update-plugin": "~0.1"
196+
"magento/composer-root-update-plugin": "~1.1"
195197
},
196198
"require-dev": {
197199
"allure-framework/allure-phpunit": "~1.2.0",

src/Magento/ComposerRootUpdatePlugin/Setup/WebSetupWizardPluginInstaller.php

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Composer\Installer;
1313
use Composer\Installer\PackageEvent;
1414
use Composer\Json\JsonFile;
15+
use Composer\Package\PackageInterface;
16+
use Composer\Plugin\PluginInterface;
1517
use Exception;
1618
use Magento\ComposerRootUpdatePlugin\Utils\Console;
1719
use Magento\ComposerRootUpdatePlugin\Utils\PackageUtils;
@@ -52,26 +54,52 @@ public function __construct($console)
5254
*/
5355
public function packageEvent($event)
5456
{
55-
$jobs = $event->getRequest()->getJobs();
5657
$packageName = PluginDefinition::PACKAGE_NAME;
57-
foreach ($jobs as $job) {
58-
if (key_exists('packageName', $job) && $job['packageName'] === $packageName) {
59-
$pkg = $event->getInstalledRepo()->findPackage($packageName, '*');
60-
if ($pkg !== null) {
61-
$version = $pkg->getPrettyVersion();
62-
try {
63-
$composer = $event->getComposer();
64-
$this->updateSetupWizardPlugin(
65-
$composer,
66-
$composer->getConfig()->getConfigSource()->getName(),
67-
$version
68-
);
69-
} catch (Exception $e) {
70-
$this->console->error("Web Setup Wizard installation of \"$packageName: $version\" failed", $e);
58+
$apiMajorVersion = explode('.', PluginInterface::PLUGIN_API_VERSION)[0];
59+
if ($apiMajorVersion == '1') {
60+
$jobs = $event->getRequest()->getJobs();
61+
foreach ($jobs as $job) {
62+
if (key_exists('packageName', $job) && $job['packageName'] === $packageName) {
63+
$pkg = $event->getInstalledRepo()->findPackage($packageName, '*');
64+
if ($pkg !== null) {
65+
$this->processEvent($pkg, $event);
66+
break;
7167
}
72-
break;
7368
}
7469
}
70+
} elseif ($apiMajorVersion == '2') {
71+
if (strpos($event->getOperation()->show(false), $packageName) !== false) {
72+
$pkg = $event->getLocalRepo()->findPackage($packageName, '*');
73+
if ($pkg !== null) {
74+
$this->processEvent($pkg, $event);
75+
}
76+
}
77+
} else {
78+
$this->console->error(
79+
"Web Setup Wizard installation of \"$packageName\" failed; unrecognized composer plugin API version"
80+
);
81+
}
82+
}
83+
84+
/**
85+
* Helper function to attempt to run updateSetupWizardPlugin when an appropriate PackageEvent is fired
86+
*
87+
* @param $pkg PackageInterface
88+
* @param $event PackageEvent
89+
*/
90+
public function processEvent($pkg, $event)
91+
{
92+
$packageName = PluginDefinition::PACKAGE_NAME;
93+
$version = $pkg->getPrettyVersion();
94+
try {
95+
$composer = $event->getComposer();
96+
$this->updateSetupWizardPlugin(
97+
$composer,
98+
$composer->getConfig()->getConfigSource()->getName(),
99+
$version
100+
);
101+
} catch (Exception $e) {
102+
$this->console->error("Web Setup Wizard installation of \"$packageName: $version\" failed", $e);
75103
}
76104
}
77105

src/Magento/ComposerRootUpdatePlugin/Updater/RootPackageRetriever.php

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Composer\Package\RootPackageInterface;
1515
use Composer\Package\Version\VersionParser;
1616
use Composer\Package\Version\VersionSelector;
17+
use Composer\Plugin\PluginInterface;
1718
use Composer\Repository\CompositeRepository;
19+
use Composer\Repository\RepositorySet;
1820
use Composer\Repository\VcsRepository;
1921
use Magento\ComposerRootUpdatePlugin\ComposerReimplementation\AccessibleRootPackageLoader;
2022
use Magento\ComposerRootUpdatePlugin\Utils\PackageUtils;
@@ -249,6 +251,7 @@ protected function fetchMageRootFromRepo(
249251
$phpVersion = null,
250252
$preferredStability = 'stable'
251253
) {
254+
$apiMajorVersion = explode('.', PluginInterface::PLUGIN_API_VERSION)[0];
252255
$packageName = $this->pkgUtils->getProjectPackageName($edition);
253256
$parsedConstraint = (new VersionParser())->parseConstraints($constraint);
254257

@@ -263,26 +266,6 @@ protected function fetchMageRootFromRepo(
263266
: $minStability;
264267
$this->console->comment("Minimum stability for \"$packageName: $constraint\": $stability", IOInterface::DEBUG);
265268

266-
$pool = new Pool(
267-
$stability,
268-
$stabilityFlags,
269-
[$packageName => $parsedConstraint]
270-
);
271-
if ($edition == PackageUtils::CLOUD_PKG_EDITION) {
272-
// magento/magento-cloud-template exists on github, not the composer repo
273-
$repoConfig = [
274-
'url' => 'https://github.com/magento/magento-cloud',
275-
'type' => 'vcs'
276-
];
277-
$pool->addRepository(new VcsRepository(
278-
$repoConfig,
279-
$this->console->getIO(),
280-
$this->composer->getConfig()
281-
));
282-
} else {
283-
$pool->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
284-
}
285-
286269
$metapackageName = $this->pkgUtils->getMetapackageName($edition);
287270
if ($edition != PackageUtils::CLOUD_PKG_EDITION && !$this->pkgUtils->isConstraintStrict($constraint)) {
288271
$this->console->warning(
@@ -293,13 +276,65 @@ protected function fetchMageRootFromRepo(
293276
}
294277

295278
$phpVersion = $ignorePlatformReqs ? null : $phpVersion;
279+
$versionSelector = null;
280+
$result = null;
281+
if ($apiMajorVersion == '1') {
282+
$pool = new Pool(
283+
$stability,
284+
$stabilityFlags,
285+
[$packageName => $parsedConstraint]
286+
);
287+
if ($edition == PackageUtils::CLOUD_PKG_EDITION) {
288+
// magento/magento-cloud-template exists on github, not the composer repo
289+
$repoConfig = [
290+
'url' => 'https://github.com/magento/magento-cloud',
291+
'type' => 'vcs'
292+
];
293+
$pool->addRepository(new VcsRepository(
294+
$repoConfig,
295+
$this->console->getIO(),
296+
$this->composer->getConfig()
297+
));
298+
} else {
299+
$pool->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
300+
}
301+
302+
$versionSelector = new VersionSelector($pool);
303+
$result = ($versionSelector)->findBestCandidate(
304+
$packageName,
305+
$constraint,
306+
$phpVersion,
307+
$preferredStability
308+
);
309+
} elseif ($apiMajorVersion == '2') {
310+
$repositorySet = new RepositorySet($minStability, $stabilityFlags);
311+
if ($edition == PackageUtils::CLOUD_PKG_EDITION) {
312+
// magento/magento-cloud-template exists on github, not the composer repo
313+
$repoConfig = [
314+
'url' => 'https://github.com/magento/magento-cloud',
315+
'type' => 'vcs'
316+
];
317+
$repositorySet->addRepository(new VcsRepository(
318+
$repoConfig,
319+
$this->console->getIO(),
320+
$this->composer->getConfig(),
321+
$this->composer->getLoop()->getHttpDownloader()
322+
));
323+
} else {
324+
$repositorySet->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
325+
}
296326

297-
$result = (new VersionSelector($pool))->findBestCandidate(
298-
$packageName,
299-
$constraint,
300-
$phpVersion,
301-
$preferredStability
302-
);
327+
$versionSelector = new VersionSelector($repositorySet);
328+
$result = ($versionSelector)->findBestCandidate(
329+
$packageName,
330+
$constraint,
331+
$preferredStability
332+
);
333+
} else {
334+
$this->console->error(
335+
"Fetching Magento root composer failed; unrecognized composer plugin API version"
336+
);
337+
}
303338

304339
if (!$result) {
305340
$err = "Could not find a Magento project package matching \"$metapackageName $constraint\"";

src/Magento/ComposerRootUpdatePlugin/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"AFL-3.0"
99
],
1010
"require": {
11-
"composer/composer": "<=1.10.15",
12-
"composer-plugin-api": "^1.0"
11+
"composer/composer": "<=1.10.19 || >=2.0.0 <=2.0.8",
12+
"composer-plugin-api": "^1.0 || ^2.0"
1313
},
1414
"suggest": {
1515
"magento/framework": "Enables the Magento Composer Root Update Plugin's functionality for the Web Setup Wizard"

0 commit comments

Comments
 (0)