diff --git a/src/Package/Concerns/HasConfig.php b/src/Package/Concerns/HasConfig.php index edd789f..9485e4e 100644 --- a/src/Package/Concerns/HasConfig.php +++ b/src/Package/Concerns/HasConfig.php @@ -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 { @@ -54,7 +54,9 @@ 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; @@ -62,16 +64,20 @@ public function getRegisteredConfig(): Collection 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 @@ -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; } @@ -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)); + } } diff --git a/tests/Feature/NestedServiceProvider/RegisterConfigTest.php b/tests/Feature/NestedServiceProvider/RegisterConfigTest.php index 2880eeb..81e5285 100644 --- a/tests/Feature/NestedServiceProvider/RegisterConfigTest.php +++ b/tests/Feature/NestedServiceProvider/RegisterConfigTest.php @@ -5,7 +5,7 @@ use function Pest\Laravel\{ artisan }; use function PHPUnit\Framework\assertFileExists; -trait NestedServiceProviderWithConfig +trait NestedServiceProviderConfig { protected function configureNestedService(Package $package): void { @@ -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')) diff --git a/tests/Feature/Package/RegisterConfigTest.php b/tests/Feature/Package/RegisterConfigTest.php index 46c72d4..af12be2 100644 --- a/tests/Feature/Package/RegisterConfigTest.php +++ b/tests/Feature/Package/RegisterConfigTest.php @@ -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')) @@ -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')); }); diff --git a/tests/Feature/Package/UnregisterConfigTest.php b/tests/Feature/Package/UnregisterConfigTest.php index 701e696..b7b4df2 100644 --- a/tests/Feature/Package/UnregisterConfigTest.php +++ b/tests/Feature/Package/UnregisterConfigTest.php @@ -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(); @@ -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')); });