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
|
|
|
|
2022-10-28 12:24:55 +00:00
|
|
|
As of Composer 2.5 scripts can also be Symfony Console Command classes,
|
|
|
|
which allows you to easily run them including passing options. This is
|
|
|
|
however not recommended for handling events.
|
|
|
|
|
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
|
|
|
|
|
2016-10-19 06:40:06 +00:00
|
|
|
- **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.
|
2020-04-12 11:27:48 +00:00
|
|
|
- **pre-status-cmd**: occurs before the `status` command is executed.
|
2015-09-20 13:11:09 +00:00
|
|
|
- **post-status-cmd**: occurs after the `status` command has been executed.
|
2015-03-04 23:50:10 +00:00
|
|
|
- **pre-archive-cmd**: occurs before the `archive` command is executed.
|
2015-09-20 13:11:09 +00:00
|
|
|
- **post-archive-cmd**: occurs after the `archive` command has been executed.
|
2016-10-19 06:40:06 +00:00
|
|
|
- **pre-autoload-dump**: occurs before the autoloader is dumped, either during
|
|
|
|
`install`/`update`, or via the `dump-autoload` command.
|
2015-09-20 13:11:09 +00:00
|
|
|
- **post-autoload-dump**: occurs after the autoloader has been dumped, either
|
2013-02-22 17:44:50 +00:00
|
|
|
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
|
2020-11-23 20:41:52 +00:00
|
|
|
installed during the `create-project` command (but before its
|
|
|
|
dependencies are installed).
|
2015-09-20 13:11:09 +00:00
|
|
|
- **post-create-project-cmd**: occurs after the `create-project` command has
|
|
|
|
been executed.
|
2015-03-04 23:50:10 +00:00
|
|
|
|
|
|
|
### Installer Events
|
|
|
|
|
2020-02-12 13:35:31 +00:00
|
|
|
- **pre-operations-exec**: occurs before the install/upgrade/.. operations
|
2022-02-16 13:58:24 +00:00
|
|
|
are executed when installing a lock file. Plugins that need to hook into
|
|
|
|
this event will need to be installed globally to be usable, as otherwise
|
|
|
|
they would not be loaded yet when a fresh install of a project happens.
|
2015-03-04 23:50:10 +00:00
|
|
|
|
|
|
|
### Package Events
|
|
|
|
|
|
|
|
- **pre-package-install**: occurs before a package is installed.
|
2015-06-06 10:06:14 +00:00
|
|
|
- **post-package-install**: occurs after a package has been installed.
|
2015-03-04 23:50:10 +00:00
|
|
|
- **pre-package-update**: occurs before a package is updated.
|
2015-06-06 10:06:14 +00:00
|
|
|
- **post-package-update**: occurs after a package has been updated.
|
|
|
|
- **pre-package-uninstall**: occurs before a package is uninstalled.
|
2015-03-04 23:50:10 +00:00
|
|
|
- **post-package-uninstall**: occurs after a package has been uninstalled.
|
|
|
|
|
|
|
|
### Plugin Events
|
|
|
|
|
2016-04-22 19:30:24 +00:00
|
|
|
- **init**: occurs after a Composer instance is done being initialized.
|
2015-03-04 23:50:10 +00:00
|
|
|
- **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
|
2018-11-12 14:34:54 +00:00
|
|
|
you to manipulate the `HttpDownloader` object prior to downloading files
|
2015-03-04 23:50:10 +00:00
|
|
|
based on the URL to be downloaded.
|
2020-04-14 21:51:04 +00:00
|
|
|
- **post-file-download**: occurs after package dist files are downloaded and
|
|
|
|
allows you to perform additional checks on the file if required.
|
2018-01-24 14:21:46 +00:00
|
|
|
- **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.
|
2020-01-30 21:14:19 +00:00
|
|
|
- **pre-pool-create**: occurs before the Pool of packages is created, and lets
|
2021-12-19 14:15:21 +00:00
|
|
|
you filter the list of packages that is going to enter the Solver.
|
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
|
2016-04-20 17:25:06 +00:00
|
|
|
corresponding scripts. An event's scripts can be defined as either a string
|
2012-10-22 18:24:05 +00:00
|
|
|
(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.
|
2022-10-28 12:24:55 +00:00
|
|
|
- PHP classes and commands 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
|
|
|
|
2021-02-24 20:17:05 +00:00
|
|
|
**Note:** During a Composer `install` or `update` command run, a variable named
|
2016-10-19 06:40:06 +00:00
|
|
|
`COMPOSER_DEV_MODE` will be added to the environment. If the command was run
|
|
|
|
with the `--no-dev` flag, this variable will be set to 0, otherwise it will be
|
2021-02-24 20:17:05 +00:00
|
|
|
set to 1. The variable is also available while `dump-autoload` runs, and it
|
2021-12-19 14:15:21 +00:00
|
|
|
will be set to the same as the last `install` or `update` was run in.
|
2016-10-17 19:29:52 +00:00
|
|
|
|
2016-04-24 14:48:48 +00:00
|
|
|
## Event classes
|
|
|
|
|
2016-04-20 17:25:06 +00:00
|
|
|
When an event is fired, your PHP callback receives as first argument a
|
2015-03-04 23:50:10 +00:00
|
|
|
`Composer\EventDispatcher\Event` object. This object has a `getName()` method
|
2016-04-24 14:48:48 +00:00
|
|
|
that lets you retrieve the event name.
|
2015-03-04 23:50:10 +00:00
|
|
|
|
2016-04-24 14:48:48 +00:00
|
|
|
Depending on the [script types](#event-names) you will get various event
|
2015-03-04 23:50:10 +00:00
|
|
|
subclasses containing various getters with relevant data and associated
|
|
|
|
objects:
|
|
|
|
|
2021-10-14 14:21:47 +00:00
|
|
|
- Base class: [`Composer\EventDispatcher\Event`](https://github.com/composer/composer/blob/main/src/Composer/EventDispatcher/Event.php)
|
|
|
|
- Command Events: [`Composer\Script\Event`](https://github.com/composer/composer/blob/main/src/Composer/Script/Event.php)
|
|
|
|
- Installer Events: [`Composer\Installer\InstallerEvent`](https://github.com/composer/composer/blob/main/src/Composer/Installer/InstallerEvent.php)
|
|
|
|
- Package Events: [`Composer\Installer\PackageEvent`](https://github.com/composer/composer/blob/main/src/Composer/Installer/PackageEvent.php)
|
2015-03-04 23:50:10 +00:00
|
|
|
- Plugin Events:
|
2021-10-14 14:21:47 +00:00
|
|
|
- init: [`Composer\EventDispatcher\Event`](https://github.com/composer/composer/blob/main/src/Composer/EventDispatcher/Event.php)
|
|
|
|
- command: [`Composer\Plugin\CommandEvent`](https://github.com/composer/composer/blob/main/src/Composer/Plugin/CommandEvent.php)
|
|
|
|
- pre-file-download: [`Composer\Plugin\PreFileDownloadEvent`](https://github.com/composer/composer/blob/main/src/Composer/Plugin/PreFileDownloadEvent.php)
|
|
|
|
- post-file-download: [`Composer\Plugin\PostFileDownloadEvent`](https://github.com/composer/composer/blob/main/src/Composer/Plugin/PostFileDownloadEvent.php)
|
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:
|
|
|
|
|
2022-08-20 10:23:00 +00:00
|
|
|
```shell
|
2021-10-25 11:11:35 +00:00
|
|
|
php composer.phar run-script [--dev] [--no-dev] script
|
2014-05-19 10:17:07 +00:00
|
|
|
```
|
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
|
2019-04-30 11:46:17 +00:00
|
|
|
**post-install-cmd** scripts and [plugins](plugins.md) that have been defined.
|
2014-07-23 14:47:54 +00:00
|
|
|
|
|
|
|
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
|
2020-10-23 19:52:05 +00:00
|
|
|
running `composer test`:
|
2014-07-23 14:47:54 +00:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"scripts": {
|
2022-10-28 12:24:55 +00:00
|
|
|
"test": "phpunit",
|
|
|
|
"do-something": "MyVendor\\MyClass::doSomething"
|
|
|
|
"my-cmd": "MyVendor\\MyCommand"
|
2014-07-23 14:47:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-12-07 11:21:41 +00:00
|
|
|
Similar to the `run-script` command you can give additional arguments to scripts,
|
|
|
|
e.g. `composer test -- --filter <pattern>` will pass `--filter <pattern>` along
|
|
|
|
to the `phpunit` script.
|
|
|
|
|
2022-10-28 12:24:55 +00:00
|
|
|
Using a PHP method via `composer do-something arg` lets you execute a
|
|
|
|
`static function doSomething(\Composer\Script\Event $event)` and `arg` becomes
|
|
|
|
available in `$event->getArguments()`. This however does not let you easily pass
|
|
|
|
custom options in the form of `--flags`.
|
|
|
|
|
|
|
|
Using a [symfony/console](https://packagist.org/packages/symfony/console) `Command`
|
|
|
|
class you can define and access arguments and options more easily.
|
|
|
|
|
|
|
|
For example with the command below you can then simply call `composer my-cmd
|
|
|
|
--arbitrary-flag` without even the need for a `--` separator. To be detected
|
|
|
|
as symfony/console commands the class name must end with `Command` and extend
|
|
|
|
symfony's `Command` class. Also note that this will run using Composer's built-in
|
|
|
|
symfony/console version which may not match the one you have required in your
|
|
|
|
project, and may change between Composer minor releases. If you need more
|
|
|
|
safety guarantees you should rather use your own binary file that runs your own
|
|
|
|
symfony/console version in isolation in its own process then.
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MyVendor;
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
class MyCommand extends Command
|
|
|
|
{
|
|
|
|
protected function configure(): void
|
|
|
|
{
|
|
|
|
$this->setDefinition([
|
|
|
|
new InputOption('arbitrary-flag', null, InputOption::VALUE_NONE, 'Example flag'),
|
|
|
|
new InputArgument('foo', InputArgument::OPTIONAL, 'Optional arg'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
{
|
|
|
|
if ($input->getOption('arbitrary-flag')) {
|
2024-08-21 16:49:52 +00:00
|
|
|
$output->writeln('The flag was used');
|
2022-10-28 12:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-12-18 10:39:05 +00:00
|
|
|
> **Note:** Before executing scripts, Composer's bin-dir is temporarily pushed
|
|
|
|
> on top of the PATH environment variable so that binaries of dependencies
|
2020-10-23 19:52:05 +00:00
|
|
|
> are directly accessible. In this example no matter if the `phpunit` binary is
|
2016-12-18 10:39:05 +00:00
|
|
|
> actually in `vendor/bin/phpunit` or `bin/phpunit` it will be found and executed.
|
2015-11-09 10:35:48 +00:00
|
|
|
|
2022-10-28 12:24:55 +00:00
|
|
|
|
|
|
|
## Managing the process timeout
|
|
|
|
|
2019-04-10 17:44:35 +00:00
|
|
|
Although Composer is not intended to manage long-running processes and other
|
|
|
|
such aspects of PHP projects, it can sometimes be handy to disable the process
|
|
|
|
timeout on custom commands. This timeout defaults to 300 seconds and can be
|
2019-05-14 08:18:13 +00:00
|
|
|
overridden in a variety of ways depending on the desired effect:
|
|
|
|
|
|
|
|
- disable it for all commands using the config key `process-timeout`,
|
|
|
|
- disable it for the current or future invocations of composer using the
|
|
|
|
environment variable `COMPOSER_PROCESS_TIMEOUT`,
|
|
|
|
- for a specific invocation using the `--timeout` flag of the `run-script` command,
|
|
|
|
- using a static helper for specific scripts.
|
2019-04-10 17:44:35 +00:00
|
|
|
|
2019-05-13 13:26:27 +00:00
|
|
|
To disable the timeout for specific scripts with the static helper directly in
|
|
|
|
composer.json:
|
2019-04-10 17:44:35 +00:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"scripts": {
|
|
|
|
"test": [
|
|
|
|
"Composer\\Config::disableProcessTimeout",
|
|
|
|
"phpunit"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-05-13 13:26:27 +00:00
|
|
|
To disable the timeout for every script on a given project, you can use the
|
|
|
|
composer.json configuration:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"config": {
|
|
|
|
"process-timeout": 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
It's also possible to set the global environment variable to disable the timeout
|
|
|
|
of all following scripts in the current terminal environment:
|
|
|
|
|
2022-08-20 10:23:00 +00:00
|
|
|
```shell
|
2019-05-13 13:26:27 +00:00
|
|
|
export COMPOSER_PROCESS_TIMEOUT=0
|
|
|
|
```
|
|
|
|
|
2019-05-14 08:19:37 +00:00
|
|
|
To disable the timeout of a single script call, you must use the `run-script` composer
|
2019-05-13 13:26:27 +00:00
|
|
|
command and specify the `--timeout` parameter:
|
|
|
|
|
2022-08-20 10:23:00 +00:00
|
|
|
```shell
|
2021-10-25 11:11:35 +00:00
|
|
|
php composer.phar run-script --timeout=0 test
|
2019-05-13 13:26:27 +00:00
|
|
|
```
|
|
|
|
|
2015-11-14 14:02:47 +00:00
|
|
|
## Referencing scripts
|
|
|
|
|
|
|
|
To enable script re-use and avoid duplicates, you can call a script from another
|
|
|
|
one by prefixing the command name with `@`:
|
2015-11-09 10:35:48 +00:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"scripts": {
|
|
|
|
"test": [
|
|
|
|
"@clearCache",
|
|
|
|
"phpunit"
|
|
|
|
],
|
|
|
|
"clearCache": "rm -rf cache/*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2016-04-11 18:03:32 +00:00
|
|
|
|
2018-11-26 11:35:41 +00:00
|
|
|
You can also refer a script and pass it new arguments:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2022-08-20 10:23:00 +00:00
|
|
|
"scripts": {
|
|
|
|
"tests": "phpunit",
|
|
|
|
"testsVerbose": "@tests -vvv"
|
|
|
|
}
|
2018-11-26 11:35:41 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-04-11 18:03:32 +00:00
|
|
|
## Calling Composer commands
|
|
|
|
|
|
|
|
To call Composer commands, you can use `@composer` which will automatically
|
|
|
|
resolve to whatever composer.phar is currently being used:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"scripts": {
|
|
|
|
"test": [
|
|
|
|
"@composer install",
|
|
|
|
"phpunit"
|
2016-09-02 11:59:22 +00:00
|
|
|
]
|
2016-04-11 18:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
One limitation of this is that you can not call multiple composer commands in
|
|
|
|
a row like `@composer install && @composer foo`. You must split them up in a
|
|
|
|
JSON array of commands.
|
2016-12-11 15:14:36 +00:00
|
|
|
|
|
|
|
## Executing PHP scripts
|
|
|
|
|
|
|
|
To execute PHP scripts, you can use `@php` which will automatically
|
|
|
|
resolve to whatever php process is currently being used:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"scripts": {
|
|
|
|
"test": [
|
|
|
|
"@php script.php",
|
|
|
|
"phpunit"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
One limitation of this is that you can not call multiple commands in
|
|
|
|
a row like `@php install && @php foo`. You must split them up in a
|
|
|
|
JSON array of commands.
|
2017-09-12 03:17:08 +00:00
|
|
|
|
2019-08-02 18:41:50 +00:00
|
|
|
You can also call a shell/bash script, which will have the path to
|
|
|
|
the PHP executable available in it as a `PHP_BINARY` env var.
|
|
|
|
|
2024-09-18 12:44:55 +00:00
|
|
|
## Controlling additional arguments
|
|
|
|
|
2024-10-03 14:17:30 +00:00
|
|
|
As of Composer 2.8, you can control how additional arguments are passed to script commands.
|
|
|
|
|
2024-09-18 12:44:55 +00:00
|
|
|
When running scripts like `composer script-name arg arg2` or `composer script-name -- --option`,
|
|
|
|
Composer will by default append `arg`, `arg2` and `--option` to the script's command.
|
|
|
|
|
|
|
|
If you do not want these args in a given command, you can put `@no_additional_args`
|
|
|
|
anywhere in it, that will remove the default behavior and that flag will be removed
|
|
|
|
as well before running the command.
|
|
|
|
|
|
|
|
If you want the args to be added somewhere else than at the very end, then you can put
|
|
|
|
`@additional_args` to be able to choose exactly where they go.
|
|
|
|
|
|
|
|
For example running `composer run-commands ARG` with the below config:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"scripts": {
|
|
|
|
"run-commands": [
|
|
|
|
"echo hello @no_additional_args",
|
|
|
|
"command-with-args @additional_args && do-something-without-args --here"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Would end up executing these commands:
|
|
|
|
|
|
|
|
```
|
|
|
|
echo hello
|
|
|
|
command-with-args ARG && do-something-without-args --here
|
|
|
|
```
|
|
|
|
|
2019-12-22 11:32:04 +00:00
|
|
|
## Setting environment variables
|
|
|
|
|
2019-12-22 11:33:58 +00:00
|
|
|
To set an environment variable in a cross-platform way, you can use `@putenv`:
|
2019-12-22 11:32:04 +00:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"scripts": {
|
|
|
|
"install-phpstan": [
|
|
|
|
"@putenv COMPOSER=phpstan-composer.json",
|
2024-03-11 15:41:05 +00:00
|
|
|
"@composer install --prefer-dist"
|
2019-12-22 11:32:04 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-02-15 10:42:14 +00:00
|
|
|
## Custom descriptions
|
2017-09-12 03:17:08 +00:00
|
|
|
|
2017-12-18 09:14:24 +00:00
|
|
|
You can set custom script descriptions with the following in your `composer.json`:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2017-12-18 09:46:07 +00:00
|
|
|
"scripts-descriptions": {
|
2017-12-18 09:14:24 +00:00
|
|
|
"test": "Run all tests!"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2017-09-12 03:17:08 +00:00
|
|
|
|
2020-04-22 15:08:33 +00:00
|
|
|
The descriptions are used in `composer list` or `composer run -l` commands to
|
|
|
|
describe what the scripts do when the command is run.
|
2020-04-22 11:39:10 +00:00
|
|
|
|
2017-09-12 03:17:08 +00:00
|
|
|
> **Note:** You can only set custom descriptions of custom commands.
|
2023-10-27 09:36:59 +00:00
|
|
|
|
2024-02-15 10:42:14 +00:00
|
|
|
## Custom aliases
|
2023-10-27 09:36:59 +00:00
|
|
|
|
2024-02-15 10:42:14 +00:00
|
|
|
As of Composer 2.7, you can set custom script aliases with the following in your `composer.json`:
|
2023-10-27 09:36:59 +00:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"scripts-aliases": {
|
|
|
|
"phpstan": ["stan", "analyze"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The aliases provide alternate command names.
|
|
|
|
|
|
|
|
> **Note:** You can only set custom aliases of custom commands.
|