如何開發一個 Notadd 擴充套件

依劍聽雨發表於2017-03-07

擴充套件程式碼結構請參考專案 http://git.oschina.net/notadd/duoshuo

擴充套件將包含如下幾個部分:

  • 擴充套件注入
  • 路由注入
  • CSRF注入

擴充套件安裝

對於 擴充套件 的安裝,僅需將擴充套件檔案按照下面的目錄結構,放置到目錄 extensions 下,然後執行命令 composer update 即可。

目錄結構

目錄結構如下:

vendor                                                       廠商目錄
    extension                                                外掛目錄
        configuations                                        可載入配置檔案目錄
        resources                                            資源目錄
            translations                                     翻譯檔案目錄
            views                                            檢視目錄
        src                                                  原始碼目錄
            Extension                                        擴充套件服務提供者定義檔案
        composer.json                                        Composer 配置檔案

一個 Notadd 的擴充套件,是一個符合 composer 規範的包,所以,擴充套件對第三方程式碼有依賴時,可以在 composer.json 中的 require 節點中新增第三方的包。

而作為一個符合 Notadd 擴充套件定義規範的包,composer.json 需擁有如下資訊:

  • type 必須為 notadd-module
  • require 中必須新增包 notadd/installers

程式碼參考如下(來自擴充套件根目錄下的檔案 composer.json, 檔案中不應該包含 // 的註釋資訊,此處僅作為說明)

{
    "name": "notadd/duoshuo",
    "description": "Notadd Extension for Duoshuo.",
    "type": "notadd-extension",                                                      // type 必須設定為 notadd-extension
    "keywords": ["notadd", "duoshuo", "extension"],
    "homepage": "https://notadd.com",
    "license": "Apache-2.0",
    "authors": [
        {
            "name": "Notadd",
            "email": "notadd@ibenchu.com"
        }
    ],
    "autoload": {
        "psr-4": {
            "Notadd\\Duoshuo\\": "src/"
        }
    },
    "require": {
        "php": ">=7.0",
        "notadd/installers": "0.1.*"                                                // 必須依賴包 notadd/installers
    }
}

擴充套件注入

所謂 擴充套件注入 ,是 Notadd 在載入擴充套件的時候,會檢索擴充套件目錄下的類 ModuleServiceProvider,此類必須命名為 ModuleServiceProvider,且需放在原始碼根目錄中,且名稱空間必須為 composer.json 的中 autoload 節點定義的符合 psr-4 規範的名稱空間,否則 Notadd 將不能正確載入擴充套件!

類 Extension 的父類必須為 Notadd\Foundation\Extension\Abstracts\Extension ,且必須包含 boot 方法。

類 Extension 的程式碼參考如下:

<?php
/**
 * This file is part of Notadd.
 *
 * @author TwilRoad <269044570@qq.com>
 * @copyright (c) 2017, iBenchu.org
 * @datetime 2017-02-21 11:28
 */
namespace Notadd\Duoshuo;

use Notadd\Foundation\Extension\Abstracts\Extension as AbstractExtension;

/**
 * Class Extension.
 */
class Extension extends AbstractExtension
{
    /**
     * Boot provider.
     */
    public function boot()
    {
    }
}

路由注入

由於 Notadd 的路由注入,需要實現事件 RouteRegister,並在事件監聽中新增 路由 。

所以,所謂的路由注入,實際是在類 Extension 實現事件 RouteRegister 的訂閱,並在事件訂閱類中註冊擴充套件所需要的路由。

類 Extension 的程式碼參考如下:

<?php
/**
 * This file is part of Notadd.
 *
 * @author TwilRoad <269044570@qq.com>
 * @copyright (c) 2017, iBenchu.org
 * @datetime 2017-02-21 11:28
 */
namespace Notadd\Duoshuo;

use Illuminate\Events\Dispatcher;
use Notadd\Duoshuo\Listeners\RouteRegister;
use Notadd\Foundation\Extension\Abstracts\Extension as AbstractExtension;

/**
 * Class Extension.
 */
class Extension extends AbstractExtension
{
    /**
     * Boot provider.
     */
    public function boot()
    {
        $this->app->make(Dispatcher::class)->subscribe(RouteRegister::class);
    }
}

事件訂閱類 RouteRegister 的程式碼參考如下:

<?php
/**
 * This file is part of Notadd.
 *
 * @author TwilRoad <269044570@qq.com>
 * @copyright (c) 2017, iBenchu.org
 * @datetime 2017-02-21 11:50
 */
namespace Notadd\Duoshuo\Listeners;

use Notadd\Duoshuo\Controllers\DuoshuoController;
use Notadd\Foundation\Routing\Abstracts\RouteRegistrar as AbstractRouteRegistrar;

/**
 * Class RouteRegister.
 */
class RouteRegister extends AbstractRouteRegistrar
{
    /**
     * Handle Route Registrar.
     */
    public function handle()
    {
        $this->router->group(['middleware' => ['auth:api', 'cross', 'web'], 'prefix' => 'api/duoshuo'], function () {
            $this->router->post('backup', DuoshuoController::class . '@backup');
            $this->router->post('configuration', DuoshuoController::class . '@configuration');
            $this->router->post('number', DuoshuoController::class . '@number');
        });
    }
}

CSRF注入

所謂的CSRF注入,實際是在類 Extension 實現事件 CsrfTokenRegister 的訂閱,並在事件訂閱類中註冊擴充套件所需要的路由。

類 Extension 的程式碼參考如下:

<?php
/**
 * This file is part of Notadd.
 *
 * @author TwilRoad <269044570@qq.com>
 * @copyright (c) 2017, iBenchu.org
 * @datetime 2017-02-21 11:28
 */
namespace Notadd\Duoshuo;

use Illuminate\Events\Dispatcher;
use Notadd\Duoshuo\Listeners\CsrfTokenRegister;
use Notadd\Foundation\Extension\Abstracts\Extension as AbstractExtension;

/**
 * Class Extension.
 */
class Extension extends AbstractExtension
{
    /**
     * Boot provider.
     */
    public function boot()
    {
        $this->app->make(Dispatcher::class)->subscribe(CsrfTokenRegister::class);
    }
}

事件訂閱類 CsrfTokenRegister 的程式碼參考如下:

<?php
/**
 * This file is part of Notadd.
 *
 * @author TwilRoad <269044570@qq.com>
 * @copyright (c) 2017, iBenchu.org
 * @datetime 2017-02-23 19:38
 */
namespace Notadd\Duoshuo\Listeners;

use Notadd\Foundation\Event\Abstracts\EventSubscriber;
use Notadd\Foundation\Http\Events\CsrfTokenRegister as CsrfTokenRegisterEvent;

/**
 * Class CsrfTokenRegister.
 */
class CsrfTokenRegister extends EventSubscriber
{
    /**
     * Name of event.
     *
     * @throws \Exception
     * @return string|object
     */
    protected function getEvent()
    {
        return CsrfTokenRegisterEvent::class;
    }

    /**
     * Register excepts.
     *
     * @param $event
     */
    public function handle(CsrfTokenRegisterEvent $event)
    {
        $event->registerExcept('api/duoshuo*');
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章