3.6. 插件
这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。
请对照 原文链接 进行翻译
PLUGINS
You can add functionalities to Rocketeer or simply bundle common tasks into reusable modules by using the plugins system. A plugin at its core is a class implementing the Rocketeer\Abstracts\AbstractPlugin
abstract.
Adding a plugin
To add a plugin, you need to call the rocketeer plugin:install <package>
command, per example rocketeer plugin:install anahkiasen/rocketeer-slack
, the package being the Packagist/Github handle of the package.
Once this is done, add the plugin's class to the plugins
array in .rocketeer/config.php
:
'plugins' => array(
'Rocketeer\Plugins\Slack\RocketeerSlack',
),
Then, in most cases you'll need to configure said plugin. For this you'll want to publish its configuration in user land via the rocketeer plugin:publish <package>
command. Here we'll call rocketeer plugin:publish anahkiasen/rocketeer-slack
per example.
This will create the .rocketeer/plugins/rocketeers/rocketeer-slack
folder, with all the plugin's configuration files inside.
Creating a plugin
There's two methods a plugin will most likely have on its class are register(Container $app)
and onQueue(TasksQueue $queue)
.
- The first one will be used to bind eventual instances into Rocketeer's container, that is a facultative method that if overridden needs to return the Container at the end.
- The second one is used to add actions or tasks to Rocketeer : the TasksQueue class is the one behind the Rocketeer facade so most of the methods you're familiar with are available on it :
$queue->before('deploy', ...)
,$queue->add('MyCustomTask')
etc.
Here is an example dumbed-down version of the current Campfire plugin, using rcrowe/Campfire
as a dependency :
use rcrowe\Campfire;
use Illuminate\Container\Container;
use Rocketeer\Services\Tasks\TasksQueue;
use Rocketeer\Abstracts\AbstractPlugin;
class RocketeerCampfire extends AbstractPlugin
{
/**
* Bind additional classes to the Container
*
* @param Container $app
*
* @return void
*/
public function register(Container $app)
{
$app->bind('campfire', function ($app) {
return new Campfire(['domain' => '...', 'key' => '...', 'room' => '...']);
});
return $app;
}
/**
* Register Tasks with Rocketeer
*
* @param TasksQueue $queue
*
* @return void
*/
public function onQueue(TasksQueue $queue)
{
$queue->after('deploy', function ($task) {
$application = $task->rocketeer->getApplicationName();
$task->campfire->send($application. ' was deployed!');
});
}
}
如你所见,插件 非常 易用,你可以将其保存在某个地方并在不同项目之间重复使用。
插件配置
通过在插件的 src
文件夹中创建一个 config
文件夹,插件可以有自己的配置。你需要在类的构造函数中设置指向它的路径,例如:
public function __construct()
{
$this->configurationFolder = __DIR__.'/../../config';
}
随后,你可以在该文件夹中创建一个 config.php
文件,将配置存放到 PHP 数组中。插件的配置将通过任务中的 Config 类提供,在 my-plugin::
命名空间下,假设你的类是 RocketeerHipchat
,你将通过执行 $task->config->get('rocketeer-hipchat::myoption')
获得配置。
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
推荐文章: