diff --git a/README.md b/README.md index 165658c..285385c 100644 --- a/README.md +++ b/README.md @@ -390,6 +390,17 @@ php artisan vendor:publish --tag=your-package-name-provider ... will copy `/resources/stubs/{$nameOfYourServiceProvider}.php.stub` in your package to `app/Providers/{$nameOfYourServiceProvider}.php` in the app of the user. +You can publish multiple providers at cone by declaring an array of providers name +```php +$package + ->name('your-package-name') + ->publishesServiceProvider([ + $nameOfYourServiceProvider1, + $nameOfYourServiceProvider2, + ... + ]); +``` + ### Registering commands You can register any command you package provides with the `hasCommand` function. diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index 196ad38..50fdd3f 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -62,7 +62,7 @@ public function handle() if ($this->copyServiceProviderInApp) { $this->comment('Publishing service provider...'); - $this->copyServiceProviderInApp(); + $this->copyServiceProvidersInApp(); } if ($this->starRepo) { @@ -150,9 +150,28 @@ public function endWith($callable): self return $this; } - protected function copyServiceProviderInApp(): self + protected function copyServiceProvidersInApp(): self { - $providerName = $this->package->publishableProviderName; + $providersName = $this->package->publishableProviderName; + + if (! $providersName) { + return $this; + } + + if (is_array($providersName)) { + foreach($providersName as $providerName) { + $this->copyServiceProviderInApp($providerName); + } + } else { + $this->copyServiceProviderInApp($providersName); + } + + return $this; + } + + protected function copyServiceProviderInApp(?string $providerName = null): self + { + $providerName = $providerName ?? $this->package->publishableProviderName; if (! $providerName) { return $this; diff --git a/src/Package.php b/src/Package.php index 940c144..7ffc5c3 100644 --- a/src/Package.php +++ b/src/Package.php @@ -39,7 +39,7 @@ class Package public string $basePath; - public ?string $publishableProviderName = null; + public mixed $publishableProviderName = null; public function name(string $name): static { @@ -61,7 +61,7 @@ public function hasConfigFile($configFileName = null): static return $this; } - public function publishesServiceProvider(string $providerName): static + public function publishesServiceProvider(mixed $providerName): static { $this->publishableProviderName = $providerName; diff --git a/src/PackageServiceProvider.php b/src/PackageServiceProvider.php index 4a737c0..3a71417 100644 --- a/src/PackageServiceProvider.php +++ b/src/PackageServiceProvider.php @@ -3,6 +3,7 @@ namespace Spatie\LaravelPackageTools; use Carbon\Carbon; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; @@ -141,10 +142,18 @@ public function boot() ], "{$this->package->name}-components"); } - if ($this->package->publishableProviderName) { - $this->publishes([ - $this->package->basePath("/../resources/stubs/{$this->package->publishableProviderName}.php.stub") => base_path("app/Providers/{$this->package->publishableProviderName}.php"), - ], "{$this->package->shortName()}-provider"); + if ($providersName = $this->package->publishableProviderName) { + if (!is_array($providersName)) { + $providersName = [$providersName]; + } + + $publishableProviders = []; + + foreach ($providersName as $publishableProviderName) { + $publishableProviders[$this->package->basePath("/../resources/stubs/{$publishableProviderName}.php.stub")] = base_path("app/Providers/{$publishableProviderName}.php"); + } + + $this->publishes($publishableProviders, "{$this->package->shortName()}-provider"); }