使用Phake自动化PHP-简介

As developers, we often have to do repetitive tasks such as updating the database structure, seeding the database, writing CRUD code, running tests, and uploading files to a server. Wouldn’t it be great if we could automate these mundane tasks and proceed with solving the more important problems such as making our app more secure or more usable to our users?

作为开发人员,我们经常必须执行重复性的任务,例如更新数据库结构,为数据库设置种子,编写CRUD代码,运行测试以及将文件上传到服务器。 如果我们可以自动化这些平凡的任务并着手解决更重要的问题(例如使我们的应用程序更安全或对用户更易使用),那不是很好吗?

Phake, an automation tool written for PHP, can do those tasks for you. If you’re familiar with Ruby, it’s basically a Rake clone. In this two-part series I’m going to walk you through integrating Phake into your workflow. I will walk you through the installation, some of the basics in using Phake and then finally some real-world examples.

Phake是为PHP编写的自动化工具,可以为您完成这些任务。 如果您熟悉Ruby,则基本上是Rake克隆。 在这个由两部分组成的系列文章中,我将指导您将Phake集成到您的工作流程中。 我将引导您完成安装过程,使用Phake的一些基础知识以及最后的一些实际示例。

安装 (Installation)

Phake should be installed globally via Composer:

Phake应该通过Composer全局安装:

composer global require 'jaz303/phake=*'

This makes Phake accessible from any folder, and doesn’t require you to change your project’s composer.json file.

这使得Phake可以从任何文件夹访问,并且不需要您更改项目的composer.json文件。

If you don’t have access to the ‘composer’ command, install Composer globally.

如果您无权使用“ composer”命令, 请全局安装Composer

基本 (Basics)

To execute Phake tasks, you need to create a Phakefile. The Phakefile contains the configuration for the tasks that you want to execute. If you’ve used Grunt before, a Phakefile is similar to a Gruntfile.

要执行Phake任务,您需要创建一个Phakefile。 Phakefile包含要执行的任务的配置。 如果你已经使用咕噜之前,一个Phakefile类似于Gruntfile

An important note about the Phakefile is that it’s just a PHP file so you can just write it the same way as you do with your PHP projects.

关于Phakefile的重要说明是,它只是一个PHP文件,因此您可以像对PHP项目一样进行编写。

创建任务 (Creating Tasks)

You can create tasks by calling the task() method. This takes up the name of the task as the first argument, and the function to execute as the last argument.

您可以通过调用task()方法来创建任务。 这将任务名称作为第一个参数,并将要执行的函数作为最后一个参数。

<?php
task('task_a', function(){
  echo "Hi I'm task A!\n"; 
});

You can then execute it with the following command:

然后可以使用以下命令执行它:

phake task_a

This will then return the following output:

然后将返回以下输出:

Hi I'm task A!

依存关系 (Dependencies)

If one task depends on another task, you can supply the name of that task right after the main task:

如果一个任务依赖于另一个任务,则可以在主任务之后提供该任务的名称:

<?php
task('task_a', function(){
  echo "Hi I'm task A!\n"; 
});

task('task_b', 'task_a', function(){
  echo "Hi I'm task B! I need task A to execute first before I can do my thing!\n";
});

To execute the tasks in order you just have to call the task which has a dependency first. In this case task_b depends on task_a so we call it first:

要按顺序执行任务,您只需要先调用具有依赖性的任务即可。 在这种情况下, task_b取决于task_a因此我们首先将其称为:

phake task_b

Executing it will return the following output:

执行它会返回以下输出:

Hi I'm task A!
Hi I'm task B! I need task A to execute first before I can do my thing!

And you can keep on adding dependencies:

您可以继续添加依赖项:

<?php
task('task_a', function(){
  echo "I get to execute first!\n"; 
});


task('task_b', 'task_a', function(){
  echo "Second here!\n";
});

task('task_c', 'task_b', function(){
  echo "I'm the last one!\n";
});

Execute them by calling the the final task that needs to be called. In this case, it’s task_c that we want to execute last:

通过调用需要调用的最终任务来执行它们。 在这种情况下,我们要最后执行的是task_c

phake task_c

It will then return the following output:

然后它将返回以下输出:

I get to execute first!
Second here!
I'm the last one!

Note that with this method of declaring dependencies, calling task_b would result in task_a being called first. If you don’t want this to happen and you still want to execute a specific task separately without executing its dependencies first then you can declare it using the following method:

请注意,以声明依赖这种方法,调用task_b会导致task_a被先叫。 如果您不希望发生这种情况,而仍然想单独执行特定任务而不先执行其依赖项,则可以使用以下方法对其进行声明:

task('task_a', function(){
  echo "I get to execute first!\n"; 
});

task('task_b', function(){
  echo "Second here!\n";
});

task('task_c', 'task_a', 'task_b', function(){
  echo "I'm the last one!\n";
});

In the example above we are setting task_a, and task_b as the dependencies of task_c. Note that the order matters here. So the task right after the main task (task_a) will get executed first, and the one right next to it (task_b) will be the second and then finally the main task (task_c) is executed.

在上面的示例中,我们将task_atask_b设置为task_a的依赖task_c 。 请注意,这里的顺序很重要。 因此, task_a在主任务( task_a )之后的任务将被首先执行, task_b其后的任务( task_c )将是第二个,最后执行主任务( task_c )。

With Phake there’s another way of defining your dependencies: through the use of before or after blocks right after defining the main task. In this case our main task is to eat so we define the tasks that we want to execute before and after it under its declaration:

使用Phake,还有另一种定义依赖项的方法:在定义主要任务之后立即使用before或after块。 在这种情况下,我们的主要任务是eat因此我们在声明之前和之后定义要执行的任务:

task('eat', function(){
  echo "Yum!";
});

before('eat', function(){
  echo "Wash your hands before you eat\n";
});

after('eat', function(){
  echo "Brushy brush! brush!\n";
});

When you execute eat you get the following output:

执行eat将获得以下输出:

Wash your hands before you eat
Yum!
Brushy brush! brush!

分组任务 (Grouping Tasks)

With Phake you can also group related tasks together:

使用Phake,您还可以将相关任务分组在一起:

group('clean_the_house', function(){
  task('polish_furniture', function(){..});
  task('wash_the_clothes', function(){..});
  task('mop_the_floor', function(){..}); 
});

Grouped tasks can be called using the group name that you specified, followed by a colon, then the name of the task that you want to execute:

可以使用您指定的组名,后跟冒号,然后是您要执行的任务的名称来调用已分组的任务:

phake clean_the_house:polish_furniture

If you want to execute all of the tasks in a group you can just make the final task depend on the first and second task. In the example below, the final task that we want to execute is the mop_the_floor task so we make it depend on the polish_furniture and wash_the_clothes task:

如果要执行组中的所有任务,则可以使最终任务取决于第一个和第二个任务。 在下面的示例中,我们要执行的最后一个任务是mop_the_floor任务,因此我们使其取决于polish_furniturewash_the_clothes任务:

group('clean_the_house', function(){
  task('polish_furniture', function(){..});
  task('wash_the_clothes', function(){..});
  task('mop_the_floor', 'polish_furniture', 'wash_the_clothes' function(){..}); 
});

Then we simply call the mop_the_floor task from the terminal:

然后,我们只需从终端调用mop_the_floor任务:

phake clean_the_house:mop_the_floor

This will then call the tasks in the following order:

然后,将按以下顺序调用任务:

polish_furniture
wash_the_clothes
mop_the_floor

描述任务 (Describing Tasks)

After some time of using Phake, you might accumulate a bunch of tasks in your Phakefile, so its a good idea to have some sort of documentation. Lucky for us, Phake comes with a utility that allows us to describe what a specific Phake task does. You can use it by calling the desc method right before the declaration of the task that you want to describe:

使用Phake一段时间后,您可能会在Phakefile积累很多任务,因此拥有某种文档是一个好主意。 对我们来说幸运的是,Phake附带了一个实用程序,可让我们描述特定的Phake任务的功能。 您可以通过在要描述的任务的声明之前调用desc方法来使用它:

desc('Allows you to water the plants');
task('hose', function(){..}); 

desc('Allows you to wash the dish');
task('dish_washer', function(){..});

You can then list out the available tasks in your Phakefile with the following command:

然后,您可以使用以下命令列出Phakefile中的可用任务:

phake -T

It will return an output similar to the following:

它将返回类似于以下内容的输出:

hose              Allows you to water the plants
dish_washer       Allows you to wash the dish

将参数传递给任务 (Passing Arguments to Tasks)

To make the tasks more flexible we can also pass in arguments. This can be done by declaring a parameter in the function. This can then be used to access individual arguments that you pass in to the task:

为了使任务更加灵活,我们还可以传递参数。 这可以通过在函数中声明参数来完成。 然后可以使用它来访问传递给任务的各个参数:

task('brush_teeth', function($args){
  $motion = (!empty($args['motion'])) ? $args['motion'] : 'circular';
  $includes = (!empty($args['includes'])) ? $args['includes'] : '';

  brush($motion, $includes); 

});

Arguments can be passed by including a name-value pair right after the name of the task. If you wish to pass in more than 1 argument you can separate them by using a single space between the value of the first argument and the name of the second argument:

可以通过在任务名称之后添加名称/值对来传递参数。 如果希望传递多个参数,则可以通过在第一个参数的值和第二个参数的名称之间使用单个空格来分隔它们:

phake brush_teeth motion=horizontal includes=tongue,teeth,gums

If you need to pass in arguments with spaces between them you can simply wrap it up in single or double quotes:

如果您需要传入带有空格的参数,则可以将其用单引号或双引号引起来:

phake brush_teeth motion="circular horizontal and vertical"

结论 (Conclusion)

Now that we’ve seen what Phake is for and how we can execute tasks with it, we’ve readied the terrain for some real world applications in part two. Stay tuned!

既然我们已经了解了Phake的用途以及如何使用它执行任务,我们已经在第二部分中为一些实际应用程序做好了准备。 敬请关注!

翻译自: https://www.sitepoint.com/automate-php-phake-introduction/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值