Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed May 23, 2021
1 parent faabcdf commit 9a236a8
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-package-tools` will be documented in this file.

## 1.6.0 - 2021-05-23

- add support for multiple config files

## 1.5.0 - 2021-03-10

- add support for Blade view components
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ The `hasConfigFile` method will also make the config file publishable. Users of
php artisan vendor:publish --tag=your-package-name-config
```

Should your package have multiple config files, you can pass their names as an array to `hasConfigFile`

```php
$package
->name('your-package-name')
->hasConfigFile(['my-config-file', 'another-config-file']);
```


### Working with views

Any views your package provides, should be placed in the `<package root>/resources/views` directory.
Expand Down
12 changes: 9 additions & 3 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Package
{
public string $name;

public ?string $configFileName = null;
public array $configFileNames = [];

public bool $hasViews = false;

Expand All @@ -33,9 +33,15 @@ public function name(string $name): self
return $this;
}

public function hasConfigFile(string $configFileName = null): self
public function hasConfigFile($configFileName = null): self
{
$this->configFileName = $configFileName ?? $this->shortName();
$configFileName = $configFileName ?? $this->shortName();

if (! is_array($configFileName)) {
$configFileName = [$configFileName];
}

$this->configFileNames = $configFileName;

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function register()
throw InvalidPackage::nameIsRequired();
}

if ($configFileName = $this->package->configFileName) {
foreach($this->package->configFileNames as $configFileName) {
$this->mergeConfigFrom($this->package->basePath("/../config/{$configFileName}.php"), $configFileName);
}

Expand All @@ -41,7 +41,7 @@ public function boot()
$this->bootingPackage();

if ($this->app->runningInConsole()) {
if ($configFileName = $this->package->configFileName) {
foreach($this->package->configFileNames as $configFileName) {
$this->publishes([
$this->package->basePath("/../config/{$configFileName}.php") => config_path("{$configFileName}.php"),
], "{$this->package->shortName()}-config");
Expand Down
24 changes: 24 additions & 0 deletions tests/PackageServiceProviderTests/PackageMultipleConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Spatie\LaravelPackageTools\Tests\PackageServiceProviderTests;

use Spatie\LaravelPackageTools\Package;

class PackageMultipleConfigTest extends PackageServiceProviderTestCase
{
public function configurePackage(Package $package)
{
$package
->name('laravel-package-tools')
->hasConfigFile(['package-tools', 'alternative-config']);
}

/** @test */
public function it_can_register_multiple_config_files()
{
$this->assertEquals('value', config('package-tools.key'));

$this->assertEquals('alternative_value', config('alternative-config.alternative_key'));

}
}
7 changes: 7 additions & 0 deletions tests/TestPackage/config/alternative-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
// this is a dummy config file

'alternative_key' => 'alternative_value',
];

0 comments on commit 9a236a8

Please sign in to comment.