One of many joys of utilizing packages within the Laravel ecosystem is how straightforward they’re to put in. Packages will be pulled in utilizing Composer, and Laravel will mechanically uncover them.
On this put up, you may discover ways to simply add an set up
command, making it even simpler for bundle customers to start out utilizing a bundle.
Package deal set up instructions are nice for the bundle consumer
Most packages will ask you to publish the config file, migrations, or some property of their set up directions. You most likely have already executed instructions reminiscent of:
php artisan vendor:publish --tag=awesome-package-config
php artisan vendor:publish --tag=awesome-package-migrations
php artisan vendor:publish --tag=awesome-package-assets
...
Some packages go about it a little bit bit higher. Laravel Horizon, as an illustration, has a nifty set up command that can:
- publish the config file
- create a Horizon service supplier inside your app
- register that service supplier in your config/app.php
That is very handy for the consumer to rise up and working shortly.
In Horizon’s supply code, you’ll be able to learn the code that powers that set up command. As a bundle developer, it is not an excessive amount of hassle creating such a command. Nonetheless, particular duties – reminiscent of registering a service supplier within the config/app.php within the software – can take a while to get proper.
Most bundle builders do not create set up instructions as a result of it takes too lengthy to get them proper. That is why I spent a while making creating such set up instructions straightforward.
Simply create bundle set up instructions
Laravel Package deal Instruments bundle is a bundle we made to assist bundle creators. Here is what a typical service supplier of a bundle may appear like.
class BackupServiceProvider extends ServiceProvider
{
public perform boot()
{
$this->publishes([
__DIR__.'/../config/backup.php' => config_path('backup.php'),
], 'config');
$this->publishes([
__DIR__.'/../resources/lang' => "{$this->app['path.lang']}/vendor/backup",
]);
$this->loadTranslationsFrom(__DIR__.'/../assets/lang/', 'backup');
}
public perform register()
{
$this->mergeConfigFrom(__DIR__.'/../config/backup.php', 'backup');
$this->app->bind('command.backup:run', BackupCommand::class);
$this->app->bind('command.backup:clear', CleanupCommand::class);
$this->app->bind('command.backup:listing', ListCommand::class);
$this->app->bind('command.backup:monitor', MonitorCommand::class);
$this->instructions([
'command.backup:run',
'command.backup:clean',
'command.backup:list',
'command.backup:monitor',
]);
}
Utilizing the PackageServiceProvider
supplied by spatie/laravel-package-tools, the code above will be rewritten like this:
class BackupServiceProvider extends PackageServiceProvider
{
public perform configurePackage(Package deal $bundle): void
{
$bundle
->identify('laravel-backup')
->hasConfigFile()
->hasTranslations()
->hasCommands([
BackupCommand::class,
CleanupCommand::class,
ListCommand::class,
MonitorCommand::class,
]);
}
}
Within the newest model of laravel-package-tools, we have added new performance so as to add an installer on your bundle. That is how you are able to do that.
class YourServiceProvider extends PackageServiceProvider
{
public perform configurePackage(Package deal $bundle): void
{
$bundle
->identify('your-package')
->hasConfigFile()
->hasTranslations()
->hasCommands([
BackupCommand::class,
CleanupCommand::class,
ListCommand::class,
MonitorCommand::class,
])
->hasInstallCommand(perform(InstallCommand $command) {
$command
->publishConfigFile()
->publishMigrations()
->askToRunMigrations()
->copyAndRegisterServiceProviderInApp()
->askToStarRepoOnGitHub();
});
}
}
Utilizing this code, your bundle will acquire an set up command that can:
- copy the config file
- publish the migration of your bundle
- ask to run the migrations
- copy a service supplier stub to the app the place the bundle is put in in. That service supplier can even be registered in
config/app.php
of the app - ask if the repo must be starred on GitHub
Utilizing hasInstallCommand
is way faster than writing an set up command by hand.
We’ll be utilizing hasInstallCommand
ourselves in our upcoming Dynamic Servers bundle. Here is what the code within the service supplier of that bundle appears like.
// ...
->hasInstallCommand(perform (InstallCommand $command) {
$command
->publishConfigFile()
->copyAndRegisterServiceProviderInApp()
->publishMigrations()
->askToRunMigrations()
->askToStarRepoOnGitHub('spatie/laravel-dynamic-servers')
->endWith(perform (InstallCommand $installCommand) {
$installCommand->line('');
$installCommand->data("We have added appProvidersDynamicServersProvider to your venture.");
$installCommand->data("Be at liberty to customise it to your wants.");
$installCommand->line('');
$installCommand->data('You possibly can view all docs at https://spatie.be/docs/laravel-dynamic-servers');
$installCommand->line('');
$installCommand->data('Thanks very a lot for putting in this bundle!');
});
Discover there’s additionally an endWith
perform that you need to use to make any customization you may like.
Here is what it appears like after we run dynamic-servers:set up
in a venture the place the bundle is put in.
In closing
We hope this new performance in spatie/laravel-package-tools will assist bundle creators create an set up command. I am positive bundle customers will probably be pleased with reminiscent of command.
This is not the primary bundle that our group has constructed. On our firm web site, try all our open supply packages in this lengthy listing. If you wish to help us, take into account selecting up any of our paid merchandise.