Skip to content

Commit

Permalink
Code style fix
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-cintra committed Nov 22, 2024
1 parent f3e8c61 commit ac3314f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
6 changes: 2 additions & 4 deletions src/Console/MakeModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function createServiceProvider(): void
$path = base_path("modules/{$this->moduleName}/{$this->moduleName}ServiceProvider.php");

file_put_contents($path, $stub);

$this->call('modular:register-provider', ['name' => $this->moduleName]);
}

Expand Down Expand Up @@ -90,6 +90,4 @@ private function createModuleDirectoryStructure(): bool

return true;
}


}
}
20 changes: 11 additions & 9 deletions src/Console/RegisterServiceProviderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,30 @@ public function handle(): int

if ($this->registerServiceProvider($moduleName)) {
$this->info("Service provider for module {$moduleName} registered successfully.");

return self::SUCCESS;
}

$this->error("Service provider for module {$moduleName} is already registered.");

return self::FAILURE;
}

public function registerServiceProvider(string $moduleName): bool
{
$providerPath = base_path('bootstrap/providers.php');
$content = file_get_contents($providerPath);

$providerClass = "Modules\\{$moduleName}\\{$moduleName}ServiceProvider::class";

// Check if provider already exists
if (strpos($content, $providerClass) !== false) {
return false;
}

// Split content into lines
$lines = explode("\n", $content);

// Find the position of the closing bracket
$returnArrayIndex = -1;
foreach ($lines as $index => $line) {
Expand All @@ -46,17 +48,17 @@ public function registerServiceProvider(string $moduleName): bool
break;
}
}

if ($returnArrayIndex === -1) {
return false;
}

// Insert the new provider before the closing bracket
array_splice($lines, $returnArrayIndex, 0, " {$providerClass},");

// Join the lines back together
$updatedContent = implode("\n", $lines);

return (bool) file_put_contents($providerPath, $updatedContent);
}
}
}
2 changes: 1 addition & 1 deletion src/ModularServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public function configurePackage(Package $package): void
->hasCommand(RegisterServiceProviderCommand::class);

}
}
}
28 changes: 14 additions & 14 deletions tests/Console/RegisterServiceProviderCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
// Create a temporary providers.php file for testing
$filesystem = new Filesystem;
$bootstrapPath = $this->app->bootstrapPath();

// Ensure bootstrap directory exists
if (!$filesystem->exists($bootstrapPath)) {
if (! $filesystem->exists($bootstrapPath)) {
$filesystem->makeDirectory($bootstrapPath, 0755, true);
}

// Create providers.php with initial content
$filesystem->put($bootstrapPath . '/providers.php', $initialProvidersContent);
$filesystem->put($bootstrapPath.'/providers.php', $initialProvidersContent);
});

afterEach(function () use ($initialProvidersContent) {
// Restore the providers.php file to its initial state
$filesystem = new Filesystem;
$bootstrapPath = $this->app->bootstrapPath();
$filesystem->put($bootstrapPath . '/providers.php', $initialProvidersContent);
$filesystem->put($bootstrapPath.'/providers.php', $initialProvidersContent);
});

/**
Expand All @@ -50,8 +50,8 @@ function getPackageProviders($app)
->assertSuccessful()
->expectsOutput('Service provider for module Blog registered successfully.');

$content = file_get_contents($this->app->bootstrapPath() . '/providers.php');
$content = file_get_contents($this->app->bootstrapPath().'/providers.php');

// Check that the new provider was added
expect($content)
->toContain('Modules\Blog\BlogServiceProvider::class');
Expand All @@ -68,16 +68,16 @@ function getPackageProviders($app)
->expectsOutput('Service provider for module Blog is already registered.');

// Verify the provider appears only once
$content = file_get_contents($this->app->bootstrapPath() . '/providers.php');
$content = file_get_contents($this->app->bootstrapPath().'/providers.php');
expect(substr_count($content, 'Modules\Blog\BlogServiceProvider::class'))->toBe(1);
});

test('it maintains existing providers and formatting when adding new provider', function () {
$this->artisan('modular:register-provider', ['name' => 'Blog'])
->assertSuccessful();

$content = file_get_contents($this->app->bootstrapPath() . '/providers.php');
$content = file_get_contents($this->app->bootstrapPath().'/providers.php');

// Verify the structure is maintained
expect($content)
->toContain('<?php')
Expand All @@ -95,9 +95,9 @@ function getPackageProviders($app)
->assertSuccessful();
$this->artisan('modular:register-provider', ['name' => 'Shop'])
->assertSuccessful();
$content = file_get_contents($this->app->bootstrapPath() . '/providers.php');

$content = file_get_contents($this->app->bootstrapPath().'/providers.php');

// Verify both new providers were added while maintaining existing ones
expect($content)
->toContain('Modules\Blog\BlogServiceProvider::class')
Expand All @@ -112,7 +112,7 @@ function getPackageProviders($app)

test('registerServiceProvider method returns correct boolean', function () {
$command = $this->app->make(RegisterServiceProviderCommand::class);

expect($command->registerServiceProvider('NewModule'))->toBeTrue()
->and($command->registerServiceProvider('NewModule'))->toBeFalse();
});
});

0 comments on commit ac3314f

Please sign in to comment.