脚本 章节翻译结束

This commit is contained in:
成武 2014-02-21 14:05:10 +08:00
parent 903783d7c5
commit 131c7ef0e1
2 changed files with 47 additions and 68 deletions

View File

@ -28,8 +28,8 @@ Composer 中文文档
Host your own composer repository Host your own composer repository
- [插件](/cn-introduction/articles/plugins.md) - [插件](/cn-introduction/articles/plugins.md)
修改和扩展 Composer 的功能。 修改和扩展 Composer 的功能。
- [Scripts](/cn-introduction/articles/scripts.md) - [脚本](/cn-introduction/articles/scripts.md)
Script are callbacks that are called before/after installing packages 脚本是指一些 Composer 事件的回调,它们在安装资源包的过程中被触发执行。
- [Troubleshooting](/cn-introduction/articles/troubleshooting.md) - [Troubleshooting](/cn-introduction/articles/troubleshooting.md)
Solving problems Solving problems
- [Vendor Binaries](/cn-introduction/articles/vendor-binaries.md) - [Vendor Binaries](/cn-introduction/articles/vendor-binaries.md)

View File

@ -2,68 +2,51 @@
tagline: Script are callbacks that are called before/after installing packages tagline: Script are callbacks that are called before/after installing packages
--> -->
# Scripts # 脚本
## What is a script? ## 什么是脚本?
A script, in Composer's terms, can either be a PHP callback (defined as a 一个脚本,在 Composer 中,可以是一个 PHP 回调(定义为静态方法)或任何命令行可执行的命令。脚本对于在 Composer 运行过程中,执行一个资源包的自定义代码或包专用命令是非常有用的。
static method) or any command-line executable command. Scripts are useful
for executing a package's custom code or package-specific commands during
the Composer execution process.
**NOTE: Only scripts defined in the root package's `composer.json` are > **注意:**只有在根包的 `composer.json` 中定义的脚本才会被执行。即便根包的外部依赖定义了其自身的脚本Composer 也不会去执行这些额外的脚本。
executed. If a dependency of the root package specifies its own scripts,
Composer does not execute those additional scripts.**
<a name="event-names"></a> <a name="event-names"></a>
## Event names ## 事件名称
Composer fires the following named events during its execution process: Composer 在运行过程中将会触发以下事件:
- **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. | **pre-install-cmd** | 在 `install` 命令执行前触发。
- **post-update-cmd**: occurs after the `update` command is executed. | **post-install-cmd** | 在 `install` 命令执行后触发。
- **pre-status-cmd**: occurs before the `status` command is executed. | **pre-update-cmd** | 在 `update` 命令执行前触发。
- **post-status-cmd**: occurs after the `status` command is executed. | **post-update-cmd** | 在 `update` 命令执行后触发。
- **pre-package-install**: occurs before a package is installed. | **pre-status-cmd** | 在 `status` 命令执行前触发。
- **post-package-install**: occurs after a package is installed. | **post-status-cmd** | 在 `status` 命令执行后触发。
- **pre-package-update**: occurs before a package is updated. | **pre-package-install** | 在资源包安装前触发。
- **post-package-update**: occurs after a package is updated. | **post-package-install** | 在资源包安装后触发。
- **pre-package-uninstall**: occurs before a package has been uninstalled. | **pre-package-update** | 在资源包更新前触发。
- **post-package-uninstall**: occurs after a package has been uninstalled. | **post-package-update** | 在资源包更新后触发。
- **pre-autoload-dump**: occurs before the autoloader is dumped, either | **pre-package-uninstall** | 在资源包被卸载前触发。
during `install`/`update`, or via the `dump-autoload` command. | **post-package-uninstall** | 在资源包被卸载后触发。
- **post-autoload-dump**: occurs after the autoloader is dumped, either | **pre-autoload-dump** | 在自动加载器被转储前触发,无论是 `install`/`update` 还是 `dump-autoload` 命令都会触发。
during `install`/`update`, or via the `dump-autoload` command. | **post-autoload-dump** | 在自动加载器被转储后触发,无论是 `install`/`update` 还是 `dump-autoload` 命令都会触发。
- **post-root-package-install**: occurs after the root package has been | **post-root-package-install** | 在 `create-project` 命令期间,根包安装完成后触发。
installed, during the `create-project` command. | **post-create-project-cmd** | 在 `create-project` 命令执行后触发。
- **post-create-project-cmd**: occurs after the `create-project` command is
executed.
**NOTE: Composer makes no assumptions about the state of your dependencies > **注意:**Composer 不会去执行任何依赖包中定义的 `install``update` 相关脚本。因此你不应该在依赖包中申明 `pre-update-cmd``pre-install-cmd`。如果你需要在执行 `install``update` 命令前使用脚本,请确保它们已被定义在根包中。
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.**
## Defining scripts ## 定义脚本
The root JSON object in `composer.json` should have a property called `composer.json` 的根 JSON 对象中应该有一个名为 `"scripts"` 的属性,它包含有一系列的事件名称,以及对应的事件脚本。一个事件的脚本可以被定义为一个字符串(仅适用于单个脚本)或数组(单个或多个脚本)。
`"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.)
For any given event: 对于任何给定的事件:
- 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 - 一个脚本数组可以包含 PHP 回调和命令行可执行命令。
and command-line executables commands. - 由 PHP 类文件包含的回调,其存放的位置必须确保 Composer 能够正确的载入。
- PHP classes containing defined callbacks must be autoloadable via Composer's
autoload functionality.
Script definition example: 脚本定义实例:
{ {
"scripts": { "scripts": {
@ -78,8 +61,7 @@ Script definition example:
} }
} }
Using the previous definition example, here's the class `MyVendor\MyClass` 使用前面定义的例子,这里的 `MyVendor\MyClass` 类,就可以被使用来执行 PHP 的回调:
that might be used to execute the PHP callbacks:
<?php <?php
@ -107,19 +89,16 @@ that might be used to execute the PHP callbacks:
} }
} }
When an event is fired, Composer's internal event handler receives a 当一个事件被触发Composer 的内部事件处理程序将接收一个 `Composer\Script\Event` 对象,这是传递给您的 PHP 回调的第一个参数。这个 `Event` 对象拥有一些 getter 方法来帮助你取得当前事件的上下文:
`Composer\Script\Event` object, which is passed as the first argument to your
PHP callback. This `Event` object has getters for other contextual objects:
- `getComposer()`: returns the current instance of `Composer\Composer` - `getComposer()`: 返回当前的 `Composer\Composer` 对象实例。
- `getName()`: returns the name of the event being fired as a string - `getName()`: 返回事件名称的字符串。
- `getIO()`: returns the current input/output stream which implements - `getIO()`: 返回当前的 输入\输出 流,它实现了 `Composer\IO\IOInterface` 接口,以便在控制台中使用。
`Composer\IO\IOInterface` for writing to the console
## Running scripts manually ## 手动运行脚本
If you would like to run the scripts for an event manually, the syntax is: 如果你想手动运行事件脚本,可以使用下面的语法结构:
$ composer run-script [--dev] [--no-dev] script $ composer run-script [--dev] [--no-dev] script
For example `composer run-script post-install-cmd` will run any **post-install-cmd** scripts that have been defined. 例如 `composer run-script post-install-cmd` 将会运行所有 **post-install-cmd** 事件下定义的脚本。