Files
ABSDEV/Developer/Support/ModuleGenerator.php
T
2026-10-12 14:50:13 +01:00

56 lines
3.1 KiB
PHP

<?php
namespace Modules\Developer\Support;
final class ModuleGenerator extends Generator
{
public function generate(string $name, bool $force = false): array
{
$name = $this->studly($name);
$alias = $this->kebab($name);
$root = rtrim(config('developer.modules_path', base_path('Modules')), '/').'/'.$name;
$provider = "Modules\\{$name}\\Providers\\{$name}ServiceProvider";
$files = [
"$root/module.json" => json_encode([
'name' => $name,
'alias' => $alias,
'version' => '1.0.0',
'description' => "{$name} module.",
'keywords' => [$alias],
'priority' => 100,
'providers' => [$provider],
'aliases' => (object) [],
'files' => [],
'requires' => ['Core', 'Administration'],
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES).PHP_EOL,
"$root/Config/config.php" => "<?php\n\nreturn ['name' => '{$name}'];\n",
"$root/Providers/{$name}ServiceProvider.php" => $this->provider($name),
"$root/Providers/RouteServiceProvider.php" => $this->routeProvider($name),
"$root/Routes/web.php" => "<?php\n\nuse Illuminate\\Support\\Facades\\Route;\n\nRoute::middleware(['web', 'auth'])->prefix('administration/{$alias}')->name('administration.{$alias}.')->group(function (): void {\n // Register module routes here.\n});\n",
"$root/Tests/Feature/{$name}ModuleBootTest.php" => $this->test($name),
];
foreach ($files as $path => $contents) {
$this->write($path, $contents, $force);
}
return array_keys($files);
}
private function provider(string $name): string
{
return "<?php\n\nnamespace Modules\\{$name}\\Providers;\n\nuse Illuminate\\Support\\ServiceProvider;\n\nfinal class {$name}ServiceProvider extends ServiceProvider\n{\n public function register(): void\n {\n \$this->app->register(RouteServiceProvider::class);\n \$this->mergeConfigFrom(module_path('{$name}', 'Config/config.php'), '".str($name)->snake()."');\n }\n\n public function boot(): void\n {\n \$this->loadMigrationsFrom(module_path('{$name}', 'Database/Migrations'));\n \$this->loadViewsFrom(module_path('{$name}', 'Resources/views'), '".str($name)->snake()."');\n }\n}\n";
}
private function routeProvider(string $name): string
{
return "<?php\n\nnamespace Modules\\{$name}\\Providers;\n\nuse Illuminate\\Foundation\\Support\\Providers\\RouteServiceProvider as ServiceProvider;\nuse Illuminate\\Support\\Facades\\Route;\n\nfinal class RouteServiceProvider extends ServiceProvider\n{\n public function boot(): void\n {\n Route::middleware('web')->group(module_path('{$name}', 'Routes/web.php'));\n }\n}\n";
}
private function test(string $name): string
{
return "<?php\n\nit('boots the {$name} module', function (): void {\n expect(app()->providerIsLoaded(Modules\\{$name}\\Providers\\{$name}ServiceProvider::class))->toBeTrue();\n});\n";
}
}