Skip to content

Commit

Permalink
Merge pull request #15 from sarahelsagheir/main
Browse files Browse the repository at this point in the history
Register Service Provider Command  in providers.php
  • Loading branch information
daniel-cintra authored Nov 22, 2024
2 parents f3fc3b0 + f5207db commit f3e8c61
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Console/MakeModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ 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]);
}

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

return true;
}
}


}
62 changes: 62 additions & 0 deletions src/Console/RegisterServiceProviderCommand.php
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);
}
}
7 changes: 5 additions & 2 deletions src/ModularServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Modular\Modular\Console\MakeValidateCommand;
use Modular\Modular\Console\PublishLaravelTranslationsCommand;
use Modular\Modular\Console\PublishSiteFilesCommand;
use Modular\Modular\Console\RegisterServiceProviderCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand Down Expand Up @@ -49,6 +50,8 @@ public function configurePackage(Package $package): void
->hasCommand(MakeMigrationCommand::class)
->hasCommand(PublishLaravelTranslationsCommand::class)
->hasCommand(PublishSiteFilesCommand::class)
->hasCommand(MakeFactoryCommand::class);
->hasCommand(MakeFactoryCommand::class)
->hasCommand(RegisterServiceProviderCommand::class);

}
}
}
118 changes: 118 additions & 0 deletions tests/Console/RegisterServiceProviderCommandTest.php
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();
});

0 comments on commit f3e8c61

Please sign in to comment.