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 GenerateUi5Action extends BaseGenerator
10: {
11: protected $signature = 'ui5:action
12: {name : Action name in App/Action format (e.g. Offers/ToggleLock)}
13: {--method=POST : The HTTP method to use for this action}
14: {--with-params : Allow uri encoded parameters}
15: {--php-ns-prefix=Pragmatiqu : Root namespace prefix for PHP classes}
16: {--js-ns-prefix=io.pragmatiqu : Root namespace prefix for JS artifacts}';
17:
18: protected $description = 'Generates a new Ui5 Action class using a predefined stub.';
19:
20: protected Filesystem $files;
21:
22: public function __construct(Filesystem $files)
23: {
24: parent::__construct();
25: $this->files = $files;
26: }
27:
28: public function handle(): int
29: {
30: $name = $this->argument('name');
31: $phpPrefix = rtrim($this->option('php-ns-prefix'), '\\');
32: $jsPrefix = rtrim($this->option('js-ns-prefix'), '.');
33: $withParams = $this->option('with-params') ?? false;
34: [$app, $action] = $this->parseCamelCasePair($name);
35:
36: if (!$this->assertAppExists($app)) {
37: $this->components->error("App {$app} does not exist.");
38: return self::FAILURE;
39: }
40:
41: $className = Str::studly($action);
42: $urlKey = Str::snake($action);
43: $slug = Str::kebab($action);
44: $phpNamespace = "{$phpPrefix}\\{$app}\\Actions\\{$className}";
45: $classDir = base_path("ui5/{$app}/src/Actions/{$className}");
46: $variant = $withParams ? 'extends AbstractUi5Action' : 'implements Ui5ActionInterface';
47: $useVariant = $withParams ? 'LaravelUi5\\Core\\Ui5\\AbstractUi5Action' : 'LaravelUi5\\Core\\Ui5\\Contracts\\Ui5ActionInterface';
48:
49: $classPath = "{$classDir}/Action.php";
50: if (File::exists($classPath)) {
51: $this->components->error("Action class already exists: {$classPath}");
52: return self::FAILURE;
53: }
54:
55: File::ensureDirectoryExists($classDir);
56:
57: $this->files->put($classPath, $this->compileStub('Ui5Action.stub', [
58: 'phpNamespace' => $phpNamespace,
59: 'ui5Namespace' => implode('.', [$jsPrefix, Str::snake($app), 'actions', $urlKey]),
60: 'title' => Str::headline($className),
61: 'description' => "Action for " . Str::headline($className),
62: 'slug' => $slug,
63: 'variant' => $variant,
64: 'useVariant' => $useVariant,
65: 'method' => trim($this->option('method'))
66: ]));
67: $this->files->put("{$classDir}/Handler.php", $this->compileStub('ActionHandler.stub', [
68: 'phpNamespace' => $phpNamespace,
69: ]));
70:
71: $this->components->success("Action created: {$classPath}");
72: $this->components->info("💡 Don’t forget to register this action in your module");
73:
74: return self::SUCCESS;
75: }
76: }
77: