filefabrik / bootraiser
Laravel 自定义包的启动助手
Requires (Dev)
- filefabrik/bootraiser-testing: ~2.0
- friendsofphp/php-cs-fixer: ^3.14
- livewire/livewire: ^v3.4
- mockery/mockery: ^1.5
- orchestra/testbench: ^9.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-drift: ^2.6
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/phpstan: @stable
- phpunit/phpunit: ^10.5
README
Laravel 自定义包的启动工具
特性
- Bootraiser 可以帮你节省 Laravel 包所需的所有样板代码。
- Bootraiser 可以轻松集成到现有包中。
- Bootraiser 完全基于 Laravel
- 无需“额外魔法”包
如果你编写自己的 Laravel 包,通常需要在 Laravel 中启动包的一部分。
这有时可能会浪费很多时间。
Filefabrik-Bootraiser 可以立即为你提供所有重要的 Laravel 启动方法,无需太多配置。如果 Laravel 的“发布”方法得到支持,那么稍后可以发布视图|翻译|包配置覆盖,这也是相当酷的。
- github-project: https://github.com/Filefabrik/bootraiser
- packagist.org: https://packagist.org.cn/packages/filefabrik/bootraiser
当前状态为 "dev"。没有测试。但它们正在建设中
安装
composer require filefabrik/bootraiser
如果您只想在开发期间使用 Bootraiser,请将 bootraiser 包与以下内容集成
composer require filefabrik/bootraiser --dev
然后请记住实现您自己的启动机制或从 Bootraiser-Trait 中复制您需要的启动机制到您的 YourPackageServiceProvider
修改您的 YourPackageServiceProvider.php
然后转到您的提供者文件,通常位于
~/packages/your-package/src/Providers/YourPackageServiceProvider.php
YourPackageServiceProvider.php 文件看起来像这样
<?php namespace YourCompanyVendor\YourPackage\Providers; use Filefabrik\Bootraiser\WithBootraiser; use Illuminate\Support\ServiceProvider; class YourPackageServiceProvider extends ServiceProvider { // insert this Magic Trait :) use WithBootraiser; public function register() { $this->bootraiserRegister('Config'); } public function boot(): void { // parts to boot if they are already exists in your code $bootParts = [ 'Seeders', 'Routes', 'Migrations', 'Translations', 'Views', 'Commands', // 3rd Party package 'Livewire', ]; /* Easy boot utility. You can replace all the booted service/parts with your own*/ $this->bootraiserBoot($bootParts); } }
-
要使用 Bootraiser,必须包含
use WithBootraiser;
。 -
然后指定您想使用 Bootraiser 启动的组件,以数组形式。
注意:您可以输入所有部分作为启动部分。Bootraiser 只会启动您包中实际存在的部分。
分割 Bootraiser 启动过程
如有需要,使用 boot raiser 细分启动过程。
<?php ... public function boot(): void { // parts to boot if they are already exists in your code $bootParts = [ 'Seeders', 'Routes', 'Migrations', 'Translations', ]; /* Easy boot utility. You can replace all the booted service/parts with your own*/ $this->bootraiserBoot($bootParts); /** * your custom boot stuff */ // boot the rest if need $bootParts2 = [ 'Views', 'Commands', // 3rd Party package 'Livewire', ]; $this->bootraiserBoot($bootParts2); } ... ?>
以下启动机制可供您使用
启动 Routes
packages/your-package/routes/web.php
@see https://laravel.net.cn/docs/11.x/packages#routes
启动 Migrations
(将迁移发布到 Laravel 迁移目录)
提供用于发布的迁移文件 packages/your-package/database/migrations/*
php artisan vendor:publish --tag=your-package-migrations
@see https://laravel.net.cn/docs/11.x/packages#migrations
不发布集成迁移
无需发布包的迁移文件。Bootraiser 可以在命令 php artisan migrate:status
中为每个包单独提供迁移。
为此,在您的 register 或 boot 方法中放置以下代码片段
public function register(){ $this->bootraiserIntegrate('Migrations') }
不集成迁移
集成迁移
启动 Translations
(语言文件)
packages/your-package/lang/*
php artisan vendor:publish --tag=your-package-translations
https://laravel.net.cn/docs/11.x/packages#language-files
启动 Views
(blade)和视图组件
- loadViews
- 如有需要,发布您的 blade 文件以进行覆盖
php artisan vendor:publish --tag=your-package-views
- 将您的包注册到视图组件
@see https://laravel.net.cn/docs/11.x/packages#views
启动 Commands
如果存在,启动您的命令,并且如果您目前使用 cli 处理 Laravel
packages/your-package/src/Console/Commands
@see https://laravel.net.cn/docs/11.x/packages#commands
启动 Config
packages/your-package/packageConfig/packageConfig.php
php artisan vendor:publish --tag=your-package-packageConfig
packageConfig
是单数!
将输出到 packageConfig/your-package.php
或使用自定义 $bootraiserPackage->setGroupName('cooler')
到 packageConfig/cooler.php
注册 Config
有关更多功能,请参阅下面的 高级用法
启动 Livewire
如果您创建了自己的 Livewire 视图,Livewire 也受支持并已启动。
- blade 目录:
packages/your-package/resource/views/livewire/*
- Livewire 组件目录
packages/your-package/src/Livewire/
启动 livewire 将支持点命名空间(即使没有 paxsy 生成器也可以工作)
启动 Livewire 也会显示在 Laravel 的调试栏中
注意:Laravel Debug-Bar 可以通过以下方式安装
composer install barryvdh/laravel-debugbar --dev
关于 vendor:publish --tag=“your-package”-views|translations|migrations 的说明
如果您的包名太长或难以创建一个易于记忆的组名,只需为组名设置不同的标识符
<?php ... $this->bootraiserPackage()->setGroupName('cooler'); ?>
现在所有您的发布标签选项将看起来像 --tag=cooler-views
php artisan vendor:publish --tag=cooler-views
命令 bootraiser:seed
bootraiser:seed 命令获取一个与 Bootraiser 的菜单
php artisan bootraiser:seed
选择您要执行的分发器
为了能够为包执行数据库分发器,db:seed 命令已通过以下选项扩展。
--main
数据库分发器
Bootraiser 跟踪的所有 DatabaseSeeder.php 都将执行 --main
php artisan bootraiser:seed --main
使用 --main
标志,所有 DatabaseSeeder 都将在 DatabaseSeeder.php 中执行。
例如,所有执行的分发器
Laravel 应用分发器
- ~./database/seeders/DatabaseSeeder.php
对于使用 bootraiser 的包
- ~./packages/my-package/database/seeders/DatabaseSeeder.php
- ~./app-paxsy/another-package/database/seeders/DatabaseSeeder.php
@see https://laravel.net.cn/docs/11.x/seeding#running-seeders
高级用法
如果您想使 packageConfig 可发布,您还必须根据以下方式调整 YourServiceProvider::register()
当在 YourServiceProvider 中使用 register() 和 boot() 方法时,请使用以下更好的 Schema
<?php namespace YourCompanyVendor\YourPackage\Providers; use Filefabrik\Bootraiser\WithBootraiser; use Illuminate\Support\ServiceProvider; class YourPackageServiceProvider extends ServiceProvider { // insert this Magic Trait :) use WithBootraiser; // public function register() { $this->bootraiserRegister(['Config']); } public function boot(): void { // parts to boot if they are already exists in your code // parts to boot if they are already exists in your code $bootParts = [ 'Seeders', 'Routes', 'Migrations', 'Translations', ]; /* Easy boot utility. You can replace all the booted service/parts with your own*/ $this->bootraiserBoot($bootParts); /** * your custom boot stuff */ // boot the rest if need $bootParts2 = [ 'Views', 'Commands', // 3rd Party package 'Livewire', ]; $this->bootraiserBoot($bootParts2); } }
https://laravel.net.cn/docs/11.x/packages#default-package-configuration
事件
Bootraiser 也通过以下约定处理包事件。
- 包必须在
/src/Providers/
下包含一个扩展 LaravelEventServiceProvider
类的服务提供器 - 将
"Filefabrik\\BootraiserDemo\\Providers\\BootraiserDemoEventServiceProvider"
插入到包的 composer.json 中
BootraiserDemoEventServiceProvider 事件服务提供器
<?php namespace Filefabrik\BootraiserDemo\Providers; use Filefabrik\Bootraiser\WithBootraiserEvent; use Illuminate\Foundation\Support\Providers\EventServiceProvider; class BootraiserDemoEventServiceProvider extends EventServiceProvider { // Magic for Events use WithBootraiserEvent; public function register() { // call parent::register() is mandatory! parent::register(); $this->bootraiserRegister('Config','Events'); } }
包 composer.json
... "extra": { "laravel": { "providers": [ "Filefabrik\\BootraiserDemo\\Providers\\BootraiserDemoServiceProvider", "Filefabrik\\BootraiserDemo\\Providers\\BootraiserDemoEventServiceProvider" ] } } ...
请记住运行 Laravel 主机的 composer.json
composer update
待办事项
- 对于每个使用 bootraiser 的供应商/包,都提供了显示在包中使用的/启用的所有发布标签的能力。因此,包的开发者文档可以更容易地编写。例如:
php artisan bootraiser:show {packagename}
- 如果有许多自己的自定义包,可能允许缓存 bootraiser 启动部分会更好。这样,在实时环境中,自己包的发现/启动/注册将更快。
- 结合缓存,启用或禁用缓存中的包。原因是,您正在开发特定的包,其他包不在开发中。
- 对于 env.testing,迁移应该可用于测试数据库,但不应该发布。
待办事项:反射命令以显示其中包含的 laravel 组件,例如 seeder 或 livewire 或或或。待办事项:包中的默认数据库分发器是类 DatabaseSeeder
,其中 db:seed 方法必须运行。待办事项:使用显式类名运行,显式类将运行。使用标志 --package 或与包目录中的类一起使用。待办事项:描述 db 分发器(带有空格)php artisan db:seed "\DemoPackage\TryCommandOptions\Database\Seeders\MySeeder"
php artisan db:seed --class="\DemoPackage\TryCommandOptions\Database\Seeders\MySeeder"
- 待办事项:迁移
- 自己的迁移,如 bootraiser:migrate
- 显示包迁移,允许通过选择路径进行刷新。在控制台命令期间显示。在包开发期间,表迁移通常会更改
- bootraiser 应该成为一次性的方法,因为 EventServiceProvider 或 FancyServiceProvider 不能再次调用相同的 "booting" 命令
- 在包外部,有一个全局配置,通过这个配置,开发者能够禁用或启用在包中硬编码的“启动”方法。
- 将发布迁移的迁移与加载迁移分开