如何写一个 Laravel 的 Artisan 命令行工具?

Artisan 是 Laravel 自带的命令行接口,它提供了许多实用的命令来帮助你构建 Laravel 应用

开始接触 Laravel 这个框架的时候,才发现居然能够使用命令行去执行一些操做,好比:建立文件,运行一个服务等.出于学习或者不能知足需求的时候,咱们就须要本身去写一个 Artisan 命令行。php

使用命令行输出 Hello

  • 在项目的根目录下面执行 php artisan make:command Hello。该命令的结果会在 app\Console 下面建立一个 Commands 的文件夹,而且建立 Hello.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Hello extends Command
{
    /**
     * 控制台命令 signature 的名称。
     *
     * @var string
     */
    protected $signature = 'hello';

    /**
     * 控制台命令说明
     *
     * @var string
     */
    protected $description = '这条命令将会输出一个 hello';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 执行命令
     *
     * @return mixed
     */
    public function handle()
    {
        var_dump('Hello');
    }
}
  • app/Console/Commands 下面的命令都会自动注册到 Artisan,看这个文件app/Console/Kernel.php
protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }

你也能够调用 load 方法添加你的其余 Commands 文件夹laravel

  • 执行 php artisan

clipboard.png

  • 执行 php artisan hello

clipboard.png
这样就很简单的写出了第一个 Artisan 命令行服务器

使用 Artisan 启动一个服务

  • 咱们建立一个服务命令 php artisan make:command SwooleStart
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class SwooleStart extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'swoole:start';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '启动 swoole';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $serv = new \swoole_server("127.0.0.1", 9501);

        //监听链接进入事件
        $serv->on('connect', function ($serv, $fd) {
            echo "Client: Connect.\n";
        });

        //监听数据接收事件
        $serv->on('receive', function ($serv, $fd, $from_id, $data) {
            $serv->send($fd, "Server: ".$data);
        });

        //监听链接关闭事件
        $serv->on('close', function ($serv, $fd) {
            echo "Client: Close.\n";
        });

        //启动服务器
        $serv->start();
    }
}
  • 执行 php artisan swoole:start

clipboard.png

  • 在打开一个命令行窗口 输入telnet 127.0.0.1 9501 用来监听这个端口,

clipboard.png

这样就成功的使用 Artisan 启动了一个服务。swoole

固然你也能够询问是否启动app

  • 使用 ask 方法
public function handle()
    {
        if ($this->ask('是否启动 swlloe,请输入 yes') != 'yes') {
            die;
        }
        $serv = new \swoole_server("127.0.0.1", 9501);

        //监听链接进入事件
        $serv->on('connect', function ($serv, $fd) {
            echo "Client: Connect.\n";
        });

        //监听数据接收事件
        $serv->on('receive', function ($serv, $fd, $from_id, $data) {
            $serv->send($fd, "Server: " . $data);
        });

        //监听链接关闭事件
        $serv->on('close', function ($serv, $fd) {
            echo "Client: Close.\n";
        });

        //启动服务器
        $serv->start();
    }

clipboard.png

像 Artisan 那样建立文件

  • 咱们先建立一个命令行文件 php artisan make:MakeController
  • 修改继承 Commanduse Illuminate\Console\GeneratorCommand;
<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;


class MakeController extends GeneratorCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'controller:make';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new controller class';



    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {

        return __DIR__.'/stubs/controller.stub';
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Http\Controllers';
    }

}
  • app\Console\commands下建立一个模板目录 stubs,里面存放要生成文件的模板,建立 controller.stub
<?php

namespace DummyNamespace;

use Illuminate\Http\Request;
use DummyRootNamespaceHttp\Controllers\Controller;

class DummyClass extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

}

在执行 Artisan 是会将 DummyClass 等以 Dummy 开头的替换为你的参数,替换代码能够看 GeneratorCommand,框架

  • getDefaultNamespace 修改你的文件存放目录
  • getStub是必须实现的方法。
  • 执行 php artisan controller:make HelloController 你将会在 Http\controller 下面看到你使用命令行建立的文件。
  • 建立文件的 handle 在继承的 GeneratorCommand 里面写好了,若是你还须要执行一些其余操做,在当前 command 里面写就行了。
  • Artlsan 还能够携带参数,还有一些其余的小方法,能够参考 Laravle 的文档。

Laravel 的 Artisan 命令行工具工具

相关文章
相关标签/搜索