| 1: | <?php |
| 2: | |
| 3: | namespace LaravelUi5\Core\Commands; |
| 4: | |
| 5: | use Illuminate\Filesystem\Filesystem; |
| 6: | use Illuminate\Support\Facades\File; |
| 7: | use Illuminate\Support\Str; |
| 8: | |
| 9: | class GenerateUi5Dashboard extends BaseGenerator |
| 10: | { |
| 11: | protected $signature = 'ui5:dashboard |
| 12: | {name : Dashboard name in App/Dashboard format (e.g. Offers/ProjectKpi)} |
| 13: | {--php-ns-prefix=Pragmatiqu : Root namespace prefix for PHP classes} |
| 14: | {--js-ns-prefix=io.pragmatiqu : Root namespace prefix for JS artifacts}'; |
| 15: | |
| 16: | protected $description = 'Generates a new Ui5 Dashboard class using a predefined stub.'; |
| 17: | |
| 18: | protected Filesystem $files; |
| 19: | |
| 20: | public function __construct(Filesystem $files) |
| 21: | { |
| 22: | parent::__construct(); |
| 23: | $this->files = $files; |
| 24: | } |
| 25: | |
| 26: | public function handle(): int |
| 27: | { |
| 28: | $name = $this->argument('name'); |
| 29: | [$app, $dashboard] = $this->parseCamelCasePair($name); |
| 30: | |
| 31: | if (!$this->assertAppExists($app)) { |
| 32: | $this->components->error("App {$app} does not exist."); |
| 33: | return self::FAILURE; |
| 34: | } |
| 35: | |
| 36: | $phpPrefix = rtrim($this->option('php-ns-prefix'), '\\'); |
| 37: | $jsPrefix = rtrim($this->option('js-ns-prefix'), '.'); |
| 38: | |
| 39: | $className = Str::studly($dashboard); |
| 40: | $urlKey = Str::snake($dashboard); |
| 41: | $namespace = "{$phpPrefix}\\{$app}\\Dashboards"; |
| 42: | $classPath = base_path("ui5/{$app}/src/Dashboards"); |
| 43: | $bladePath = base_path("ui5/{$app}/resources/ui5/dashboards"); |
| 44: | |
| 45: | $filePath = "{$classPath}/{$className}.php"; |
| 46: | if (File::exists($filePath)) { |
| 47: | $this->components->error("Dashboard class already exists: {$filePath}"); |
| 48: | return self::FAILURE; |
| 49: | } |
| 50: | |
| 51: | $resourcePath = "{$bladePath}/{$urlKey}.blade.php"; |
| 52: | if (File::exists($resourcePath)) { |
| 53: | $this->components->success("Dashboard template already exists: {$resourcePath}"); |
| 54: | return self::FAILURE; |
| 55: | } |
| 56: | |
| 57: | File::ensureDirectoryExists($classPath); |
| 58: | File::ensureDirectoryExists($bladePath); |
| 59: | |
| 60: | $this->files->put($filePath, $this->compileStub('Ui5Dashboard.stub', [ |
| 61: | 'namespace' => $namespace, |
| 62: | 'class' => $className, |
| 63: | 'ui5Namespace' => implode('.', [$jsPrefix, Str::snake($app), 'dashboards', Str::kebab($dashboard)]), |
| 64: | 'title' => Str::headline($className), |
| 65: | 'description' => "Dashboard for " . Str::headline($className), |
| 66: | 'url_key' => Str::kebab($dashboard), |
| 67: | 'path' => "/../../resources/ui5/dashboards/{$urlKey}.blade.php", |
| 68: | ])); |
| 69: | |
| 70: | $this->files->put($resourcePath, $this->compileStub('Dashboard.blade.stub', [])); |
| 71: | |
| 72: | $this->components->success("Dashboard `{$filePath}` successfully created."); |
| 73: | $this->components->info("💡 Don’t forget to register this dashboard in `config/ui5.php`"); |
| 74: | return self::SUCCESS; |
| 75: | } |
| 76: | } |
| 77: | |