Update scripts docs
parent
d43dd33632
commit
39ab5017aa
|
@ -0,0 +1,66 @@
|
||||||
|
# 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:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"scripts": {
|
||||||
|
"post-update-cmd": "MyVendor\\MyClass::postUpdate",
|
||||||
|
"post-package-install": ["MyVendor\\MyClass::postPackageInstall"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Script listener example:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MyVendor;
|
||||||
|
|
||||||
|
class MyClass
|
||||||
|
{
|
||||||
|
public static function postUpdate($event)
|
||||||
|
{
|
||||||
|
// do stuff
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function postPackageInstall($event)
|
||||||
|
{
|
||||||
|
$installedPackage = $event->getOperation()->getPackage();
|
||||||
|
// do stuff
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
|
@ -1,70 +0,0 @@
|
||||||
# Triggers
|
|
||||||
|
|
||||||
## What is a trigger?
|
|
||||||
|
|
||||||
A trigger is an event that runs a script in a static method, defined by a
|
|
||||||
project. This event is raised before and after each action (install, update).
|
|
||||||
|
|
||||||
|
|
||||||
## Where are the event types defined?
|
|
||||||
|
|
||||||
It is in the constant property in `Composer\Trigger\TriggerEvents` class.
|
|
||||||
|
|
||||||
|
|
||||||
## How is it defined?
|
|
||||||
|
|
||||||
It is defined by adding the `triggers` key in the `extra` key to a project's
|
|
||||||
`composer.json` or package's `composer.json`.
|
|
||||||
|
|
||||||
It is specified as an array of classes with her static method,
|
|
||||||
in associative array define the event's type.
|
|
||||||
|
|
||||||
The PSR-0 must be defined, otherwise the trigger will not be triggered.
|
|
||||||
|
|
||||||
For any given project:
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"extra": {
|
|
||||||
"triggers": {
|
|
||||||
"post_install": [
|
|
||||||
"MyVendor\\MyRootPackage\\MyClass::myStaticMethod"
|
|
||||||
],
|
|
||||||
"post_update": [
|
|
||||||
"MyVendor\\MyRootPackage\\MyClass::myStaticMethod2"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"autoload": {
|
|
||||||
"psr-0": {
|
|
||||||
"MyVendor\\MyRootPackage": "my/folder/path/that/contains/triggers/from/the/root/project"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Trigger Example:
|
|
||||||
|
|
||||||
```php
|
|
||||||
<?php
|
|
||||||
namespace MyVendor\MyRootPackage;
|
|
||||||
|
|
||||||
use Composer\Trigger\TriggerEvent;
|
|
||||||
|
|
||||||
class MyClass
|
|
||||||
{
|
|
||||||
public static function myStaticMethod(TriggerEvent $event)
|
|
||||||
{
|
|
||||||
// code...
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function myStaticMethod2(TriggerEvent $event)
|
|
||||||
{
|
|
||||||
// code...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Informations:
|
|
||||||
|
|
||||||
A declared trigger with non existent file or without registered namespace will be ignored.
|
|
Loading…
Reference in New Issue