1: <?php
2:
3: namespace LaravelUi5\Core\Commands;
4:
5: use Exception;
6: use Illuminate\Support\Facades\File;
7: use Illuminate\Support\Str;
8: use Illuminate\Filesystem\Filesystem;
9:
10: class GenerateUi5TileCommand extends BaseGenerator
11: {
12: protected $signature = 'ui5:tile
13: {name : Tile name in App/Tile format (e.g. Offers/ProjectKpi)}
14: {--php-ns-prefix=Pragmatiqu : Root namespace prefix for PHP classes}
15: {--js-ns-prefix=io.pragmatiqu : Root namespace prefix for JS artifacts}
16: {--title= : The title of the tile}
17: {--description= : The description of the tile}';
18:
19: protected $description = 'Create a new UI5 tile artifact with related resources';
20:
21: protected Filesystem $files;
22:
23: public function __construct(Filesystem $files)
24: {
25: parent::__construct();
26: $this->files = $files;
27: }
28:
29: /**
30: * @throws Exception
31: */
32: public function handle(): int
33: {
34: $name = $this->argument('name');
35: [$app, $tile] = $this->parseCamelCasePair($name);
36:
37: if (!$this->assertAppExists($app)) {
38: $this->components->error("App {$app} does not exist.");
39: return self::FAILURE;
40: }
41:
42: $phpPrefix = rtrim($this->option('php-ns-prefix'), '\\');
43: $jsPrefix = rtrim($this->option('js-ns-prefix'), '.');
44: $urlKey = Str::snake($tile);
45: $namespace = "{$phpPrefix}\\{$app}\\Tiles\\{$tile}";
46: $title = $this->option('title') ?? $tile;
47: $description = $this->option('description') ?? 'Tile generated via ui5:tile';
48:
49: $targetPath = base_path("ui5/{$app}/src/Tiles/{$tile}");
50: if (File::exists("$targetPath/Tile.php")) {
51: $this->components->error("Tile {$name} already exists in Ui5App module {$app}.");
52: return self::FAILURE;
53: }
54:
55: File::ensureDirectoryExists($targetPath);
56:
57: // Create Ui5Tile
58: $this->files->put("$targetPath/Tile.php", $this->compileStub('Ui5Tile.stub', [
59: 'namespace' => $namespace,
60: 'class' => $tile,
61: 'ui5Namespace' => implode('.', [$jsPrefix, Str::snake($app), 'tiles', $urlKey]),
62: 'urlKey' => $urlKey,
63: 'title' => $title,
64: 'description' => $description,
65: ]));
66:
67: // Create DataProvider
68: $this->files->put("$targetPath/Provider.php", $this->compileStub('TileProvider.stub', [
69: 'namespace' => $namespace,
70: ]));
71:
72: $this->components->success("UI5 Tile '$tile' created successfully.");
73: $this->components->info("💡 Don’t forget to register this card in your module");
74:
75: return self::SUCCESS;
76: }
77: }
78: