Skip to content

Commit

Permalink
Don't overwrite existing module service providers
Browse files Browse the repository at this point in the history
  • Loading branch information
axyr committed Aug 16, 2024
1 parent f89225f commit e5d1d42
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

abstract class AbstractGenerator
{
public bool $overwriteExistingFile = true;

public function __construct(protected string $name, protected ?string $module = null)
{
$this->name = Str::singular($this->name);
Expand Down Expand Up @@ -116,7 +118,14 @@ public function write(): void
mkdir($dir, 0777, true);
}

file_put_contents($file, $this->content());
if ($this->shouldWriteFile($file)) {
file_put_contents($file, $this->content());
}
}

public function shouldWriteFile(string $file): bool
{
return $this->overwriteExistingFile || ! file_exists($file);
}

public function content(): string
Expand Down
2 changes: 2 additions & 0 deletions src/Generators/ModuleServiceProviderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class ModuleServiceProviderGenerator extends AbstractGenerator
{
public bool $overwriteExistingFile = false;

public function className(): string
{
return sprintf('%sServiceProvider', Str::singular($this->module()));
Expand Down
4 changes: 2 additions & 2 deletions tests/Generators/GeneratorTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testGenerator(string $name, string $module, string $expectedPath
foreach ($config as $key => $value) {
config()->set($key, $value);
}

$generator = $this->generator($name, $module);

$generator->write();
Expand All @@ -28,7 +28,7 @@ public function testGenerator(string $name, string $module, string $expectedPath
}
}

private function generator(string $name, string $module): AbstractGenerator
public function generator(string $name, string $module): AbstractGenerator
{
$generatorClassName = $this->generatorClassName();
return new $generatorClassName($name, $module ?: null);
Expand Down
15 changes: 15 additions & 0 deletions tests/Generators/ModuleServiceProviderGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,19 @@ public static function dataGenerator(): array
],
];
}

public function testItDoesNotOverwriteAnExistingFile(): void
{
$expectedFile = base_path('app-modules/Posts/src/PostServiceProvider.php');

// a Module can contain multiple CRUDs
// we always only want to create a ModuleServiceProvider once
$generator = $this->generator('Post', 'Posts');

$this->assertTrue($generator->shouldWriteFile($expectedFile));

$generator->write();

$this->assertFalse($generator->shouldWriteFile($expectedFile));
}
}

0 comments on commit e5d1d42

Please sign in to comment.