如何寫一個 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 命令行工具工具

相關文章
相關標籤/搜索