move into sonardesk
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Developer\Support;
|
||||
|
||||
final class CrudGenerator extends Generator
|
||||
{
|
||||
public function generate(string $module, string $model, bool $force = false): array
|
||||
{
|
||||
$module = $this->studly($module);
|
||||
$model = $this->studly($model);
|
||||
$table = str($model)->plural()->snake()->toString();
|
||||
$route = str($model)->plural()->kebab()->toString();
|
||||
$root = rtrim(config('developer.modules_path', base_path('Modules')), '/').'/'.$module;
|
||||
$timestamp = now()->format('Y_m_d_His');
|
||||
|
||||
$files = [
|
||||
"$root/Models/{$model}.php" => $this->model($module, $model),
|
||||
"$root/Http/Controllers/{$model}Controller.php" => $this->controller($module, $model),
|
||||
"$root/Http/Requests/Store{$model}Request.php" => $this->request($module, $model),
|
||||
"$root/Database/Migrations/{$timestamp}_create_{$table}_table.php" => $this->migration($table),
|
||||
"$root/Resources/views/admin/{$route}/index.blade.php" => $this->indexView($model, $route),
|
||||
"$root/Resources/views/admin/{$route}/form.blade.php" => $this->formView($model),
|
||||
"$root/Tests/Feature/{$model}CrudTest.php" => $this->crudTest($module, $model),
|
||||
];
|
||||
|
||||
foreach ($files as $path => $contents) {
|
||||
$this->write($path, $contents, $force);
|
||||
}
|
||||
|
||||
return array_keys($files);
|
||||
}
|
||||
|
||||
private function model(string $module, string $model): string
|
||||
{
|
||||
return "<?php\n\nnamespace Modules\\{$module}\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nfinal class {$model} extends Model\n{\n protected \$guarded = [];\n\n protected function casts(): array\n {\n return ['is_active' => 'boolean'];\n }\n}\n";
|
||||
}
|
||||
|
||||
private function controller(string $module, string $model): string
|
||||
{
|
||||
$plural = str($model)->plural()->kebab();
|
||||
|
||||
return "<?php\n\nnamespace Modules\\{$module}\\Http\\Controllers;\n\nuse Illuminate\\Http\\RedirectResponse;\nuse Illuminate\\View\\View;\nuse Modules\\{$module}\\Http\\Requests\\Store{$model}Request;\nuse Modules\\{$module}\\Models\\{$model};\n\nfinal class {$model}Controller\n{\n public function index(): View\n {\n return view('".str($module)->snake()."::admin.{$plural}.index', ['records' => {$model}::query()->latest()->paginate()]);\n }\n\n public function store(Store{$model}Request \$request): RedirectResponse\n {\n {$model}::query()->create(\$request->validated());\n return back()->with('status', '{$model} created.');\n }\n}\n";
|
||||
}
|
||||
|
||||
private function request(string $module, string $model): string
|
||||
{
|
||||
return "<?php\n\nnamespace Modules\\{$module}\\Http\\Requests;\n\nuse Illuminate\\Foundation\\Http\\FormRequest;\n\nfinal class Store{$model}Request extends FormRequest\n{\n public function authorize(): bool { return \$this->user() !== null; }\n public function rules(): array { return ['name' => ['required', 'string', 'max:255'], 'is_active' => ['sometimes', 'boolean']]; }\n}\n";
|
||||
}
|
||||
|
||||
private function migration(string $table): string
|
||||
{
|
||||
return "<?php\n\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nreturn new class extends Migration {\n public function up(): void { Schema::create('{$table}', function (Blueprint \$table): void { \$table->id(); \$table->string('name'); \$table->boolean('is_active')->default(true); \$table->timestamps(); }); }\n public function down(): void { Schema::dropIfExists('{$table}'); }\n};\n";
|
||||
}
|
||||
|
||||
private function indexView(string $model, string $route): string
|
||||
{
|
||||
return "<x-layouts.app :title=\"__('{$model}')\">\n <div class=\"space-y-6\"><h1 class=\"text-2xl font-semibold\">{$model}</h1>@foreach (\$records as \$record)<div>{{ \$record->name }}</div>@endforeach{{ \$records->links() }}</div>\n</x-layouts.app>\n";
|
||||
}
|
||||
|
||||
private function formView(string $model): string
|
||||
{
|
||||
return "<form method=\"POST\">@csrf<flux:input name=\"name\" :label=\"__('Name')\" required /><flux:button type=\"submit\" variant=\"primary\">{{ __('Save {$model}') }}</flux:button></form>\n";
|
||||
}
|
||||
|
||||
private function crudTest(string $module, string $model): string
|
||||
{
|
||||
return "<?php\n\nuse Modules\\{$module}\\Models\\{$model};\n\nit('can create a {$model}', function (): void { expect({$model}::query()->create(['name' => 'Example']))->toBeInstanceOf({$model}::class); });\n";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Developer\Support;
|
||||
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use RuntimeException;
|
||||
|
||||
abstract class Generator
|
||||
{
|
||||
public function __construct(protected readonly Filesystem $files) {}
|
||||
|
||||
protected function createDirectory(string $path): void
|
||||
{
|
||||
if (! $this->files->isDirectory($path)) {
|
||||
$this->files->makeDirectory($path, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected function write(string $path, string $contents, bool $force = false): void
|
||||
{
|
||||
if ($this->files->exists($path) && ! $force) {
|
||||
throw new RuntimeException("File already exists: {$path}");
|
||||
}
|
||||
|
||||
$this->createDirectory(dirname($path));
|
||||
$this->files->put($path, $contents);
|
||||
}
|
||||
|
||||
protected function studly(string $value): string
|
||||
{
|
||||
return str($value)->studly()->toString();
|
||||
}
|
||||
|
||||
protected function snake(string $value): string
|
||||
{
|
||||
return str($value)->snake()->toString();
|
||||
}
|
||||
|
||||
protected function kebab(string $value): string
|
||||
{
|
||||
return str($value)->kebab()->toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?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";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Developer\Support;
|
||||
|
||||
final class ThemeGenerator extends Generator
|
||||
{
|
||||
public function generate(string $name, bool $force = false): array
|
||||
{
|
||||
$name = $this->kebab($name);
|
||||
$root = rtrim(config('developer.themes_path', resource_path('themes')), '/').'/'.$name;
|
||||
$files = [
|
||||
"$root/theme.json" => json_encode(['name' => str($name)->headline()->toString(), 'slug' => $name, 'version' => '1.0.0', 'parent' => 'common'], JSON_PRETTY_PRINT).PHP_EOL,
|
||||
"$root/views/layouts/app.blade.php" => "<!DOCTYPE html><html lang=\"{{ str_replace('_', '-', app()->getLocale()) }}\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>{{ \$title ?? config('app.name') }}</title>@vite(['resources/css/app.css', 'resources/js/app.js'])</head><body>{{ \$slot }}</body></html>\n",
|
||||
"$root/views/home.blade.php" => "<x-layouts.app><main><h1>{{ config('app.name') }}</h1></main></x-layouts.app>\n",
|
||||
"$root/assets/css/theme.css" => ":root { --theme-radius: 0.5rem; }\n",
|
||||
"$root/README.md" => '# '.str($name)->headline()." Theme\n\nGenerated by `php artisan theme:make {$name}`.\n",
|
||||
];
|
||||
foreach ($files as $path => $contents) {
|
||||
$this->write($path, $contents, $force);
|
||||
}
|
||||
|
||||
return array_keys($files);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user