Working with composer¶
Sane defaults¶
PHP Version¶
Rather than requiring a range of supported PHP versions, we should require the target php version of the live server.
You'll gain some benefits of better IDE integration and early warnings if the dependency tree has any issues with that version.
If the server is upgrading its PHP version, the require can be temporarily expanded until the work is complete.
PHP Extensions¶
Similar to the previous point, defining a list of required PHP extensions in your top level composer file will benefit your IDE integration, and give you early warnings when performing composer tasks on an incompatible setup.
You can copy the default list from the main Magento package and add to it as needed.
{
"require": {
"ext-bcmath": "*",
"ext-ctype": "*",
"ext-curl": "*",
"ext-dom": "*",
"ext-gd": "*",
"ext-hash": "*",
"ext-iconv": "*",
"ext-intl": "*",
"ext-mbstring": "*",
"ext-openssl": "*",
"ext-pdo_mysql": "*",
"ext-simplexml": "*",
"ext-soap": "*",
"ext-xsl": "*",
"ext-zip": "*"
}
}
Autoloading¶
Some of the autoload paths are legacy or only for the git cloned version.
See below for an example list of paths you can safely remove:
{
"autoload": {
"psr-4": {
"Magento\\Framework\\": "lib/internal/Magento/Framework/"
}
},
"autoload-dev": {
"psr-4": {
"Magento\\Tools\\": "dev/tools/Magento/Tools/",
"Magento\\Tools\\Sanity\\": "dev/build/publication/sanity/Magento/Tools/Sanity/"
}
}
}
The psr-0
rule "generated/code/"
has been known to cause problems when optimising the autoloader, and then changing anything with a new di compile.
In development, you can skip running an optimise step, and ensure the composer config has "optimize-autoloader": false
In production, the recommended solution for this is to always run the following commands in sequence.
php bin/magento setup:di:compile
composer dump --o
Removing default packages¶
Replacing¶
Please note: This method can cause dependency issues when excluding a module another module in your codebase depends on.
Extensions required by the Magento codebase but are disabled still have their CSS added to the compiled version, and can add extra time to compile tasks in Magento. See #24666
Much in the same way the git managed version of Magento prevents any of its modules in app/code also being required when adding 3rd party extensions via composer, we can exclude undesired modules from the default Magento setup.
We can do this by adding these modules to a list in the root composer file.
The following is an example list of third party modules installed by default.
{
"replace": {
"amzn/amazon-pay-and-login-magento-2-module": "*",
"amzn/amazon-pay-and-login-with-amazon-core-module": "*",
"amzn/amazon-pay-module": "*",
"amzn/amazon-pay-sdk-php": "*",
"amzn/login-with-amazon-module": "*",
"dotmailer/dotmailer-magento2-extension": "*",
"dotmailer/dotmailer-magento2-extension-package": "*",
"dotmailer/dotmailer-magento2-extension-enterprise": "*",
"dotmailer/dotmailer-magento2-extension-chat": "*",
"klarna/m2-payments": "*",
"klarna/module-core": "*",
"klarna/module-kp": "*",
"klarna/module-ordermanagement": "*",
"temando/module-shipping": "*",
"temando/module-shipping-m2": "*",
"temando/module-shipping-remover": "*",
"vertex/product-magento-module": "*",
"vertex/module-tax": "*",
"vertex/sdk": "*",
"vertex/module-address-validation": "*"
}
}
Requiring¶
An alternative approach is to manage a set of composer metapackages that are versioned and updated in line with Magento releases.
Magento themeselves are working towards better support for this setup.
These metapackages would include a subset of Magento modules to fit the minimum requirements of a site, with the benefit of third party extensions being able to list Magento modules as a dependency.
The following example is using outdated versions, but illistrates the setup for a core metapackage to get Magento running, with the idea that any additional functionallity required can be added to the project as needed.
{
"name": "edmondscommerce/magento-core-deps",
"description": "Minimum Composer packages required for a working Magento install",
"keywords": ["magento"],
"type": "metapackage",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"require": {
"php": "~7.1.3||~7.2.0||~7.3.0",
"ext-bcmath": "*",
"ext-ctype": "*",
"ext-curl": "*",
"ext-dom": "*",
"ext-gd": "*",
"ext-hash": "*",
"ext-iconv": "*",
"ext-intl": "*",
"ext-mbstring": "*",
"ext-openssl": "*",
"ext-pdo_mysql": "*",
"ext-simplexml": "*",
"ext-soap": "*",
"ext-xsl": "*",
"ext-zip": "*",
"lib-libxml": "*",
"colinmollenhour/cache-backend-file": "^1.4.1",
"colinmollenhour/cache-backend-redis": "^1.10.6",
"magento/framework-message-queue": "^100.3",
"magento/magento-composer-installer": "^0.1.13",
"magento/magento2-base": "2.3.3",
"magento/module-admin-notification": "^100.3",
"magento/module-advanced-pricing-import-export": "^100.3",
"magento/module-bundle-import-export": "^100.3",
"magento/module-catalog-widget": "^100.3",
"magento/module-cookie": "^100.3",
"magento/module-currency-symbol": "^100.3",
"magento/module-customer-import-export": "^100.3",
"magento/module-downloadable-import-export": "^100.3",
"magento/module-elasticsearch-6": "^100.3",
"magento/module-encryption-key": "^100.3",
"magento/module-message-queue": "^100.3",
"magento/module-mysql-mq": "^100.3",
"magento/module-offline-payments": "^100.3",
"magento/module-offline-shipping": "^100.3",
"magento/module-persistent": "^100.3",
"magento/module-product-video": "^100.3",
"magento/module-sales-inventory": "^100.3",
"magento/module-sitemap": "^100.3",
"magento/module-store": "^101.0",
"magento/module-webapi-async": "^100.3",
"magento/theme-adminhtml-backend": "^100.3",
"magento/theme-frontend-blank": "^100.3",
"paragonie/sodium_compat": "^1.6",
"phpseclib/mcrypt_compat": "^1.0.8",
"ramsey/uuid": "^3.8.0",
"webonyx/graphql-php": "^0.13.8"
},
"suggest": {
"edmondscommerce/magento-recommended-deps": "Recommended for all projects",
"edmondscommerce/magento-deps-for-braintree": "For when the project uses braintree/paypal",
"edmondscommerce/magento-deps-for-swatches": "For when the project uses swatches",
"edmondscommerce/magento-deps-for-configurable": "For when the project uses configurable products",
"edmondscommerce/magento-deps-for-grouped": "For when the project uses grouped products",
"edmondscommerce/magento-deps-for-graphql": "For when the project uses graphql api's",
"edmondscommerce/magento-deps-for-analytics": "For when the project uses Magento analytics tracking",
"edmondscommerce/magento-deps-for-amqp": "For when the project uses amqp message queues",
"edmondscommerce/magento-inventory": "For when the project uses inventory management",
"ext-pcntl": "Need for run processes in parallel mode"
}
}