Laravel 模块开发包 (nWidart/Laravel-modules) 源码解读

laravel模块开发包(nWidart/laravel-modules)

服务注册

composer 自动加载

先看 composer.php 文件中自动加载服务提供者

其中注册了 laravel 模块开发的服务提供者和门面别名

"extra": {
  "laravel": {
    "providers": [
      "LittleSuperman\\Modules\\LaravelModulesServiceProvider"
    ],
    "aliases": {
      "Module": "LittleSuperman\\Modules\\Facades\\Module"
    }
  }
}

服务提供者注册

tip: 由于服务提供者是先执行 register 方法, 然后在执行 boot 方法

<?php

namespace LittleSuperman\Modules;

use LittleSuperman\Modules\Contracts\RepositoryInterface;
use LittleSuperman\Modules\Support\Stub;

class LaravelModulesServiceProvider extends ModulesServiceProvider
{
    /**
     * Booting the package.
     */
    public function boot()
    {
        $this->registerNamespaces();
        $this->registerModules();
    }

    /**
     * Register the service provider.
     */
    public function register()
    {
        $this->registerServices();
        $this->setupStubPath();
        $this->registerProviders();
    }

    /**
     * Setup stub path.
     */
    public function setupStubPath()
    {
        Stub::setBasePath(__DIR__ . '/Commands/stubs');

        $this->app->booted(function ($app) {
            /** @var RepositoryInterface $moduleRepository */
            $moduleRepository = $app[RepositoryInterface::class];
            if ($moduleRepository->config('stubs.enabled') === true) {
                Stub::setBasePath($moduleRepository->config('stubs.path'));
            }
        });
    }

    /**
     * {@inheritdoc}
     */
    protected function registerServices()
    {
        $this->app->singleton(Contracts\RepositoryInterface::class, function ($app) {
            $path = $app['config']->get('modules.paths.modules');

            return new Laravel\LaravelFileRepository($app, $path);
        });
        $this->app->alias(Contracts\RepositoryInterface::class, 'modules');
    }
}

register 方法中注册了

  1. 单例 LittleSuperman\Modules\Laravel\LaravelFileRepository,
  2. 用于创建模板路径
  3. 注册其他服务提供者

说明

由于本人也是一边看一写文档可能会出现错误, 更新可能持续更新

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!