2012-02-29 14:57:16 +00:00
|
|
|
<!--
|
|
|
|
tagline: Script are callbacks that are called before/after installing packages
|
|
|
|
-->
|
2012-08-29 20:30:47 +00:00
|
|
|
|
2012-02-05 15:27:55 +00:00
|
|
|
# Scripts
|
|
|
|
|
|
|
|
## What is a script?
|
|
|
|
|
2012-10-01 02:53:26 +00:00
|
|
|
A script, in Composer's terms, can either be a PHP callback (defined as a
|
|
|
|
static method) or any command-line executable command. Scripts are useful
|
|
|
|
for executing a package's custom code or package-specific commands during
|
2013-12-18 21:59:10 +00:00
|
|
|
the Composer execution process.
|
2012-02-05 15:27:55 +00:00
|
|
|
|
2014-07-23 17:00:59 +00:00
|
|
|
> **Note:** Only scripts defined in the root package's `composer.json` are
|
|
|
|
> executed. If a dependency of the root package specifies its own scripts,
|
|
|
|
> Composer does not execute those additional scripts.
|
2012-02-05 15:27:55 +00:00
|
|
|
|
|
|
|
|
2012-10-01 02:53:26 +00:00
|
|
|
## Event names
|
2012-02-05 15:27:55 +00:00
|
|
|
|
2012-10-01 02:53:26 +00:00
|
|
|
Composer fires the following named events during its execution process:
|
|
|
|
|
2015-03-04 23:50:10 +00:00
|
|
|
### Command Events
|
|
|
|
|
2012-10-21 19:05:32 +00:00
|
|
|
- **pre-install-cmd**: occurs before the `install` command is executed.
|
2012-10-01 02:53:26 +00:00
|
|
|
- **post-install-cmd**: occurs after the `install` command is executed.
|
|
|
|
- **pre-update-cmd**: occurs before the `update` command is executed.
|
|
|
|
- **post-update-cmd**: occurs after the `update` command is executed.
|
2013-08-01 06:44:07 +00:00
|
|
|
- **pre-status-cmd**: occurs before the `status` command is executed.
|
|
|
|
- **post-status-cmd**: occurs after the `status` command is executed.
|
2015-03-04 23:50:10 +00:00
|
|
|
- **pre-archive-cmd**: occurs before the `archive` command is executed.
|
|
|
|
- **post-archive-cmd**: occurs after the `archive` command is executed.
|
2013-05-06 14:37:21 +00:00
|
|
|
- **pre-autoload-dump**: occurs before the autoloader is dumped, either
|
|
|
|
during `install`/`update`, or via the `dump-autoload` command.
|
2013-02-22 17:44:50 +00:00
|
|
|
- **post-autoload-dump**: occurs after the autoloader is dumped, either
|
|
|
|
during `install`/`update`, or via the `dump-autoload` command.
|
2013-05-31 09:39:14 +00:00
|
|
|
- **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 is
|
|
|
|
executed.
|
2015-03-04 23:50:10 +00:00
|
|
|
|
|
|
|
### Installer Events
|
|
|
|
|
|
|
|
- **pre-dependencies-solving**: occurs before the dependencies are resolved.
|
|
|
|
- **post-dependencies-solving**: occurs after the dependencies are resolved.
|
|
|
|
|
|
|
|
### Package Events
|
|
|
|
|
|
|
|
- **pre-package-install**: occurs before a package is installed.
|
|
|
|
- **post-package-install**: occurs after a package is installed.
|
|
|
|
- **pre-package-update**: occurs before a package is updated.
|
|
|
|
- **post-package-update**: occurs after a package is updated.
|
|
|
|
- **pre-package-uninstall**: occurs before a package has been uninstalled.
|
|
|
|
- **post-package-uninstall**: occurs after a package has been uninstalled.
|
|
|
|
|
|
|
|
### Plugin Events
|
|
|
|
|
|
|
|
- **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.
|
2012-02-05 15:27:55 +00:00
|
|
|
|
2014-07-23 14:47:54 +00:00
|
|
|
> **Note:** Composer makes no assumptions about the state of your dependencies
|
|
|
|
> prior to `install` or `update`. Therefore, you should not specify scripts
|
|
|
|
> that require Composer-managed dependencies in the `pre-update-cmd` or
|
|
|
|
> `pre-install-cmd` event hooks. If you need to execute scripts prior to
|
|
|
|
> `install` or `update` please make sure they are self-contained within your
|
|
|
|
> root package.
|
2013-10-12 03:58:43 +00:00
|
|
|
|
2012-02-05 15:27:55 +00:00
|
|
|
## Defining scripts
|
|
|
|
|
2012-10-22 18:24:05 +00:00
|
|
|
The root JSON object in `composer.json` should have a property called
|
|
|
|
`"scripts"`, which contains pairs of named events and each event's
|
|
|
|
corresponding scripts. An event's scripts can be defined as either as a string
|
|
|
|
(only for a single script) or an array (for single or multiple scripts.)
|
2012-02-05 15:27:55 +00:00
|
|
|
|
2012-10-01 02:53:26 +00:00
|
|
|
For any given event:
|
2012-02-05 15:27:55 +00:00
|
|
|
|
2012-10-01 02:53:26 +00:00
|
|
|
- Scripts execute in the order defined when their corresponding event is fired.
|
|
|
|
- An array of scripts wired to a single event can contain both PHP callbacks
|
2014-09-05 16:21:50 +00:00
|
|
|
and command-line executable commands.
|
2012-10-01 02:53:26 +00:00
|
|
|
- PHP classes containing defined callbacks must be autoloadable via Composer's
|
|
|
|
autoload functionality.
|
2015-08-12 11:37:40 +00:00
|
|
|
- Callbacks can only autoload classes from psr-0, psr-4 and classmap
|
|
|
|
definitions. If a defined callback relies on functions defined outside of a
|
|
|
|
class, the callback itself is responsible for loading the file containing these
|
|
|
|
functions.
|
2012-02-05 15:27:55 +00:00
|
|
|
|
|
|
|
Script definition example:
|
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"scripts": {
|
|
|
|
"post-update-cmd": "MyVendor\\MyClass::postUpdate",
|
|
|
|
"post-package-install": [
|
|
|
|
"MyVendor\\MyClass::postPackageInstall"
|
|
|
|
],
|
|
|
|
"post-install-cmd": [
|
|
|
|
"MyVendor\\MyClass::warmCache",
|
|
|
|
"phpunit -c app/"
|
2014-08-06 12:54:43 +00:00
|
|
|
],
|
2015-07-01 16:38:14 +00:00
|
|
|
"post-autoload-dump": [
|
|
|
|
"MyVendor\\MyClass::postAutoloadDump"
|
2015-08-12 11:37:40 +00:00
|
|
|
],
|
|
|
|
"post-create-project-cmd": [
|
2014-08-06 12:54:43 +00:00
|
|
|
"php -r \"copy('config/local-example.php', 'config/local.php');\""
|
2014-05-19 10:17:07 +00:00
|
|
|
]
|
2012-02-05 15:27:55 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
2012-02-05 15:27:55 +00:00
|
|
|
|
2012-10-01 02:53:26 +00:00
|
|
|
Using the previous definition example, here's the class `MyVendor\MyClass`
|
|
|
|
that might be used to execute the PHP callbacks:
|
2012-02-05 15:27:55 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MyVendor;
|
2012-02-05 15:27:55 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
use Composer\Script\Event;
|
2015-03-04 23:50:10 +00:00
|
|
|
use Composer\Installer\PackageEvent;
|
2012-02-05 15:27:55 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
class MyClass
|
|
|
|
{
|
|
|
|
public static function postUpdate(Event $event)
|
|
|
|
{
|
|
|
|
$composer = $event->getComposer();
|
|
|
|
// do stuff
|
|
|
|
}
|
|
|
|
|
2015-06-27 21:38:09 +00:00
|
|
|
public static function postAutoloadDump(Event $event)
|
|
|
|
{
|
|
|
|
$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
|
2015-07-01 16:15:22 +00:00
|
|
|
require $vendorDir . '/autoload.php';
|
2015-08-12 11:37:40 +00:00
|
|
|
|
2015-06-27 21:38:09 +00:00
|
|
|
some_function_from_an_autoloaded_file();
|
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
|
2015-03-04 23:50:10 +00:00
|
|
|
public static function postPackageInstall(PackageEvent $event)
|
2014-05-19 10:17:07 +00:00
|
|
|
{
|
|
|
|
$installedPackage = $event->getOperation()->getPackage();
|
|
|
|
// do stuff
|
|
|
|
}
|
2012-03-26 22:59:08 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
public static function warmCache(Event $event)
|
2012-02-05 15:27:55 +00:00
|
|
|
{
|
2014-05-19 10:17:07 +00:00
|
|
|
// make cache toasty
|
2012-02-05 15:27:55 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
2012-10-01 02:53:26 +00:00
|
|
|
|
2015-03-04 23:50:10 +00:00
|
|
|
When an event is fired, your PHP callback receives as first argument an
|
|
|
|
`Composer\EventDispatcher\Event` object. This object has a `getName()` method
|
|
|
|
that lets you retrieve event name.
|
|
|
|
|
|
|
|
Depending on the script types (see list above) you will get various event
|
|
|
|
subclasses containing various getters with relevant data and associated
|
|
|
|
objects:
|
|
|
|
|
|
|
|
- Base class: [`Composer\EventDispatcher\Event`](https://getcomposer.org/apidoc/master/Composer/EventDispatcher/Event.html)
|
|
|
|
- Command Events: [`Composer\Script\Event`](https://getcomposer.org/apidoc/master/Composer/Script/Event.html)
|
|
|
|
- Installer Events: [`Composer\Installer\InstallerEvent`](https://getcomposer.org/apidoc/master/Composer/Installer/InstallerEvent.html)
|
|
|
|
- Package Events: [`Composer\Installer\PackageEvent`](https://getcomposer.org/apidoc/master/Composer/Installer/PackageEvent.html)
|
|
|
|
- Plugin Events:
|
|
|
|
- command: [`Composer\Plugin\CommandEvent`](https://getcomposer.org/apidoc/master/Composer/Plugin/CommandEvent.html)
|
|
|
|
- pre-file-download: [`Composer\Plugin\PreFileDownloadEvent`](https://getcomposer.org/apidoc/master/Composer/Plugin/PreFileDownloadEvent.html)
|
2013-12-18 21:58:20 +00:00
|
|
|
|
|
|
|
## Running scripts manually
|
|
|
|
|
|
|
|
If you would like to run the scripts for an event manually, the syntax is:
|
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
composer run-script [--dev] [--no-dev] script
|
|
|
|
```
|
2013-12-18 21:58:20 +00:00
|
|
|
|
2014-07-23 14:47:54 +00:00
|
|
|
For example `composer run-script post-install-cmd` will run any
|
|
|
|
**post-install-cmd** scripts that have been defined.
|
|
|
|
|
|
|
|
You can also give additional arguments to the script handler by appending `--`
|
|
|
|
followed by the handler arguments. e.g.
|
|
|
|
`composer run-script post-install-cmd -- --check` will pass`--check` along to
|
|
|
|
the script handler. Those arguments are received as CLI arg by CLI handlers,
|
|
|
|
and can be retrieved as an array via `$event->getArguments()` by PHP handlers.
|
|
|
|
|
|
|
|
## Writing custom commands
|
|
|
|
|
|
|
|
If you add custom scripts that do not fit one of the predefined event name
|
|
|
|
above, you can either run them with run-script or also run them as native
|
|
|
|
Composer commands. For example the handler defined below is executable by
|
|
|
|
simply running `composer test`:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"scripts": {
|
|
|
|
"test": "phpunit"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
> **Note:** Composer's bin-dir is pushed on top of the PATH so that binaries
|
|
|
|
> of dependencies are easily accessible as CLI commands when writing scripts.
|