generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from sarahelsagheir/main
Register Service Provider Command in providers.php
- Loading branch information
Showing
4 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Modular\Modular\Console; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class RegisterServiceProviderCommand extends Command | ||
{ | ||
protected $signature = 'modular:register-provider {name}'; | ||
|
||
protected $description = 'Register a module service provider'; | ||
|
||
public function handle(): int | ||
{ | ||
$moduleName = $this->argument('name'); | ||
|
||
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) { | ||
if (trim($line) === '];') { | ||
$returnArrayIndex = $index; | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
use Illuminate\Filesystem\Filesystem; | ||
use Modular\Modular\Console\RegisterServiceProviderCommand; | ||
use Modular\Modular\ModularServiceProvider; | ||
|
||
// Store initial content as a variable in the closure scope | ||
$initialProvidersContent = <<<'PHP' | ||
<?php | ||
return [ | ||
App\Providers\AppServiceProvider::class, | ||
]; | ||
PHP; | ||
|
||
beforeEach(function () use ($initialProvidersContent) { | ||
// Create a temporary providers.php file for testing | ||
$filesystem = new Filesystem; | ||
$bootstrapPath = $this->app->bootstrapPath(); | ||
|
||
// Ensure bootstrap directory exists | ||
if (!$filesystem->exists($bootstrapPath)) { | ||
$filesystem->makeDirectory($bootstrapPath, 0755, true); | ||
} | ||
|
||
// Create providers.php with initial content | ||
$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); | ||
}); | ||
|
||
/** | ||
* Get package providers. | ||
*/ | ||
function getPackageProviders($app) | ||
{ | ||
return [ | ||
ModularServiceProvider::class, | ||
]; | ||
} | ||
|
||
test('it runs provider registration', function () { | ||
|
||
$this->artisan('modular:register-provider', ['name' => 'Blog']) | ||
->assertSuccessful() | ||
->expectsOutput('Service provider for module Blog registered successfully.'); | ||
|
||
$content = file_get_contents($this->app->bootstrapPath() . '/providers.php'); | ||
|
||
// Check that the new provider was added | ||
expect($content) | ||
->toContain('Modules\Blog\BlogServiceProvider::class'); | ||
}); | ||
|
||
test('it prevents duplicate service provider registration', function () { | ||
// First registration | ||
$this->artisan('modular:register-provider', ['name' => 'Blog']) | ||
->assertSuccessful(); | ||
|
||
// Try to register again | ||
$this->artisan('modular:register-provider', ['name' => 'Blog']) | ||
->assertFailed() | ||
->expectsOutput('Service provider for module Blog is already registered.'); | ||
|
||
// Verify the provider appears only once | ||
$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'); | ||
|
||
// Verify the structure is maintained | ||
expect($content) | ||
->toContain('<?php') | ||
->toContain('return [') | ||
->toContain('App\Providers\AppServiceProvider::class') | ||
->toContain('];'); | ||
|
||
// Check that the new provider is properly placed before the closing bracket | ||
expect(strpos($content, 'BlogServiceProvider::class'))->toBeLessThan(strpos($content, '];')); | ||
}); | ||
|
||
test('it properly adds multiple new providers', function () { | ||
// Register multiple providers | ||
$this->artisan('modular:register-provider', ['name' => 'Blog']) | ||
->assertSuccessful(); | ||
$this->artisan('modular:register-provider', ['name' => 'Shop']) | ||
->assertSuccessful(); | ||
|
||
$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') | ||
->toContain('Modules\Shop\ShopServiceProvider::class') | ||
->toContain('App\Providers\AppServiceProvider::class'); | ||
|
||
// Verify the structure is maintained | ||
$lines = explode("\n", $content); | ||
expect($lines)->toContain('<?php'); | ||
expect($lines)->toContain('return ['); | ||
}); | ||
|
||
test('registerServiceProvider method returns correct boolean', function () { | ||
$command = $this->app->make(RegisterServiceProviderCommand::class); | ||
|
||
expect($command->registerServiceProvider('NewModule'))->toBeTrue() | ||
->and($command->registerServiceProvider('NewModule'))->toBeFalse(); | ||
}); |