May 25, 2021 Composer
A script, in Composer, can be a PHP callback (defined as a static method) or any command line executable command. Scripts are useful for executing custom code or package-specific commands for a resource pack while composer is running.
Note: Only
scripts defined
composer.json
of the root package will be executed.
Even if the external dependencies of the root package define their own scripts, Composer does not execute these additional scripts.
Composer will trigger the following events during operation:
The name of the event | Details |
---|---|
**pre-install-cmd** | Triggered before the 'install' command is executed. |
**post-install-cmd** | Triggered after the 'install' command is executed. |
**pre-update-cmd** | Triggered before the 'update' command is executed. |
**post-update-cmd** | Triggered after the 'update' command is executed. |
**pre-status-cmd** | Triggered before the 'status' command is executed. |
**post-status-cmd** | Triggered after the 'status' command is executed. |
**pre-package-install** | Triggered before the resource pack is installed. |
**post-package-install** | Triggered after the resource pack is installed. |
**pre-package-update** | Triggered before the resource pack is updated. |
**post-package-update** | Triggered after the resource pack is updated. |
**pre-package-uninstall** | Triggered before the resource pack is unloaded. |
**post-package-uninstall** | Triggered after the resource pack is unloaded. |
**pre-autoload-dump** | Triggered before the autoloader is dumped, whether it is the 'install'/'update' or 'dump-autoload' command. |
**post-autoload-dump** | Triggered after the autoloader is dumped, either the 'install'/'update' or 'dump-autoload' command is triggered. |
**post-root-package-install** | During the 'create-project' command, the root package is triggered after installation. |
**post-create-project-cmd** | Triggered after the 'create-project' command is executed. |
archive
command is executed.
post-archive-cmd
: occurs after the
archive
command is executed.
Note:
C
omposer does not execute any install or
install
scripts that depend on the
update
the package. T
herefore, you should not state
pre-update-cmd
pre-install-cmd
If you need to
install
update
or update commands, make sure they are defined in the root package.
There should be a property called
"scripts"
a series of event names, as well as corresponding event scripts.
composer.json
A script for an event can be defined as a string (for a single script only) or an array (for a single or multiple scripts).
For any given event:
Script definition instance:
{
"scripts": {
"post-update-cmd": "MyVendor\\MyClass::postUpdate",
"post-package-install": [
"MyVendor\\MyClass::postPackageInstall"
],
"post-install-cmd": [
"MyVendor\\MyClass::warmCache",
"phpunit -c app/"
]
}
}
Using the example defined
MyVendor\MyClass
class here can be used to perform a callback to PHP:
<?php
namespace MyVendor;
use Composer\Script\Event;
class MyClass
{
public static function postUpdate(Event $event)
{
$composer = $event->getComposer();
// do stuff
}
public static function postPackageInstall(Event $event)
{
$installedPackage = $event->getOperation()->getPackage();
// do stuff
}
public static function warmCache(Event $event)
{
// make cache toasty
}
}
When an event is triggered, Composer's internal event handler
Composer\Script\Event
which is the first parameter passed to your PHP callback.
This
Event
has some getter methods to help you get the context of the current event:
getComposer()
the current
Composer\Composer
object.
getName()
string that returns the name of the event.
getIO()
Returns the current input-output stream, which
Composer\IO\IOInterface
for use in the console.
If you want to run the event script manually, you can use the following syntax structure:
composer run-script [--dev] [--no-dev] script
For
composer run-script post-install-cmd
will run
all scripts defined under the post-install-cmd
event.