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 GenerateSelfContainedUi5AppCommand extends BaseGenerator
11: {
12:
13: protected $signature = 'ui5:sca
14: {name : The name of the ui5 app}
15: {--package-prefix=pragmatiqu : The composer package namespace prefix}
16: {--php-ns-prefix=Pragmatiqu : Root namespace prefix for PHP classes}
17: {--js-ns-prefix=io.pragmatiqu : Root namespace prefix for JS artifacts}
18: {--title= : The title of the app}
19: {--description= : The description of the app}
20: {--vendor="Pragmatiqu IT GmbH" : The vendor of the module}';
21:
22: protected Filesystem $files;
23:
24: public function __construct(Filesystem $files)
25: {
26: parent::__construct();
27: $this->files = $files;
28: }
29:
30: /**
31: * @throws Exception
32: */
33: public function handle(): int
34: {
35: $name = $this->argument('name');
36: $this->assertCamelCase('Ui5App', $name);
37:
38: $phpPrefix = rtrim($this->option('php-ns-prefix'), '\\');
39: $jsPrefix = rtrim($this->option('js-ns-prefix'), '.');
40: $packagePrefix = rtrim($this->option('package-prefix'), '/');
41: $urlKey = Str::snake($name);
42: $ui5Namespace = implode('.', [$jsPrefix, $urlKey]);
43: $phpNamespace = "{$phpPrefix}\\{$name}";
44: $className = "{$name}App";
45: $moduleName = "{$name}Module";
46: $title = $this->option('title') ?? $name;
47: $description = $this->option('description') ?? 'Ui5App generated via ui5:sca';
48:
49: $targetPath = base_path("ui5/{$name}");
50:
51: $targetFile = "{$targetPath}/src/{$className}.php";
52: if (File::exists($targetFile)) {
53: $this->components->error("Ui5App {$name} already exists.");
54: return self::FAILURE;
55: }
56:
57: File::ensureDirectoryExists("{$targetPath}/src/");
58:
59: $this->files->put("{$targetPath}/src/{$moduleName}.php", $this->compileStub('Ui5ModuleApp.stub', [
60: 'phpNamespace' => $phpNamespace,
61: 'class' => $className,
62: 'moduleClass' => $moduleName,
63: ]));
64:
65: // Create Ui5App
66: $this->files->put($targetFile, $this->compileStub('Ui5AppSelfContained.stub', [
67: 'namespace' => $phpNamespace,
68: 'class' => $className,
69: 'name' => $name,
70: 'ui5Namespace' => $ui5Namespace,
71: 'urlKey' => $urlKey,
72: 'title' => $title,
73: 'description' => $description,
74: 'component' => str_replace('.', '/', $ui5Namespace)
75: ]));
76:
77: // Manifest
78: $this->files->put("{$targetPath}/src/{$name}Manifest.php", $this->compileStub('Ui5Manifest.stub', [
79: 'phpNamespace' => $phpNamespace,
80: 'class' => $name
81: ]));
82:
83: // composer.json
84: $this->files->put("{$targetPath}/composer.json", $this->compileStub('composer.stub', [
85: 'packagePrefix' => $packagePrefix,
86: 'urlKey' => $urlKey,
87: 'description' => $description,
88: 'namespace' => json_encode($phpNamespace),
89: ]));
90:
91: // ServiceProvider
92: $this->files->put("{$targetPath}/src/{$name}ServiceProvider.php", $this->compileStub('ServiceProvider.stub', [
93: 'namespace' => $phpNamespace,
94: 'name' => $name,
95: ]));
96:
97: File::ensureDirectoryExists("{$targetPath}/resources/app/controller");
98: File::ensureDirectoryExists("{$targetPath}/resources/app/i18n");
99: File::ensureDirectoryExists("{$targetPath}/resources/app/view");
100:
101: $this->files->put("{$targetPath}/resources/app/manifest.json", $this->compileStub('manifest.stub', [
102: 'ui5Namespace' => $ui5Namespace,
103: 'title' => $title,
104: 'description' => $description,
105: 'version' => '1.0.0',
106: ]));
107: $this->files->put("{$targetPath}/resources/app/Component.js", $this->compileStub('Component.stub', [
108: 'ui5Namespace' => $ui5Namespace,
109: ]));
110: $this->files->put("{$targetPath}/resources/app/controller/App.controller.js", $this->compileStub('App.controller.stub', [
111: 'ui5Namespace' => $ui5Namespace,
112: ]));
113: $this->files->put("{$targetPath}/resources/app/view/App.view.xml", $this->compileStub('App.view.stub', [
114: 'ui5Namespace' => $ui5Namespace,
115: ]));
116: $this->files->put("{$targetPath}/resources/app/i18n/i18n.properties", $this->compileStub('i18n.stub', [
117: 'title' => $title,
118: 'description' => $description,
119: ]));
120: $this->files->put("{$targetPath}/resources/app/i18n/i18n_en.properties", $this->compileStub('i18n.stub', [
121: 'title' => $title,
122: 'description' => $description,
123: ]));
124:
125: $this->components->success("UI5App '$name' created successfully.");
126: return self::SUCCESS;
127: }
128: }
129: