3.3. 策略
这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。
请对照 原文链接 进行翻译
STRATEGIES
Introduction
Rocketeer has all the core tasks you'd expect from a deployer: creating a new release, testing it, migrating the database, etc. Most tasks have only one way to do things, but certain core ones have multiple paths they can use to accomplish their goals: those are the strategies.
The most important tasks using strategies are the Deploy and Update ones, by default they use your configured SCM (Git, Svn, etc.) to clone a clean new release on the server, but that's not the only strategy available. You can see what tasks use strategies and what options are available by calling the rocketeer strategies
command. It'll yield a table similar to the following:
Strategy | Implementation | Description |
---|---|---|
check | Php | Checks if the server is ready to receive a PHP application |
check | Ruby | Checks if the server is ready to receive a Ruby application |
deploy | Clone | Clones a fresh instance of the repository by SCM |
deploy | Copy | Copies the previously cloned instance of the repository and update it |
deploy | Sync | Uses rsync to create or update a release from the local files |
test | Phpunit | Run the tests with PHPUnit |
migrate | Artisan | Migrates your database with Laravel's Artisan CLI |
dependencies | Composer | Installs dependencies with Composer |
dependencies | Bundler | Installs dependencies with Bundler |
dependencies | Npm | Installs dependencies with NPM |
dependencies | Bower | Installs dependencies with Bower |
dependencies | Polyglot | Runs all of the above package managers if necessary |
dependencies | null | Runs none (note null must be in quoation marks IE 'null') |
You can configure which task uses which strategy in the strategies.php
file of your configuration folder.
Adding your own strategies
Rocketeer comes with a handful of available implementations for each strategy but sometimes you use a particular tool or procedure that isn't built-in. It's really easy to add your own strategies, there's only two steps really. Say you want to test your application with Grunt instead of PHPunit, in the case of a Node application per example.
First you'll create a class for your strategy extending Rocketeer\Abstracts\AbstractStrategy
. If Rocketeer is loaded as a Composer dependency of your project, the class can be anywhere as long as it's autoloaded, otherwise you need to create it under .rocketeer/strategies
:
<?php
namespace Acme;
use Rocketeer\Abstracts\Strategies\AbstractStrategy;
class GruntStrategy extends AbstractStrategy
{
// ...
}
Next we need to define which strategy this corresponds to, for this you'll implement the relevant interface from the Rocketeer\Interfaces\Strategies
namespace, in our case, TestStrategyInterface
:
<?php
namespace Acme;
use Rocketeer\Abstracts\Strategies\AbstractStrategy;
use Rocketeer\Interfaces\Strategies\TestStrategyInterface;
class GruntStrategy extends AbstractStrategy implements TestStrategyInterface
{
public function test()
{
// ...
}
}
All that is left to do is write the business logic. Strategies implements the same traits than Tasks so you have all the usual tools available, in particular binaries (see the relevant documentation section for more informations).
<?php
namespace Acme;
use Rocketeer\Abstracts\Strategies\AbstractStrategy;
use Rocketeer\Strategies\Interfaces\TestStrategyInterface;
class GruntStrategy extends AbstractStrategy implements TestStrategyInterface
{
public function test()
{
return $this->binary('grunt')->runForCurrentRelease('test');
}
}
This will run grunt test
on the configured connection. All that is left to do is to configure Rocketeer to use that strategy, in the strategies.php
file:
// Which strategy to use to test your application
'test' => 'Acme\Grunt',
Again, make sure your class is loaded somewhere, if it's in .rocketeer/strategies
, Rocketeer will load it by itself. If Rocketeer is a dependency of your application, make sure the class is loaded by Composer.
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
推荐文章: