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: 'ui5Namespace' => $ui5Namespace,
62: 'class' => $className,
63: 'moduleClass' => $moduleName,
64: 'srcPath' => "{$targetPath}/resources/app"
65: ]));
66:
67: // Create Ui5App
68: $this->files->put($targetFile, $this->compileStub('Ui5AppSelfContained.stub', [
69: 'namespace' => $phpNamespace,
70: 'class' => $className,
71: 'name' => $name,
72: 'ui5Namespace' => $ui5Namespace,
73: 'urlKey' => $urlKey,
74: 'title' => $title,
75: 'description' => $description,
76: 'component' => str_replace('.', '/', $ui5Namespace)
77: ]));
78:
79: // Manifest
80: $this->files->put("{$targetPath}/src/{$name}Manifest.php", $this->compileStub('Ui5Manifest.stub', [
81: 'phpNamespace' => $phpNamespace,
82: 'class' => $name
83: ]));
84:
85: // composer.json
86: $this->files->put("{$targetPath}/composer.json", $this->compileStub('composer.stub', [
87: 'packagePrefix' => $packagePrefix,
88: 'urlKey' => $urlKey,
89: 'description' => $description,
90: 'namespace' => json_encode($phpNamespace),
91: ]));
92:
93: // ServiceProvider
94: $this->files->put("{$targetPath}/src/{$name}ServiceProvider.php", $this->compileStub('ServiceProvider.stub', [
95: 'namespace' => $phpNamespace,
96: 'name' => $name,
97: ]));
98:
99: File::ensureDirectoryExists("{$targetPath}/resources/app/controller");
100: File::ensureDirectoryExists("{$targetPath}/resources/app/i18n");
101: File::ensureDirectoryExists("{$targetPath}/resources/app/view");
102:
103: $this->files->put("{$targetPath}/resources/app/manifest.json", $this->compileStub('manifest.stub', [
104: 'ui5Namespace' => $ui5Namespace,
105: 'title' => $title,
106: 'description' => $description,
107: 'version' => '1.0.0',
108: ]));
109: $this->files->put("{$targetPath}/resources/app/Component.js", $this->compileStub('Component.stub', [
110: 'ui5Namespace' => $ui5Namespace,
111: ]));
112: $this->files->put("{$targetPath}/resources/app/controller/App.controller.js", $this->compileStub('App.controller.stub', [
113: 'ui5Namespace' => $ui5Namespace,
114: ]));
115: $this->files->put("{$targetPath}/resources/app/view/App.view.xml", $this->compileStub('App.view.stub', [
116: 'ui5Namespace' => $ui5Namespace,
117: ]));
118: $this->files->put("{$targetPath}/resources/app/i18n/i18n.properties", $this->compileStub('i18n.stub', [
119: 'title' => $title,
120: 'description' => $description,
121: ]));
122: $this->files->put("{$targetPath}/resources/app/i18n/i18n_en.properties", $this->compileStub('i18n.stub', [
123: 'title' => $title,
124: 'description' => $description,
125: ]));
126:
127: $this->components->success("UI5App '$name' created successfully.");
128: return self::SUCCESS;
129: }
130: }
131: