Skip to content

Commit 6216caa

Browse files
Merge pull request #2 from magento-borg/COMOPS-1503-composer-2
COMOPS-1503: Adding support for Composer 2
2 parents 5807d6e + a453f06 commit 6216caa

File tree

8 files changed

+278
-58
lines changed

8 files changed

+278
-58
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: 8 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))
@@ -208,6 +210,12 @@ This class contains methods to retrieve Composer [Package](https://getcomposer.o
208210
- Returns the existing root project package, including all user customizations
209211
- **`fetchMageRootFromRepo()`**
210212
- Given a Magento edition and version constraint, fetch the best-fit Magento root project package from the Composer repository or GitHub (in the case of cloud)
213+
- **`findBestCandidate()`**
214+
- Wrapper function around different versions of [VersionSelector::findBestCandidate()](https://getcomposer.org/apidoc/master/Composer/Package/Version/VersionSelector.html)
215+
- **`findBestCandidateComposer1()`**
216+
- Helper function to run [VersionSelector::findBestCandidate()](https://getcomposer.org/apidoc/master/Composer/Package/Version/VersionSelector.html) on Composer version 1.x.x
217+
- **`findBestCandidateComposer2()`**
218+
- Helper function to run [VersionSelector::findBestCandidate()](https://getcomposer.org/apidoc/master/Composer/Package/Version/VersionSelector.html) on Composer version 2.x.x
211219
- **`parseVersionAndEditionFromLock()`**
212220
- Inspect the `composer.lock` file for the currently-installed Magento product or cloud metapackage and parse out the edition and version for use by `getOriginalRootPackage()`
213221
- **`getTargetLabel()`**

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: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Composer\Installer;
1313
use Composer\Installer\PackageEvent;
1414
use Composer\Json\JsonFile;
15+
use Composer\Package\PackageInterface;
1516
use Exception;
1617
use Magento\ComposerRootUpdatePlugin\Utils\Console;
1718
use Magento\ComposerRootUpdatePlugin\Utils\PackageUtils;
@@ -52,26 +53,52 @@ public function __construct($console)
5253
*/
5354
public function packageEvent($event)
5455
{
55-
$jobs = $event->getRequest()->getJobs();
5656
$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);
57+
$composerMajorVersion = explode('.', Composer::VERSION)[0];
58+
if ($composerMajorVersion == '1') {
59+
$jobs = $event->getRequest()->getJobs();
60+
foreach ($jobs as $job) {
61+
if (key_exists('packageName', $job) && $job['packageName'] === $packageName) {
62+
$pkg = $event->getInstalledRepo()->findPackage($packageName, '*');
63+
if ($pkg !== null) {
64+
$this->processEvent($pkg, $event);
65+
break;
7166
}
72-
break;
7367
}
7468
}
69+
} elseif ($composerMajorVersion == '2') {
70+
if (strpos($event->getOperation()->show(false), $packageName) !== false) {
71+
$pkg = $event->getLocalRepo()->findPackage($packageName, '*');
72+
if ($pkg !== null) {
73+
$this->processEvent($pkg, $event);
74+
}
75+
}
76+
} else {
77+
$this->console->error(
78+
"Web Setup Wizard installation of \"$packageName\" failed; unrecognized composer plugin API version"
79+
);
80+
}
81+
}
82+
83+
/**
84+
* Helper function to attempt to run updateSetupWizardPlugin when an appropriate PackageEvent is fired
85+
*
86+
* @param $pkg PackageInterface
87+
* @param $event PackageEvent
88+
*/
89+
public function processEvent($pkg, $event)
90+
{
91+
$packageName = PluginDefinition::PACKAGE_NAME;
92+
$version = $pkg->getPrettyVersion();
93+
try {
94+
$composer = $event->getComposer();
95+
$this->updateSetupWizardPlugin(
96+
$composer,
97+
$composer->getConfig()->getConfigSource()->getName(),
98+
$version
99+
);
100+
} catch (Exception $e) {
101+
$this->console->error("Web Setup Wizard installation of \"$packageName: $version\" failed", $e);
75102
}
76103
}
77104

0 commit comments

Comments
 (0)