• Laravel 搭建 Composer 包,实现配置 Config、门面 Facade、服务 Service、发布到 Packagist


    Laravel 搭建 Composer 包,实现配置 Config、门面 Facade、服务 Service、发布到 Packagist
     

    好久没写 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 JesseyChenPackageTest;
    
    class Test
    {
        public function execute()
        {
            echo 'this is a test. ';
        }
    }

    TestServiceProvider.php

    <?php
    
    namespace JesseyChenPackageTest;
    
    use IlluminateSupportServiceProvider;
    
    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...
           */
          JesseyChenPackageTestTestServiceProvider::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 JesseyChenPackageTest;
    
    use IlluminateSupportServiceProvider;
    
    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 JesseyChenPackageTest;
    
    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 JesseyChenPackageTest;
    
    use IlluminateSupportFacadesFacade as LaravelFacade;
    
    class Facade extends LaravelFacade
    {
        protected static function getFacadeAccessor()
        {
            return 'test';
        }
    }
    1. 在 app/config 目录注册 Facade 类
      'aliases' => [
          ....,
          'Test' => JesseyChenPackageTestFacade::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 clear-cache 
    composer require "jesseychen/package-test:1.0"
    Laravel 搭建 Composer 包,实现配置 Config、门面 Facade、服务 Service、发布到 packagist
     
    本作品采用《CC 协议》,转载必须注明作者和本文链接
  • 相关阅读:
    file_put_contents实现内容追加
    Laravel5.5 实现session配置
    easywechat在laravel框架中的应用-代码参考
    Laravel框架模型层表单验证
    工厂方法模式及php实现
    简单工厂模式及php实现
    Actionscript,AS3,MXML,Flex,Flex Builder,Flash Builder,Flash,AIR,Flash Player之关系
    php之依赖注入和控制反转
    php pdo操作数据库
    odbc连接数据库
  • 原文地址:https://www.cnblogs.com/mouseleo/p/14210021.html
Copyright © 2020-2023  润新知