From 9ed2b7cb6f1d53ebd5458f7e1dd04f709716ab77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ayax=20C=C3=B3rdova?= Date: Sat, 23 Sep 2023 18:33:00 -0600 Subject: [PATCH] feat: exclude config file from being register/publish MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ayax Córdova --- src/Package/Concerns/HasConfig.php | 27 +++++++++++-- src/Package/Contracts/WithConfig.php | 10 +++++ .../Feature/Package/UnregisterConfigTest.php | 40 +++++++++++++++++++ workbench/tests/ServiceProviderTest.php | 7 +++- 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 tests/Feature/Package/UnregisterConfigTest.php diff --git a/src/Package/Concerns/HasConfig.php b/src/Package/Concerns/HasConfig.php index 5f744e6..edd789f 100644 --- a/src/Package/Concerns/HasConfig.php +++ b/src/Package/Concerns/HasConfig.php @@ -14,6 +14,10 @@ trait HasConfig protected bool $shouldIncludeConfigFromFolder = false; + protected array $excludeConfig = []; + + protected array $unpublishConfig = []; + public function hasConfig(): bool { return $this->shouldLoadDefaultConfigFolder() || filled($this->configFiles); @@ -48,9 +52,12 @@ public function getRegisteredConfig(): Collection $config = $this->loadConfigFilesFromFolder(); } - return collect($this->configFiles) + $register = collect($this->configFiles) ->merge($config) + ->filter(fn (bool $_, string $config) => ! in_array($config, $this->excludeConfig)) ->keys(); + + return $register; } public function getPublishableConfig(): Collection @@ -62,8 +69,8 @@ public function getPublishableConfig(): Collection } return collect($this->configFiles) - ->merge($files) - ->filter() + ->merge([...$files, ...$this->unpublishConfig]) + ->filter(fn (bool $publish, string $config) => ! in_array($config, $this->excludeConfig)) ->keys(); } @@ -79,6 +86,20 @@ public function getConfigPath(string $path = ''): string return join_paths($this->configPath, $path); } + public function unregisterConfig(string $path): static + { + $this->excludeConfig[] = $path; + + return $this; + } + + public function unpublishConfig(string $path): static + { + $this->unpublishConfig[$path] = false; + + return $this; + } + private function shouldLoadDefaultConfigFolder(): bool { return ! $this->preventLoadDefault && $this->shouldIncludeConfigFromFolder; diff --git a/src/Package/Contracts/WithConfig.php b/src/Package/Contracts/WithConfig.php index a4a98b4..c190a75 100644 --- a/src/Package/Contracts/WithConfig.php +++ b/src/Package/Contracts/WithConfig.php @@ -57,4 +57,14 @@ public function setConfigPath(string $path): static; * Get the path to the configuration folder */ public function getConfigPath(string $path = ''): string; + + /** + * Un-register a previously registered config + */ + public function unregisterConfig(string $path): static; + + /** + * Un-publish a previously published config + */ + public function unpublishConfig(string $path): static; } diff --git a/tests/Feature/Package/UnregisterConfigTest.php b/tests/Feature/Package/UnregisterConfigTest.php new file mode 100644 index 0000000..8e5da5b --- /dev/null +++ b/tests/Feature/Package/UnregisterConfigTest.php @@ -0,0 +1,40 @@ +setName('unregister') + ->withConfig($package->getConfigPath('extra/three.php')) + ->unregisterConfig($package->getConfigPath('one.php')) + ->unpublishConfig($package->getConfigPath('extra/three.php')); + } +} + +uses(UnregisterConfigTest::class); + +test('un-register config', function () { + artisan('vendor:publish', ['--tag' => 'unregister-config'])->run(); + + expect(config('one.key')) + ->toBeNull(); + + assertFileDoesNotExist(config_path('one.php')); +}); + +test('un-publish config', function () { + artisan('vendor:publish', ['--tag' => 'unregister-config'])->run(); + + expect(config('three.key')) + ->toBe('three'); + + assertFileDoesNotExist(config_path('three.key')); +}); diff --git a/workbench/tests/ServiceProviderTest.php b/workbench/tests/ServiceProviderTest.php index adaf48f..dd8da60 100644 --- a/workbench/tests/ServiceProviderTest.php +++ b/workbench/tests/ServiceProviderTest.php @@ -5,6 +5,7 @@ use Asciito\LaravelPackage\Package\Package; use Asciito\LaravelPackage\Tests\TestCase; use Illuminate\Support\Facades\File; +use Symfony\Component\Finder\SplFileInfo; use Workbench\App\Nested\NestedServiceProvider; use Workbench\App\ServiceProvider; @@ -16,6 +17,8 @@ abstract class ServiceProviderTest extends TestCase 'one.php', 'two.php', 'three.php', + 'nested-one.php', + 'nested-two.php', ]; protected function setUp(): void @@ -64,6 +67,8 @@ protected function deletePublishable(): void ->all() ); - File::cleanDirectory(database_path('migrations')); + collect(File::files(database_path('migrations'))) + ->filter(fn (SplFileInfo $file) => $file->getExtension() === '.php') + ->each(fn (SplFileInfo $file) => File::delete($file)); } }