0

PSR-15 — HTTP Middleware interface

Intermediate5 min read·php-12-012
psrframework

Concept

Composer scripts let you define shell commands and PHP callbacks that run at specific lifecycle hooks or as named commands. They reduce the need for Makefiles or shell scripts for common project tasks.

Lifecycle events:

  • pre-install-cmd / post-install-cmd: Before/after composer install.
  • pre-update-cmd / post-update-cmd: Before/after composer update.
  • pre-autoload-dump / post-autoload-dump: Before/after autoloader generation.
  • post-root-package-install: After a new project is created via create-project.
  • pre-package-install / post-package-install: For each package installation.

Named scripts: Custom commands under any key in "scripts". Run with composer run-script script-name or composer script-name.

Calling other scripts: Use @script-name to call another script within the same composer.json. Use @php artisan ... or @php vendor/bin/tool to run PHP commands.

Composer plugins: Packages of type: "composer-plugin" that hook into Composer's event system. They run with elevated privileges — Composer 2.2+ requires explicit allowance in "config": { "allow-plugins": {} }.

Semver resolution performance: Composer 2.x is dramatically faster than 1.x for dependency resolution (uses a SAT solver). If your project still uses Composer 1.x, upgrade — resolution can be 10-50× faster on large dependency trees.

Code Example

json
{
    "scripts": {
        "post-install-cmd": [
            "@php artisan key:generate --ansi --no-interaction",
            "@php artisan storage:link"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],

        "test": [
            "@php artisan test --parallel"
        ],
        "test:coverage": [
            "XDEBUG_MODE=coverage php artisan test --coverage --min=80"
        ],
        "lint": [
            "vendor/bin/pint --test"
        ],
        "lint:fix": [
            "vendor/bin/pint"
        ],
        "analyse": [
            "vendor/bin/phpstan analyse --memory-limit=256M"
        ],
        "qa": [
            "@lint",
            "@analyse",
            "@test"
        ],
        "clean": [
            "@php artisan cache:clear",
            "@php artisan config:clear",
            "@php artisan route:clear",
            "@php artisan view:clear"
        ]
    },
    "scripts-descriptions": {
        "test": "Run the test suite",
        "qa": "Run all QA checks (lint, analyse, test)",
        "clean": "Clear all Laravel caches"
    }
}
bash
# Run named scripts
composer test
composer run qa
composer run-script lint:fix

# Silence Composer output (just show script output)
composer --quiet test

# Run a specific package's binary directly
./vendor/bin/phpstan analyse src/
./vendor/bin/pint --test