Laravel 搭建 Composer 包,實現配置 Config、門面 Facade、服務 Service、釋出到 packagist

summer-1994發表於2019-06-12

好久沒寫php程式碼,有需求要寫一個內部使用的包,折騰了半天,用最簡單的程式碼寫下來備份一下流程

Step1. 初始化laravel專案

composer create-project laravel/laravel laravel-package

Step2. 建立目錄,初始化 composer.json

➜  mkdir -p  app/packages/jesseychen/package-test  
➜  cd app/packages/jesseychen/package-test
➜  composer init
➜  mkdir src
➜  touch README.md
➜  cd src
➜  touch Test.php TestServiceProvider.php
#  jesseychen是使用者名稱,表示誰的包
#  package-test 為擴充包的包名

123

Step3. 實現擴充包的內容

Test.php

<?php

namespace JesseyChen\PackageTest;

class Test
{
    public function execute()
    {
        echo 'this is a test. ';
    }
}

TestServiceProvider.php

<?php

namespace JesseyChen\PackageTest;

use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('test', function () {
            return new Test();
        });
    }
}

composer.json

{
    "name": "jesseychen/package-test",
    "description": "This is a test.",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "summer",
            "email": "chenjunxing1012@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4": {
            "JesseyChen\\PackageTest\\": "src/"
        }
    }
}

Step4. 在 laravel 專案引入本地擴充包

  1. 編輯 laravel 專案檔案的 composer.json

    .... ,
    "repositories": {
        "jesseyChen": {
            "type": "path",
            "url": "/var/www/laravel-package/app/packages/jesseychen/package-test"
        }
    }
    # 注意 “url” 為絕對路徑
  2. 安裝擴充包
    composer require jesseychen/package-test:dev-master

安裝完會發現 jesseychen/package-test 出現在 laravel/vendor

123

此時編輯app/packages/jesseychen/package-test目錄內的檔案,laravel/vendor/jesseychen/package-test目錄的檔案會跟著改變,相當做了檔案對映,這樣方便本地除錯

  1. 把service 註冊到 laravel 專案中,在 config/app.php 新增

    'providers' => [
    
        /*
         * Laravel Framework Service Providers...
         */
         ....
    
        /*
         * Package Service Providers...
         */
        JesseyChen\PackageTest\TestServiceProvider::class,
        /*
         * Application Service Providers...
         */
        ....
    ],

Step5. 測試

  1. 編輯 route/web.php
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    app('test')->execute();
});

123


新增配置檔案 config

  1. 在src下建立config 目錄來存取我們的配置引數檔案 config/test.php
<?php
return [
    'key' => 'jessey',
    'value' => 10
];
  1. 編輯TestServiceProvider.php.
<?php

namespace JesseyChen\PackageTest;

use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('test', function () {
            return new Test();
        });
    }

    public function boot()
    {
        $this->publishes([
            __DIR__ . '/config/test.php' => config_path('test.php'),
        ]);
    }
}
  1. 釋出配置檔案 php artisan vendor:publish
    123

  2. 編寫 Test.php
<?php

namespace JesseyChen\PackageTest;

class Test
{

    public function execute()
    {
        echo 'This is a test..';
    }

    public function getConfig()
    {
        var_dump(config('test'));
    }
}
  1. 測試 vim route/web.php
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    app('test')->getConfig();
});

新增門面 Facade

  1. src目錄下建立Facade.php
<?php

namespace JesseyChen\PackageTest;

use \Illuminate\Support\Facades\Facade as LaravelFacade;

class Facade extends LaravelFacade
{
    protected static function getFacadeAccessor()
    {
        return 'test';
    }
}
  1. 在 app/config 目錄註冊Facade類
    'aliases' => [
        ....,
        'Test' => JesseyChen\PackageTest\Facade::class
    ],
  2. 測試,vim route/web.php
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    Test::execute();
});

釋出擴充包到 https://packagist.org

1. 首選把 package-test包 上傳到 github 或者 coding

➜  cd app/packages/jesseychen/package-test 
➜  git init
➜  git add .
➜  git commit -m 'init package'
➜  git remote add origin https://github.com/JesseyChen/package-test.git
➜  git push -u origin master
➜  git tag 1.0
➜  git push --tag

vim package-test/composer.json ,新增 "version"

{
    ...
    "version": "1.0",
}

2. 上傳到 https://packagist.org/

  1. 點選右上角的submit
    123
  2. 把 github 上https的連結cv後 check
    123
  3. 成功後,出現下圖
    123
    以後更新擴充包,把本地新程式碼提交到github後,點選圖上的 update同步最新的程式碼

3. 測試

先刪除本地的包

  1. 刪除 composer.json 的 "repositories"、"require" 的 "jesseychen/package-test": "dev-master",
  2. 註釋掉 config/app.php 內剛剛新增的providers和aliases
  3. 執行 composer update

安裝

composer require "jesseychen/package-test:1.0"

123

相關文章