Skip to content

Commit

Permalink
fix: filter register/publish config files
Browse files Browse the repository at this point in the history
This commit includes:

Properties renamed to express "intent"
Code refactor
Unregister/Unpublish config files only by its name (file basename).
Don't publish files when param $publish is False

Signed-off-by: Ayax Córdova <ayax.cordova@aydev.mx>
  • Loading branch information
asciito committed Sep 24, 2023
1 parent 347db3b commit e544739
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
31 changes: 21 additions & 10 deletions src/Package/Concerns/HasConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ trait HasConfig

protected bool $shouldIncludeConfigFromFolder = false;

protected array $excludeConfig = [];
protected array $excludedConfig = [];

protected array $unpublishConfig = [];
protected array $unpublishedConfig = [];

public function hasConfig(): bool
{
Expand Down Expand Up @@ -54,24 +54,30 @@ public function getRegisteredConfig(): Collection

$register = collect($this->configFiles)
->merge($config)
->filter(fn (bool $_, string $config) => ! in_array($config, $this->excludeConfig))
->filter(function (bool $_, string $config) {
return ! in_array($this->getConfigName($config), $this->excludedConfig);
})
->keys();

return $register;
}

public function getPublishableConfig(): Collection
{
$files = [];
$config = [];

if ($this->shouldLoadDefaultConfigFolder()) {
$files = $this->loadConfigFilesFromFolder();
$config = $this->loadConfigFilesFromFolder();
}

return collect($this->configFiles)
->merge([...$files, ...$this->unpublishConfig])
->filter(fn (bool $publish, string $config) => ! in_array($config, $this->excludeConfig))
$publish = collect($this->configFiles)
->merge($config)
->filter(function (bool $publish, string $config) {
return $publish && ! in_array($this->getConfigName($config), [...$this->excludedConfig, ...$this->unpublishedConfig]);
})
->keys();

return $publish;
}

public function setConfigPath(string $path): static
Expand All @@ -88,14 +94,14 @@ public function getConfigPath(string $path = ''): string

public function unregisterConfig(string $path): static
{
$this->excludeConfig[] = $path;
$this->excludedConfig[] = $this->getConfigName($path);

return $this;
}

public function unpublishConfig(string $path): static
{
$this->unpublishConfig[$path] = false;
$this->unpublishedConfig[] = $this->getConfigName($path);

return $this;
}
Expand All @@ -114,4 +120,9 @@ private function loadConfigFilesFromFolder(): array
->mapWithKeys(fn (SplFileInfo $file) => [(string) $file => true])
->all();
}

private function getConfigName(string $file): string
{
return str_replace('.php', '', basename($file));
}
}
4 changes: 2 additions & 2 deletions tests/Feature/NestedServiceProvider/RegisterConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use function Pest\Laravel\{ artisan };
use function PHPUnit\Framework\assertFileExists;

trait NestedServiceProviderWithConfig
trait NestedServiceProviderConfig
{
protected function configureNestedService(Package $package): void
{
Expand All @@ -24,7 +24,7 @@ protected function configureNestedService(Package $package): void
}
}

uses(NestedServiceProviderWithConfig::class);
uses(NestedServiceProviderConfig::class);

it('register config', function () {
expect(config('nested-one.key'))
Expand Down
18 changes: 9 additions & 9 deletions tests/Feature/Package/RegisterConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
use Asciito\LaravelPackage\Package\Package;

use function Pest\Laravel\{ artisan };
use function PHPUnit\Framework\assertFileDoesNotExist;
use function PHPUnit\Framework\assertFileExists;

trait PackageWithConfig
trait RegisterConfig
{
protected function configurePackage(Package $package): void
{
$package
->setName('laravel-package')
->preventDefaultConfig()
->setName('register-configuration')
->withConfig([
$package->getConfigPath('one.php'),
$package->getConfigPath('two.php'),
$package->getConfigPath('extra/three.php'),
]);
])
->withConfig($package->getConfigPath('extra/three.php'), false)
->preventDefaultConfig();
}
}

uses(PackageWithConfig::class);
uses(RegisterConfig::class);

it('register config', function () {
expect(config('one.key'))
Expand All @@ -32,10 +33,9 @@ protected function configurePackage(Package $package): void
});

it('publish config files', function () {
artisan('vendor:publish', ['--tag' => 'laravel-package-config'])
->doesntExpectOutputToContain('No publishable resources for tag [laravel-package-config]')
->assertSuccessful();
artisan('vendor:publish', ['--tag' => 'register-configuration-config'])->run();

assertFileExists(config_path('one.php'));
assertFileExists(config_path('two.php'));
assertFileDoesNotExist(config_path('three.php'));
});
18 changes: 9 additions & 9 deletions tests/Feature/Package/UnregisterConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
use function Pest\Laravel\artisan;
use function PHPUnit\Framework\assertFileDoesNotExist;

trait UnregisterConfigTest
trait UnregisterConfig
{
protected function configurePackage(Package $package): void
{
$package
->setName('unregister')
->setName('unregistered-unpublished-configuration')
->withConfig($package->getConfigPath('extra/three.php'))
->unregisterConfig($package->getConfigPath('one.php'))
->unpublishConfig($package->getConfigPath('extra/three.php'));
->unregisterConfig('one.php')
->unpublishConfig('three.php');
}
}

uses(UnregisterConfigTest::class);
uses(UnregisterConfig::class);

test('un-register config', function () {
artisan('vendor:publish', ['--tag' => 'unregister-config'])->run();
artisan('vendor:publish', ['--tag' => 'unregistered-unpublished-configuration-config'])->run();

expect(config('one.key'))
->toBeNull();
Expand All @@ -31,10 +31,10 @@ protected function configurePackage(Package $package): void
});

test('un-publish config', function () {
artisan('vendor:publish', ['--tag' => 'unregister-config'])->run();
artisan('vendor:publish', ['--tag' => 'unregistered-unpublished-configuration-config'])->run();

assertFileDoesNotExist(config_path('three.key'));

expect(config('three.key'))
->toBe('three');

assertFileDoesNotExist(config_path('three.key'));
});

0 comments on commit e544739

Please sign in to comment.