Skip to content

Commit

Permalink
feat: exclude config file from being register/publish
Browse files Browse the repository at this point in the history
Signed-off-by: Ayax Córdova <ayax.cordova@aydev.mx>
  • Loading branch information
asciito committed Sep 24, 2023
1 parent b911f5c commit 9ed2b7c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/Package/Concerns/HasConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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();
}

Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/Package/Contracts/WithConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
40 changes: 40 additions & 0 deletions tests/Feature/Package/UnregisterConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Asciito\LaravelPackage\Tests\Feature\Package;

use Asciito\LaravelPackage\Package\Package;
use function Pest\Laravel\artisan;
use function PHPUnit\Framework\assertFileDoesNotExist;

trait
UnregisterConfigTest
{
protected function configurePackage(Package $package): void
{
$package
->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'));
});
7 changes: 6 additions & 1 deletion workbench/tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 9ed2b7c

Please sign in to comment.