Package Configuration
composer.json Structure
{
"name": "vendor/module-name",
"type": "magento2-module",
"description": "Module description",
"version": "1.0.0",
"license": [
"proprietary"
],
"autoload": {
"psr-4": {
"Vendor\\ModuleName\\": ""
},
"files": [
"registration.php"
]
},
"require": {
"php": "^8.1",
"magento/framework": "^103.0",
"magento/module-store": "^101.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"magento/module-developer": "^103.0"
}
}
Package Types
| Type | Description |
|---|---|
magento2-module |
Magento 2 module |
magento2-theme |
Magento 2 theme |
magento2-language |
Language package |
library |
PHP library |
project |
Application project |
Autoload Configuration
{
"autoload": {
"psr-4": {
"Vendor\\ModuleName\\": ""
},
"psr-0": {
"Vendor\\ModuleName\\": "lib/"
},
"classmap": ["src/"],
"files": ["src/helpers.php"]
},
"autoload-dev": {
"psr-4": {
"Vendor\\ModuleName\\Test\\": "tests/"
}
}
}
Repositories and Mirrors
Custom Repositories
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/vendor/custom-repo"
},
{
"type": "composer",
"url": "https://repo.example.com/"
},
{
"type": "artifact",
"url": "/path/to/packages/"
},
{
"type": "path",
"url": "../local-package"
}
]
}
Packagist Mirrors
{
"config": {
"packagist": {
"domain": "packagist.org"
}
},
"repositories": [
{
"type": "composer",
"url": "https://mirrors.cloud.aliyuncs.com/pear/"
}
]
}
Private Repository Authentication
{
"config": {
"github-oauth": {
"github.com": "ghp_xxxxxxxxxxxx"
},
"bearer": {
"repo.example.com": "token123"
}
}
}
Repository Priority
{
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "stable",
"prefer-stable": true
}
Composer Scripts
Script Types
{
"scripts": {
"pre-install-cmd": "php bin/magento --version",
"post-install-cmd": [
"@php bin/magento setup:upgrade",
"@php bin/magento cache:clean"
],
"pre-update-cmd": "echo 'Updating...'",
"post-update-cmd": [
"@php bin/magento setup:upgrade",
"@php bin/magento setup:static-content:deploy"
],
"pre-package-install": "@validate-package",
"post-package-install": "@php bin/magento module:enable Vendor_Module",
"post-root-package-install": "@install-magento",
"post-create-project-cmd": [
"@php bin/magento setup:install",
"@php bin/magento sampledata:deploy"
]
}
}
Custom Scripts
{
"scripts": {
"test": "phpunit tests/",
"lint": "phpcs --standard=PSR12 src/",
"static-deploy": "php bin/magento setup:static-content:deploy -f",
"clear-cache": "php bin/magento cache:clean && php bin/magento cache:flush",
"compile": "php bin/magento setup:di:compile",
"deploy": [
"@compile",
"@static-deploy",
"@clear-cache"
]
}
}
Script Variables
{
"scripts": {
"post-install-cmd": "@php bin/magento setup:upgrade --scope=default --store-front=%mbase%"
}
}
Script Execution
# Run specific script
composer run test
composer run lint
# Run multiple scripts
composer run deploy
# Run pre/post hooks
composer install # triggers pre-install-cmd, post-install-cmd
composer update # triggers pre-update-cmd, post-update-cmd
Advanced Configuration
Platform Config
{
"config": {
"platform": {
"php": "8.2.0",
"ext-redis": "*",
"ext-intl": "*"
}
}
}
Extra Configuration
{
"extra": {
"magento-core-package": false,
"branch-alias": {
"dev-master": "2.4-dev"
},
"installer-paths": {
"magento2-core": ["magento/magento2-base"],
"magento2-component": ["magento/magento2-*-component"],
"magento2-split": ["magento/magento2-*-split"]
}
}
}
Satis Build
{
"name": "myrepo/magento2-packages",
"description": "Magento 2 Package Repository",
"homepage": "https://repo.example.com",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/vendor/module-one"
},
{
"type": "vcs",
"url": "https://github.com/vendor/module-two"
}
],
"require-all": true,
"archive": {
"directory": "dist",
"format": "tar",
"skip-dev": true
}
}
Quiz
1. What is the type for a Magento 2 module?
2. What does the autoload section configure?
3. What is a Composer script?
Flashcards
Question
What is composer.json type?
Click to reveal answer
Answer
magento2-module, magento2-theme, magento2-language
Question
What does autoload configure?
Click to reveal answer
Answer
PSR-4/PSR-0 class loading paths
Question
What are Composer scripts?
Click to reveal answer
Answer
Shell commands for install/update events
Question
What is require vs require-dev?
Click to reveal answer
Answer
Runtime vs development dependencies
Revision Notes
Key Takeaways
- 1. composer.json defines package metadata and dependencies
- 2. PSR-4 autoloading maps namespaces to directories
- 3. Repositories provide alternative package sources
- 4. Scripts run shell commands during Composer events
- 5. Version constraints control allowed package versions
Interview Tips
- • Explain the difference between require and require-dev
- • Discuss PSR-4 autoloading configuration
- • Describe Composer script hooks
- • Talk about repository priority and mirroring
Cheat Sheet
composer.json:
name → vendor/package
type → magento2-module
autoload → PSR-4 paths
require → runtime deps
require-dev → dev deps
Scripts:
pre-install-cmd → before install
post-install-cmd → after install
pre-update-cmd → before update
post-update-cmd → after update
Repositories:
vcs → Git repos
composer → Composer repos
artifact → local packages
path → local path