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
方法中注册了
- 单例
LittleSuperman\Modules\Laravel\LaravelFileRepository
, - 用于创建模板路径
- 注册其他服务提供者
说明
由于本人也是一边看一写文档可能会出现错误, 更新可能持续更新
本作品采用《CC 协议》,转载必须注明作者和本文链接