自定義元件服務註冊配置

Rxy-development發表於2019-09-11
<?php
namespace Impecty\LaravelShop\Wap\Member\Providers;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use EasyWeChat\OfficialAccount\Application as OfficialAccount;

class MemberServiceProvide extends ServiceProvider
{
    // member元件需要注入的中介軟體
    protected $routeMiddleware = [
        'wechat.oauth' => \Overtrue\LaravelWeChat\Middleware\OAuthAuthenticate::class,
    ];

    //定義自定義artisan命令的路徑
    protected $commands = [
        \Impecty\LaravelShop\Wap\Member\Console\Commands\InstallCommand::class,
    ];

    protected $middlewareGroups = [];
    //註冊方法
    public function register()
    {
        //註冊路由
        $this->registerRoutes();
        //載入config配置檔案
        $this->mergeConfigFrom(__DIR__.'/../Config/member.php','wap.member');
        //註冊路由中介軟體
        $this->registerRouteMiddleware();
        //註冊釋出配置檔案方法
        $this->registerPublish();
        //重寫wechat.official_account.default,使微信設定直接在wap\member配置檔案中獲取,而不是在.env
        $this->app->singleton("wechat.official_account.default", function ($laravelApp) {
            $app = new OfficialAccount(array_merge(config('wechat.defaults', []), config("wechat.official_account.default", [])));
            if (config('wechat.defaults.use_laravel_cache')) {
                $app['cache'] = $laravelApp['cache.store'];
            }
            $app['request'] = $laravelApp['request'];
            return $app;
        });
    }

    //執行方法
    public function boot()
    {
        $this->LoadMemberConfig();
        $this->loadMigration();
        $this->commands($this->commands);

    }

    //註冊釋出配置檔案方法
    public function registerPublish()
    {
        if($this->app->runningInConsole()){
            $this->publishes([__DIR__.'/../Config' => config_path('wap')], 'laravel-shop-wap-member');
        }
    }

    //載入資料庫遷移檔案
    public function loadMigration()
    {
        if($this->app->runningInConsole()){
            $this->loadMigrationsFrom(__DIR__.'/../Database/migrations');
        }
    }

    //載入member配置檔案到框架的配置檔案(合併)
    public function LoadMemberConfig()
    {
        config(Arr::dot(config('wap.member.auth',[]),'auth.'));
        config(Arr::dot(config('wap.member.wechat',[]),'wechat.'));
    }

    protected function registerRouteMiddleware()
    {
        foreach ($this->middlewareGroups as $key => $middleware) {
            $this->app['router']->middlewareGroup($key, $middleware);
        }

        foreach ($this->routeMiddleware as $key => $middleware) {
            $this->app['router']->aliasMiddleware($key, $middleware);
        }
    }

    //註冊路由
    private function registerRoutes()
    {
        Route::group($this->routeConfiguration(), function () {
            $this->loadRoutesFrom(__DIR__.'/../Http/routes.php');
        });
    }

    //定義路由配置
    private function routeConfiguration()
    {
        return [
            // 定義訪問路由的域名
            // 'domain' => config('telescope.domain', null),
            // 是定義路由的名稱空間
            'namespace' => 'Impecty\LaravelShop\Wap\Member\Http\Controllers',
            // 這是字首
            'prefix' => 'wap/member',
            //中介軟體
            'middleware' => 'web',
        ];
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章