Skip to content

Instantly share code, notes, and snippets.

@hostep
Last active January 27, 2024 10:37
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save hostep/26de0bf2bb695eb3b6d51858c2e4562e to your computer and use it in GitHub Desktop.
Save hostep/26de0bf2bb695eb3b6d51858c2e4562e to your computer and use it in GitHub Desktop.
Add composer v2 support to older Magento2 versions

Add composer v2 support to older Magento2 versions

Magento 2.3.7 and 2.4.2 ship with composer v2 support out of the box but as far as I can see the only thing that needs to happen is to use some more modern versions of certain composer plugins which are used by certain dependencies of Magento.

This means we should be able to add composer v2 support to older Magento2 versions as well if we get a bit creative.

See below for diffs of the composer.json files you can apply to your projects, be sure to keep a mental note of these things, they will need to maintained by yourself in case newer versions of these modules are released. And if one day you update to Magento 2.3.7 or 2.4.2 or higher, you can remove these changes again.

⚠️ Disclaimer: use these tricks at your own risk!

Instructions

  1. Apply the relevant changes to the composer.json file in your project
  2. Run the composer update command specified underneath the diff with composer v1
  3. Now you can switch to composer v2, and run composer update --lock so your composer.lock file uses the new v2 format
  4. Run composer dump-autoload -o, see if it mentions autoload problems, if they look important: try to figure out how to fix them, always end this step by running composer dump-autoload (because Magento running in developer mode doesn't like an optimised autoloader)
  5. Have yourself a piece of cake and enjoy the improved performance composer v2 brings!

Diff per Magento version range

Magento >= 2.4.0 < 2.4.2

diff --git a/composer.json b/composer.json
index 60dca91..70dc34b 100644
--- a/composer.json
+++ b/composer.json
@@ -12,6 +12,8 @@
     },
     "version": "2.4.1",
     "require": {
+        "laminas/laminas-dependency-plugin": "2.1.2 as 1.0.4",
+        "magento/inventory-composer-installer": "1.2.0 as 1.1.0",
         "magento/product-community-edition": "2.4.1",
         "magento/composer-root-update-plugin": "~1.0"
     },
@@ -39,7 +41,7 @@
     },
     "require-dev": {
         "allure-framework/allure-phpunit": "~1.2.0",
-        "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
+        "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
         "friendsofphp/php-cs-fixer": "~2.16.0",
         "lusitanian/oauth": "~0.8.10",
         "magento/magento-coding-standard": "*",
composer update magento/magento-composer-installer magento/composer-root-update-plugin laminas/laminas-dependency-plugin magento/inventory-composer-installer dealerdirect/phpcodesniffer-composer-installer

Magento >= 2.3.6 < 2.3.7

diff --git a/composer.json b/composer.json
index 03713a1..162dc43 100644
--- a/composer.json
+++ b/composer.json
@@ -12,6 +12,8 @@
     },
     "version": "2.3.6-p1",
     "require": {
+        "laminas/laminas-dependency-plugin": "2.1.2 as 1.0.4", <= use version 2.0.0 if your project uses PHP 7.2
+        "magento/inventory-composer-installer": "1.2.0 as 1.1.0",
         "magento/product-community-edition": "2.3.6-p1",
         "magento/composer-root-update-plugin": "~1.0"
     },
@@ -39,7 +41,7 @@
     },
     "require-dev": {
         "allure-framework/allure-phpunit": "~1.2.0",
-        "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
+        "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
         "friendsofphp/php-cs-fixer": "~2.14.0",
         "lusitanian/oauth": "~0.8.10",
         "magento/magento-coding-standard": "*",
composer update magento/magento-composer-installer magento/composer-root-update-plugin laminas/laminas-dependency-plugin magento/inventory-composer-installer dealerdirect/phpcodesniffer-composer-installer

Magento >= 2.3.5 < 2.3.6

diff --git a/composer.json b/composer.json
index 9513f4a..4742220 100644
--- a/composer.json
+++ b/composer.json
@@ -12,11 +12,13 @@
     },
     "require": {
         "magento/product-community-edition": "2.3.5",
+        "laminas/laminas-dependency-plugin": "2.1.2 as 1.0.4", <= use version 2.0.0 if your project uses PHP 7.2
+        "magento/inventory-composer-installer": "1.2.0 as 1.1.0"
     },
     "require-dev": {
         "allure-framework/allure-phpunit": "~1.2.0",
-        "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
+        "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
         "friendsofphp/php-cs-fixer": "~2.14.0",
         "lusitanian/oauth": "~0.8.10",
         "magento/magento-coding-standard": "*",
composer update magento/magento-composer-installer laminas/laminas-dependency-plugin magento/inventory-composer-installer dealerdirect/phpcodesniffer-composer-installer

Magento >= 2.3.0 < 2.3.5

diff --git a/composer.json b/composer.json
index 78fbeb2..2119e21 100644
--- a/composer.json
+++ b/composer.json
@@ -11,6 +11,7 @@
         "sort-packages": true
     },
     "require": {
+        "magento/inventory-composer-installer": "1.2.0 as 1.1.0",
         "magento/product-community-edition": "2.3.1"
     },
     "require-dev": {
@@ -63,7 +64,8 @@
composer update magento/magento-composer-installer magento/inventory-composer-installer

Magento >= 2.2.0 < 2.3.0

diff --git a/composer.json b/composer.json
index c6c4a84..560ea7f 100644
--- a/composer.json
+++ b/composer.json
@@ -9,7 +9,7 @@
     ],
     "require": {
         "magento/product-community-edition": "2.2.2",
-        "composer/composer": "@alpha"
+        "composer/composer": "1.10.26 as 1.4.1"
     },
     "require-dev": {
         "phpunit/phpunit": "~6.2.0",
@@ -53,7 +53,8 @@
composer update magento/magento-composer-installer composer/composer

Magento >= 2.1.0 < 2.2.0

TODO - not sure if possible, need to solve justinrainbow/json-schema dependency conflict

Magento >= 2.0.0 < 2.1.0

TODO - not tested yet ...

@lamasfoker
Copy link

lamasfoker commented Oct 18, 2021

If during step 3 you get an error like:

Problem 1
    - Root composer.json requires magento/composer == 1.4.0.0, it is satisfiable by magento/composer[1.4.0] from vcs repo (git https://github.com/magento/composer) but magento/composer[1.0.2, ..., 1.7.0] from composer repo (https://repo.magento.com) has higher repository priority. The packages with higher priority do not match your constraint and are therefore not installable. See https://getcomposer.org/repoprio for details and assistance.

Add in the composer.json project the exclude part:

{
      "type": "composer",
      "url": "https://repo.magento.com/",
      "exclude": ["magento/composer"]
}

Tested on magento 2.3.2-p2 EE.

Edit: you do not have to do anymore, see magento/composer#23

@hostep
Copy link
Author

hostep commented Oct 19, 2021

@lamasfoker: you are correct unfortunately. Until magento/composer#23 is solved, you'll need to handle it like that. Please don't forget to remove that exclude section again when upgrading to a newer version of Magento please, as it circumvents a security feature of composer2 🙂

@lamasfoker
Copy link

Thank you @hostep, I did not think about the security issue. I see that magento/composer#23 is closed, so now I can remove the exclude section once and for all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment