Composer Scripts

In this tutorial we're going to investigate how you can store common commands within composer, giving you some handy shortcuts.

Composer scripts can either be PHP callbacks or command line scripts.

Built-in Events

There are a number of built in events that when triggered commposer will run the given commands.

Command Events

  • pre-install-cmd: occurs before the install command is executed with a lock file present.
  • post-install-cmd: occurs after the install command has been executed with a lock file present.
  • pre-update-cmd: occurs before the update command is executed, or before the install command is executed without a lock file present.
  • post-update-cmd: occurs after the update command has been executed, or after the install command has been executed without a lock file present.
  • post-status-cmd: occurs after the status command has been executed.
  • pre-archive-cmd: occurs before the archive command is executed.
  • post-archive-cmd: occurs after the archive command has been executed.
  • pre-autoload-dump: occurs before the autoloader is dumped, either during install/update, or via the dump-autoload command.
  • post-autoload-dump: occurs after the autoloader has been dumped, either during install/update, or via the dump-autoload command.
  • post-root-package-install: occurs after the root package has been installed, during the create-project command.
  • post-create-project-cmd: occurs after the create-project command has been executed.

Installer Events

  • pre-dependencies-solving: occurs before the dependencies are resolved.
  • post-dependencies-solving: occurs after the dependencies have been resolved.

Package Events

  • pre-package-install: occurs before a package is installed.
  • post-package-install: occurs after a package has been installed.
  • pre-package-update: occurs before a package is updated.
  • post-package-update: occurs after a package has been updated.
  • pre-package-uninstall: occurs before a package is uninstalled.
  • post-package-uninstall: occurs after a package has been uninstalled.

Plugin Events

  • init: occurs after a Composer instance is done being initialized.
  • command: occurs before any Composer Command is executed on the CLI. It provides you with access to the input and output objects of the program.
  • pre-file-download: occurs before files are downloaded and allows you to manipulate the RemoteFilesystem object prior to downloading files based on the URL to be downloaded.
  • pre-command-run: occurs before a command is executed and allows you to manipulate the InputInterface object's options and arguments to tweak a command's behavior.

Composer Events

Create Scripts

Inside your composer.json file you can add a new section for scripts.

    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ]
    },

The key inside the scripts object will be the defined event, the value will be the commands to run when the events are triggered.

Custom Commands

Along with running commands on composer events you can also create your own commands. For example if you want to run the tests you can add a new composer command for tests.

{
    "scripts": {
        "test": "phpunit"
    }
}

Now you can run the command composer test and it will run phpunit. This can be very handy if you will want to make sure everything is setup correctly before running your tests. For example you can make sure composer is up to date before running the tests.

{
    "scripts": {
        "test": [
            "@composer install",
            "phpunit"
        ]
    }
}

Running PHPUnit In Docker

If you locally work on multiple projects with different PHP versions you could run the tests through docker to make sure you're always on the right PHP version. Using composer scripts you can create a handy shortcut for your docker testing command.

{
    "scripts": {
        "test": "docker run --rm -v \"$PWD\":/var/www -w \"/var/www\" --user $(id -u):$(id -g) php:7.3-cli php ./vendor/bin/phpunit --configuration phpunit.xml --no-coverage --colors=always"
    }
}

Running Code Sniffer In Docker

Along with running phpunit in docker you can also run php code sniffer with docker. I've previous wrote a tutorial on how to get started with PHP code sniffer Setup PHP Code Sniffer.

This comes with an example phpcs.xml file phpcs.xml Gist.

To add phpcs to your composer scripts you can use the following.

{
    "scripts": {
        "check": "docker run --rm -v \"$PWD\":/var/www -w \"/var/www\" cytopia/phpcs .",
    }
}

This uses the docker image cytopia/phpcs.

If this provides any fixable errors you can use phpcbf to fix them, again instead of installing this on your local machine we can use docker to perform the fixes.

Add another script for phpcbf

{
    "scripts": {
        "check": "docker run --rm -v \"$PWD\":/var/www -w \"/var/www\" cytopia/phpcs .",
		"fix": "docker run --rm -v \"$PWD\":/var/www -w \"/var/www\" cytopia/phpcbf .",
    }
}

UpChecker

Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring