1
0
Fork 0
composer/doc/articles/scripts.md

75 lines
2.1 KiB
Markdown
Raw Normal View History

2012-02-29 14:57:16 +00:00
<!--
tagline: Script are callbacks that are called before/after installing packages
-->
2012-02-05 15:27:55 +00:00
# Scripts
## What is a script?
A script is a callback (defined as a static method) that will be called
when the event it listens on is triggered.
**Scripts are only executed on the root package, not on the dependencies
that are installed.**
## Event types
- **pre-install-cmd**: occurs before the install command is executed.
- **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.
- **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.
## Defining scripts
Scripts are defined by adding the `scripts` key to a project's `composer.json`.
They are specified as an array of classes and static method names.
The classes used as scripts must be autoloadable via Composer's autoload
functionality.
Script definition example:
2012-02-29 14:56:53 +00:00
{
"scripts": {
"post-update-cmd": "MyVendor\\MyClass::postUpdate",
"post-package-install": [
"MyVendor\\MyClass::postPackageInstall"
]
2012-02-29 14:56:53 +00:00
}
2012-02-05 15:27:55 +00:00
}
The event handler receives a `Composer\Script\Event` object as an argument,
which gives you access to the `Composer\Composer` instance through the
`getComposer` method.
Using the previous example, here's an event listener example :
2012-02-05 15:27:55 +00:00
2012-02-29 14:56:53 +00:00
<?php
2012-02-05 15:27:55 +00:00
2012-02-29 14:56:53 +00:00
namespace MyVendor;
2012-02-05 15:27:55 +00:00
use Composer\Script\Event;
2012-02-29 14:56:53 +00:00
class MyClass
2012-02-05 15:27:55 +00:00
{
public static function postUpdate(Event $event)
2012-02-29 14:56:53 +00:00
{
$composer = $event->getComposer();
2012-02-29 14:56:53 +00:00
// do stuff
}
public static function postPackageInstall(Event $event)
2012-02-29 14:56:53 +00:00
{
$installedPackage = $event->getOperation()->getPackage();
// do stuff
}
2012-02-05 15:27:55 +00:00
}