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 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。