Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat multiple providers #139

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 22 additions & 3 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function handle()
if ($this->copyServiceProviderInApp) {
$this->comment('Publishing service provider...');

$this->copyServiceProviderInApp();
$this->copyServiceProvidersInApp();
}

if ($this->starRepo) {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Package

public string $basePath;

public ?string $publishableProviderName = null;
public mixed $publishableProviderName = null;

public function name(string $name): static
{
Expand All @@ -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;

Expand Down
17 changes: 13 additions & 4 deletions src/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}


Expand Down
Loading