1: <?php
2:
3: namespace LaravelUi5\Core\Commands;
4:
5: use Exception;
6: use Illuminate\Filesystem\Filesystem;
7: use Illuminate\Support\Facades\File;
8: use Illuminate\Support\Str;
9:
10: class GenerateUi5CardCommand extends BaseGenerator
11: {
12: protected $signature = 'ui5:card
13: {name : The card in the form App/Card}
14: {--php-ns-prefix=Pragmatiqu : The prefix for the php package}
15: {--js-ns-prefix=io.pragmatiqu : The prefix for the js package}
16: {--title= : The title of the card}
17: {--description= : The description of the card}';
18:
19: protected $description = 'Generates a new UI5 Card including provider and manifest template';
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, $card] = $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: $phpNamespacePrefix = rtrim($this->option('php-ns-prefix'), '\\');;
43: $jsNamespacePrefix = rtrim($this->option('js-ns-prefix'), '.');
44:
45: $root = base_path("ui5/{$app}");
46: $src = "{$root}/src/Cards/{$card}";
47: $res = "{$root}/resources/ui5/cards";
48:
49: $providerClass = "{$card}Provider";
50: $slug = Str::kebab(Str::replaceLast('Card', '', $card));
51: $ui5Namespace = $jsNamespacePrefix . '.' . Str::kebab($app) . '.' . $slug;
52:
53: $phpNamespace = "{$phpNamespacePrefix}\\{$app}\\Cards\\{$card}";
54:
55: if (File::exists("{$src}/Card.php")) {
56: $this->components->error("Ui5Card {$card} already exists.");
57: return self::FAILURE;
58: }
59:
60: // Create directories
61: File::ensureDirectoryExists($src);
62: File::ensureDirectoryExists($res);
63:
64: // Stub: Card class
65: $this->files->put("{$src}/Card.php", $this->compileStub('Ui5Card.stub', [
66: 'phpNamespace' => $phpNamespace,
67: 'providerClass' => $providerClass,
68: 'ui5Namespace' => $ui5Namespace,
69: 'urlKey' => $slug,
70: 'title' => $this->option('title') ?? Str::headline(Str::replaceLast('Card', '', $card)),
71: 'description' => $this->option('description') ?? "Displays key data for " . Str::headline(Str::replaceLast('Card', '', $card)) . ".",
72: ]));
73:
74: // Stub: Provider class
75: $this->files->put("{$src}/Provider.php", $this->compileStub('CardProvider.stub', [
76: 'phpNamespace' => $phpNamespace,
77: ]));
78:
79: // Stub: manifest.blade.php
80: $this->files->put("{$res}/{$slug}.blade.php", $this->compileStub('CardManifest.stub', [
81: 'urlKey' => $slug,
82: 'version' => '1.0.0',
83: 'title' => $this->option('title') ?? Str::headline(Str::replaceLast('Card', '', $card)),
84: 'subTitle' => $this->option('description') ?? 'Optional Subtitle',
85: ]));
86:
87: $this->components->info("Generated UI5Card {$card} and corresponding artifacts in Ui5App {$app}.");
88: $this->components->info("💡 Don’t forget to register this card in your module");
89: return self::SUCCESS;
90: }
91: }
92: