Categories


Archives


Recent Posts


Categories


Magento Modules and Composer’s `extra` Configuration

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

I’ve recently allowed my open source time to become monopolized by a bunch of data scraping and analysis. I managed to pull together around 3400 composer.json files for Magento modules hosted on packagist. My primary interest was in their autoloader section, but then I remembered Magento’s toying around with using Composer’s nebulous extra field field for things. The intention of this field is to hold data available for composer’s script commands, but it turns out it gets overloaded for a lot of different things.

Here’s some of what I found.

Maps

The map section looks like this

"extra": {
    "map": [
        [
            "*",
            "Magento/DebugComment"
        ]
    ]
}

"extra": {
    "map": [
        [
            "pub",
            "..\/..\/pub"
        ]
    ]
}

This is an old Magento 2 experiment where their composer installer (runs after a composer create-project) would use this data to copy your package from its home in vendor/foo/bar into the app/code folder where it would be subject to Magento’s original module loading and autoloader. Just prior to Magento 2’s release this system was scrapped in favor of the psr-4 and files:['registration.php'] system that’s in favor today.

This functionality is still available in some limited form in Magento 2 today — the files in the magento/magento2-base package are copied into your root project folder this way. The app/code folder also remains available, but it doesn’t seems like something Magento’s paying attention to.

Branch Alias

Another thing you might find in the extra section of a package’s composer.json file in a branch-alias field.

"extra": {
    "branch-alias": {
        "dev-master": "1.0-dev"
    }
}

This one isn’t a Magento specific thing — it’s composer standard. Take note that despite the docs mentioning

Arbitrary extra data for consumption by scripts

that even the composer creators couldn’t resist their little blob of arbitrary data.

Patches

Another thing you might see in a Magento package’s composer.json file is a patches section.

"extra": {
    "patches": {
        "magento/framework": {
            "102.0.2": [
                {
                    "title": "[MAGENTO-22829] Cache locking issue",
                    "url": "https://dl.trollweb.no/m2-patch-composer/MAGENTO-22829/MAGENTO-22829-framework.patch",
                    "sha1": "b1adb7daa748d4f52a37cbd856c0f2bb503b03c1"
                }
            ]
        },
        "magento/module-config": {
            "101.1.2": [
                {
                    "title": "[MAGENTO-22829] Cache locking issue",
                    "url": "https://dl.trollweb.no/m2-patch-composer/MAGENTO-22829/MAGENTO-22829-module-config.patch",
                    "sha1": "aa87f017e5306140038672dd1d808ff3fae751e4"
                }
            ]
        }
    }
}

This one isn’t a Magento Inc. invention — it’s a system from the PHP community that lets you provide patches to other package files in your projects. It was en-vogue in the Magento community for a bit, and still seems like a recommended thing by Magento support.

Despite being popular among a certain set, this one always seemed like madness to me — a .patch file (event in 2015) seems like an archaic way to modify system behavior. It’s just formalized core editing.

Magento Force

You might see a magento-force field

"extra": {
  "magento-force": "override"
}

This one seems to be related to the above map functionality, and provides some sort instructions on what to do when map copies files from your package into the main Magento installation.

Exclude

I also found an exclude field.

"extra": {
    "exclude": [
        "phpcs.xml"
    ]
},

I’m honestly not sure what this one case was/is for. The phpcs.xml file is related to the PHP_CodeSniffer project — but what it’s doing in the extra.exclude field eludes me.

Module Data

You also might see something like these two outliers

"extra": {
    "swissup": {
        "links": {
            "store"    : "https://swissuplabs.com/magento-2-easy-catalog-images.html",
            "download" : "https://swissuplabs.com/subscription/customer/products/",
            "docs"     : "http://docs.swissuplabs.com/m2/extensions/easycatalogimages/",
            "changelog": "http://docs.swissuplabs.com/m2/extensions/easycatalogimages/changelog/"
        }
    }
}

"extra": {
    "vaimo-module-category": "Integration"
},

Here, the extension creators (swissup, vaimo) are using the extra section as their own arbitrary data store for reasons unknown to us.

Arbitrary data blobs are the processed sugar of the programming world.

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 14th October 2019